Home > Software engineering >  PHP 8 error Fatal error: Uncaught TypeError: strftime()
PHP 8 error Fatal error: Uncaught TypeError: strftime()

Time:11-05

The code below works fine with PHP 7.4 but in PHP 8 it gives error.

// Add a Header
function filter_manage_edit_shop_order_columns( $columns ) {
    // Add new column
    $columns['pickup_date'] = 'Afhaaldatum';

    return $columns;
}
add_filter( 'manage_edit-shop_order_columns', 'filter_manage_edit_shop_order_columns', 10, 1 );

// Populate the Column
function action_manage_shop_order_posts_custom_column( $column, $post_id ) {
    // Compare
    if ( $column == 'pickup_date' ) {
        // Get order
        $order = wc_get_order( $post_id );      
        // Loop though order shipping methods
        foreach( $order->get_items('shipping') as $shipping_item ) {
            // Get appoitment start
            $pickup_appointment_start = $shipping_item->get_meta( '_pickup_appointment_start' );
        
           date_default_timezone_set('Europe/Amsterdam');       
        
            /* Set locale to Dutch */
            setlocale(LC_ALL, 'nl_NL');
            $startdate = strftime('%e %B %Y om %H:%M uur', $pickup_appointment_start );
    
            if(!empty($startdate))
         //         echo '<p>' .$startdate. '  <br> om  ' . $endtime . '  uur</p>';
              echo '<p>' .$startdate. '</p>';

            // Testing (to be removed) - Empty value case
            else
                echo '<small>-</small>';

            break;        
        }       
    }
}
add_action( 'manage_shop_order_posts_custom_column' , 'action_manage_shop_order_posts_custom_column', 10, 2 );

This is the error:

Fatal error: Uncaught TypeError: strftime(): Argument #2 ($timestamp) must be of type ?int, string given in /home/website/domains/WEBSITEX/public_html/debug/wp-content/plugins/insert-php/includes/class.execute.snippet.php(635) : eval()'d code:29 
Stack trace:
#0 /home/website/domains/WEBSITEX/public_html/debug/wp-content/plugins/insert-php/includes/class.execute.snippet.php(635) : eval()'d code(29): strftime('%e %B %Y om %H:...', '') 
#1 /home/website/domains/WEBSITEX/public_html/debug/wp-includes/class-wp-hook.php(308): action_manage_shop_order_posts_custom_column('pickup_date', 49721) 
#2 /home/website/domains/WEBSITEX/public_html/debug/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) 
#3 /home/website/domains/WEBSITEX/public_html/debug/wp-includes/plugin.php(517): WP_Hook->do_action(Array) 
#4 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-posts-list-table.php(1363): do_action('manage_shop_ord...', 'pickup_date', 49721) 
#5 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-list-table.php(1532): WP_Posts_List_Table->column_default(Object(WP_Post), 'pickup_date') 
#6 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-posts-list-table.php(1397): WP_List_Table->single_row_columns(Object(WP_Post)) 
#7 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-posts-list-table.php(807): WP_Posts_List_Table->single_row(Object(WP_Post), 0) 
#8 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-posts-list-table.php(783): WP_Posts_List_Table->_display_rows(Array, 0) 
#9 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-list-table.php(1444): WP_Posts_List_Table->display_rows() 
#10 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/includes/class-wp-list-table.php(1371): WP_List_Table->display_rows_or_placeholder() 
#11 /home/website/domains/WEBSITEX/public_html/debug/wp-admin/edit.php(487): WP_List_Table->display() 
#12 {main} thrown in /home/website/domains/WEBSITEX/public_html/debug/wp-content/plugins/insert-php/includes/class.execute.snippet.php(635) : eval()'d code on line 29

i changed the format from datetime but that is also not working

CodePudding user response:

A careful reading of the first line of your error message reveals the problem.

Fatal error: Uncaught TypeError: strftime(): Argument #2 ($timestamp) must be of type ?int, string given

The only line in your snippet using that function is

$startdate = strftime('%e %B %Y om %H:%M uur', $pickup_appointment_start );

You get that $pickup_appointment_start from WooCommerce's ->get_meta() function, here.

$pickup_appointment_start = $shipping_item->get_meta( '_pickup_appointment_start' );

As your error message makes clear, you got back a text string for your metadata. A timestamp is an integer like 1667571169. Is it possible your metadata for that particular item is rubbish? Whatever it is, you can force it to be an integer with intval(). But it might come out zero.

The php manual page for strftime() makes clear that the function is deprecated.

You should replace it with a call to wp_date(). That function returns false if the timestamp is rubbish. Use it with your site settings to find your localized formatting.

$startdate = wp_date(
     get_option( 'date_format' ) . ' ' . get_option( 'time_format' ),
     $pickup_appointment_start,
     get_option( 'timezone_string' );

if ( ! $startdate )  ... echo etc.
  • Related