Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am having a very peculiar issue on a customer's server (vps). The code below works just fine on my servers. Basically, a specific unix timestamp is formatted correctly and as soon as wp_insert_post() is called, the dates adjust to GMT. For example:

echo "date_i18n of 1366495200: " . date_i18n('Y-m-d H:i', 1366495200 ). '<br />';
echo "date of 1366495200: " . date('Y-m-d H:i', 1366495200 ). '<br />';
// Both output >>> 2013-04-21 00:00 , correct based on server settings

 $_post = array(
            'post_type' => 'my_cpt',
            'post_title' => 'my title',
            'post_content' => '',
            'post_status' => 'publish'
        );

$new_post_ID = wp_insert_post( $_post );

echo "date_i18n of 1366495200: " . date_i18n('Y-m-d H:i', 1366495200 ). '<br />';
echo "date of 1366495200: " . date('Y-m-d H:i', 1366495200 ). '<br />';
// new output >>> 2013-04-20 22:00 <<<

Does anyone have any insight?

share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

If you do not pass a date to wp_insert_post(), get_gmt_from_date() is called. And look at that function’s content:

function get_gmt_from_date($string, $format = 'Y-m-d H:i:s') {
    preg_match('#([0-9]{1,4})-([0-9]{1,2})-([0-9]{1,2}) ([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})#', $string, $matches);
    if ( ! $matches )
        return date( $format, 0 );

    $tz = get_option('timezone_string');
    if ( $tz ) {
        date_default_timezone_set( $tz );
        $datetime = date_create( $string );
        if ( ! $datetime )
            return date( $format, 0 );

        $datetime->setTimezone( new DateTimeZone('UTC') );
        $offset = $datetime->getOffset();
        $datetime->modify( '+' . $offset / HOUR_IN_SECONDS . ' hours');
        $string_gmt = gmdate($format, $datetime->format('U'));

        date_default_timezone_set('UTC');
    } else {
        $string_time = gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
        $string_gmt = gmdate($format, $string_time - get_option('gmt_offset') * HOUR_IN_SECONDS);
    }
    return $string_gmt;
}

It changes the time zone according to the setting in the option timezone_string. So go to wp-admin/options-general.php and set the proper time zone. Or pass a valid date per parameter post_date.

share|improve this answer
2  
WordPress standardizes the PHP date to UTC, and handles timezone conversion itself. The real question is why is the timezone not UTC before calling the function. –  Otto Jan 11 '13 at 7:43
1  
Passing a date to post_date AND post_date_gmt worked in this case. Some more details if it will help: WP timezone is New York and PHP date.timezone is set to US/Central. Much obliged! –  pistolshrimp Jan 11 '13 at 7:55
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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