• Resolved ytian03

    (@ytian03)


    Hi,

    I am having a problem converting integers to time. I am getting the right data but when I am trying to convert it I get an error or a different date.

    Here’s my code:
    case ‘_date_printed’ :
    $platform = get_post_meta( $post_id, ‘_date_printed’, true );
    //$output = strtotime($platform);
    if( empty($platform)) $platform = ”;
    echo ‘<span>’.$platform.'</span>’; // display the data
    break;

    And this is the output:

    And when I try to convert it to time:

    case ‘_date_printed’ :
    $platform = get_post_meta( $post_id, ‘_date_printed’, true );
    $output = strtotime($platform);
    if( empty($output )) $output = ”;
    echo ‘<span>’.$output .'</span>’; // display the data
    break;

    Here’s the output:

Viewing 15 replies - 1 through 15 (of 15 total)
  • Anonymous User

    (@anonymized-20801613)

    @ytian03 the data above have empty output section. Can you give us both examples so we can compare the output?

    Plugin Support Zubair Zahid (woo-hc)

    (@doublezed2)

    Hello ytian03,

    Thank you for reaching out to WooCommerce support. I understand that you are encountering an issue when trying to convert integers to time and are getting errors or incorrect dates as a result.

    I must inform you of our support policy which states that we do not offer direct support for custom code. However, I am more than willing to guide you to resources that can assist with your PHP coding concerns. For further reading and examples on handling dates and times in PHP, please visit?https://www.epochconverter.com/programming/php.

    If the issue persists or if any further errors arise, it may be valuable to consult with a developer who can provide a more detailed look into your specific situation and offer a tailored solution.

    Should you have any other questions or need assistance with anything related to WooCommerce, please let me know, and I’d be happy to help.

    Best regards.

    Anonymous User

    (@anonymized-20801613)

    Based on the information above, ChatGPT has learned how to use PHP Date – Time formats and here is the suggestion.

    It seems like the issue might be related to the format of the date stored in the _date_printed meta field. The strtotime function in PHP expects the date to be in a specific format, and if the input date format is not recognized, it might return unexpected results or produce an error.

    Here are a few steps you can take to troubleshoot and resolve the issue:

    1. Check the Date Format: Verify that the date stored in the _date_printed meta field is in a format that strtotime can understand. Common formats include ‘Y-m-d H:i:s’ (e.g., ‘2022-01-14 12:30:00’).
    2. Debugging: Add some debugging statements to your code to inspect the values:
    case '_date_printed':
        $platform = get_post_meta($post_id, '_date_printed', true);
        echo '<span>Original Date: ' . $platform . '</span>'; // Display the original date
        
        $output = strtotime($platform);
        echo '<span>Converted Date: ' . $output . '</span>'; // Display the converted date
        
        if (empty($output)) $output = '';
        echo '<span>Final Output: ' . $output . '</span>'; // Display the final output
        break;
    
    1. This will help you see the original date, the result of strtotime, and the final output.
    2. Specify Date Format: If the date format is not recognized by strtotime, consider using the DateTime class with a specific format:
    case '_date_printed':
        $platform = get_post_meta($post_id, '_date_printed', true);
        $date = DateTime::createFromFormat('YourDateFormatHere', $platform);
        
        if ($date !== false) {
            $output = $date->getTimestamp();
            echo '<span>Converted Date: ' . $output . '</span>';
        } else {
            echo '<span>Invalid Date Format</span>';
        }
        
        break;
    
    1. Replace 'YourDateFormatHere' with the actual format of your date.

    By following these steps, you should be able to identify the issue and handle date conversion appropriately. If you encounter any specific error messages, they can provide additional clues about what might be going wrong.

    Hey, @ytian03!

    You can also visit the WooCommerce Facebook group or the #developers channel of the WooCommerce Community Slack. We’re lucky to have a great community of open-source developers for WooCommerce, and many of our developers hang out there too.

    I hope this helps!

    I’m also going to leave this open for a bit longer so you can check @rokmeglic’s suggestion.

    Have a wonderful day!

    Thread Starter ytian03

    (@ytian03)

    Hi @Rok Megli?,

    Thank you for your reply.
    I’ve tried the code you gave and got an invalid time.

    [url=https://ibb.co/ryVBnzg][img]https://i.ibb.co/88HyFfJ/date.png[/img][/url]

    And here is the value I am getting with my code

    [url=https://ibb.co/hVhCYmc][img]https://i.ibb.co/x6P3F7Y/date2.png[/img][/url]

    Hope you can help me.

    Anonymous User

    (@anonymized-20801613)

    Let me try to create time from your values:

    <?php
    $timestamp = 1704770979;
    $date_format = 'Y-m-d H:i:s'; // You can customize the format as needed
    
    $human_readable_date = date($date_format, $timestamp);
    
    echo $human_readable_date;
    ?>
    

    Try this service – Epoch Converter – Unix Timestamp Converter and then this code

    <?php
    $timestamp = 1703202615;
    $date_format = 'Y-m-d H:i:s'; // You can customize the format as needed
    
    $human_readable_date = date($date_format, $timestamp);
    
    echo $human_readable_date;
    ?>
    

    Does it work?

    Thread Starter ytian03

    (@ytian03)

    Hi @rokmeglic,

    No, it doesn’t work.

    Here is my whole code,

    // ADDING A CUSTOM COLUMN TITLE TO ADMIN ORDER LIST
    add_filter( ‘manage_edit-shop_order_columns’, ‘custom_shop_order_column’, 12, 1 );
    function custom_shop_order_column($columns)
    {
    // Set “Actions” column after the new colum
    $action_column = $columns[‘order_actions’]; // Set the title in a variable
    unset($columns[‘order_actions’]); // remove “Actions” column

    //add the new columns
    $columns['_purchase_order'] = '<span>'.__( 'Purchase Order','woocommerce').'</span>'; // title
    $columns['important_notes'] = '<span>'.__( 'Important Notes','woocommerce').'</span>'; // title
    $columns['_date_printed'] = '<span>'.__( 'Printed Date','woocommerce').'</span>'; // title
    
    // Set back "Actions" column
    $columns['order_actions'] = $action_column;
    
    return $columns;

    }

    // ADDING THE DATA FOR EACH ORDERS COLUMN
    add_action( ‘manage_shop_order_posts_custom_column’ , ‘custom_order_list_column_content’, 10, 2 );
    function custom_order_list_column_content( $column, $post_id )
    {

    // HERE get the data from your custom field (set the correct meta key below)
    switch ( $column )
    {
        case '_purchase_order' :
            $platform = get_post_meta( $post_id, '_purchase_order', true );
            if( empty($platform)) $platform = '';
            echo '<span>'.$platform.'</span>'; // display the data
            break;
    
        case '_date_printed' :
            add_action( 'woocommerce_order_status_printed', 'action_on_order_status_printed', 10, 2 );
            function action_on_order_status_printed( $order_id, $order ) {
            // Set your default time zone (https://php.net/manual/en/timezones.php)
            date_default_timezone_set('Australia/Brisbane');
            //$order->update_meta_data('_date_printed', date("now") );
            $order->save();
            }
    
            if( $order->has_status('wc-printed') ){
            // Set your default time zone (https://php.net/manual/en/timezones.php)
            date_default_timezone_set('Australia/Brisbane');
            $date_printed = date( "d/M/Y g:i:s", $order->get_meta('_date_printed') );
            echo '<span>'.$date_printed.'</span>';
            }
    
                break;
    }

    }

    Anonymous User

    (@anonymized-20801613)

    OK, can you now export the dates you get from database and copy it here. I will manually convert it to dates, so we will see what works and what not. Are you able to do this?

    Like you did here date2 hosted at ImgBB — ImgBB (ibb.co), but structured with:

    case '_purchase_order' :

    echo time

        case '_date_printed' :
    

    echo time

            if( $order->has_status('wc-printed') ){
    

    echo time

    P.S: Why you have $order->save in your code?

    Thread Starter ytian03

    (@ytian03)

    Hi @rokmeglic,

    Still haven’t solved this issue. Sorry for the late reply.
    Here my new code and the output

    add_action( ‘manage_shop_order_posts_custom_column’ , ‘display_admin_order_list_custom_column_content’, 20, 2 );

    function display_admin_order_list_custom_column_content( $column, $post_id )

    {
    global $the_order;
    switch ( $column )
    {
    case ‘my-column1’ :
    // Get custom order metadata
    $value = $the_order->get_meta(‘_date_printed’);
    if ( ! empty($value) ) {
    echo $value;
    }
    // For testing (to be removed) – Empty value case
    else {
    echo ‘<small>(<em>no value</em>)</small>’;
    }
    break;
    }
    }

    OUTPUT:
    https://ibb.co/6tmg5QP

    Anonymous User

    (@anonymized-20801613)

    You said:

    I am getting the right data but when I am trying to convert it I get an error or a different date

    … however, I can see there are multiple “no value” present. There are only few columns with time value there? How come?

    For values which are present, you can use, Epoch Converter – Unix Timestamp Converter. For example, one of your value is “1703202615”, which converts to:

    Thread Starter ytian03

    (@ytian03)

    Hi @rokmeglic,

    Because Completed and Printed only have a value for “_date_printed”.
    I am also using that. I changed my codes that is why I am getting a different output now.

    I am also trying this on Zoho Analytics. I can convert the “_date_printed” to the correct time. Zoho Analytics is connected to Woocommerce and they share the same database.

    See this screenshot.
    https://ibb.co/dLsD47J

    Anonymous User

    (@anonymized-20801613)

    Let’s devide this problem into multiple steps.
    First, create an output, which will have all time unix timestamp present for all valus.

    If you look at the Screenshot-10 hosted at ImgBB — ImgBB you can see there are several missing.

    After you will have this in place, send us a new screenshot with this times values. Then we will go to the next step.

    Thread Starter ytian03

    (@ytian03)

    Hi,

    This is my new code. It is working but I am not getting all the data. Some have “value” but some don’t. All of them should have value (base on screenshot).

    add_filter( ‘manage_edit-shop_order_columns’, ‘add_admin_order_list_custom_column’, 20 );
    function add_admin_order_list_custom_column($columns)
    {
    $reordered_columns = array();

    // Inserting columns to a specific location
    foreach( $columns as $key => $column){
        $reordered_columns[$key] = $column;
        if( $key ==  'order_status' ){
            // Inserting after "Status" column
            $reordered_columns['my-column1'] = __( 'Date Printed','theme_domain');
        }
    }
    return $reordered_columns;

    }

    // Adding custom fields meta data for each new column
    add_action( ‘manage_shop_order_posts_custom_column’ , ‘display_admin_order_list_custom_column_content’, 20, 2 );
    function display_admin_order_list_custom_column_content( $column, $post_id )

    {
    global $the_order;

    switch ( $column )
    {
        case 'my-column1' :
            // Get custom order metadata
            $value = $the_order->get_meta( '_date_printed');
            //$platform = get_post_meta( $order_id, '_date_printed', true );
            $friendly_date = wp_date( 'Y/m/d', $value, new DateTimeZone('Australia/Brisbane') );
            if ( ! empty($friendly_date) ) {
                echo $friendly_date;
            }
            // For testing (to be removed) - Empty value case
            else {
                echo '<small>(<em>no value</em>)</small>';
            }
            break;
    
    }

    }

    This is the output
    https://ibb.co/JrM8H3D

    Do you have any idea why I can’t fetch all the data using my meta key?
    It is weird that some have value and some don’t. I already checked my database and yes, all of them should have value for _date_printed meta key


    Anonymous User

    (@anonymized-20801613)

    Can you create more specific IF?

    if ( ! empty($friendly_date) ) {
                echo $friendly_date;
            } else if ( ! empty($value) ) {
            echo $value;
    }
            else {
                echo '<small>(<em>no value</em>)</small>';
            }

    With this you will know, if the value does not come from order, or it was not converted into freindly_date.

    Thread Starter ytian03

    (@ytian03)

    Hi @rokmeglic,

    Thank you for your help. It is all good now. ??

Viewing 15 replies - 1 through 15 (of 15 total)
  • The topic ‘Convert int to time’ is closed to new replies.