0

I put my timezone in America/Lima ( Setting > General )

But current_time('timestamp') show other datetime.

For example: Now in Lima it is 2015-10-25 12:01:00 but current_time('timestamp') says : 2016-10-24 19:01:05

Something am I doing wrong?

PD: My variables in wp_option are: gmt_offset is empty, timezone_string is America/Lima

Regards

  • On the Settings-General page, where you set the timezone, you'll see a string like this: "Universal time (UTC) is 2016-10-25 17:07:58. Local time is 2016-10-25 12:07:58." Are the times shown on that page correct for you? – Otto Oct 25 '16 at 17:08
  • yes! this date in Settings-General is correct! Current says: "La hora universal (UTC) es 25-10-2016 19:23:19. La hora local es 25-10-2016 14:23:19.." But current_time('timestamp') says 2016-10-25 09:23:19 – Alexander Oct 25 '16 at 19:23
0

Try passing in a second parameter to tell it to use the GMT offset.

current_time( 'timestamp', true );
0

Set the DateTimeZone of a DateTime object to ensure the correct zone.

$d = date( 'c', time() );

echo $d  . '<br /><br />';

$timezone = get_option('timezone_string');
if( empty($timezone) ) $timezone = 'America/Lima';

$t = new DateTime($d);
$t->setTimezone(new DateTimeZone( $timezone ));

echo "\tTime in ${timezone}\t" . $t->format( 'c' );

// 2016-10-25T06:10:16+00:00
// Time in America/Lima 2016-10-25T01:10:16-05:00
0

Cool! Thanks for the answer, it has helped me a lot.

I'm trying to get my unpaid orders, automatically cancel them after a certain period in Woocommerce. It doesn't work because this use current_time('timestamp') by default.

woocommerce/includes/wc-order-functions.php

function wc_cancel_unpaid_orders() {
    global $wpdb;
    $held_duration = get_option( 'woocommerce_hold_stock_minutes' );

    if ( $held_duration < 1 || get_option( 'woocommerce_manage_stock' ) != 'yes' )
        return;

    $date = date( "Y-m-d H:i:s", strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );

    $unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
        SELECT posts.ID
        FROM {$wpdb->posts} AS posts
        WHERE   posts.post_type   IN ('" . implode( "','", wc_get_order_types() ) . "')
        AND     posts.post_status = 'wc-pending'
        AND     posts.post_modified < %s
", $date ) );

    if ( $unpaid_orders ) {
        foreach ( $unpaid_orders as $unpaid_order ) {
            $order = wc_get_order( $unpaid_order );

            if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === get_post_meta( $unpaid_order, '_created_via', true ), $order ) ) {
                $order->update_status( 'cancelled', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
            }
        }
    }
}

Then I cannot change this code. This always use current_time( 'timestamp' ).

Then if I save a order, his date is 2016-10-25 11:20:11am but urrent_time( 'timestamp' ) says 2016-10-25 06:20:11.

Something am I doing wrong?

PD: My variables in wp_option are: gmt_offset is empty, timezone_string is America/Lima

Regards

  • Ok I found a conflict with a plugin: Payment Culqi – Alexander Oct 25 '16 at 22:41
0

It sounds like you have some code somewhere that is changing the default timezone in PHP to your timezone instead of leaving it alone as the UTC timezone.

Basically, either your PHP.INI file has American/Lima in it (when it should be left alone as UTC) or something is calling date_default_timezone_set() with America/Lima.

In such a case, the normal PHP time() and date() functions would return the time in the local timezone (America/Lima) and then WordPress would "overcorrect" it by applying the timezone change again for the second time.

To solve, you need to look for any other code that is calling date_default_timezone_set and disable it. WordPress expects the timezone in PHP to be UTC, and applies the correction itself, instead of having PHP apply it. This is for legacy reasons mostly. PHP 4 did not have good timezone handling, so a lot of existing code expects WordPress to handle it this way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.