WordPress Date and Time: current_time, wp_date, Timezones, and UTC Handling

Dates and time zones in WordPress: current_time(), wp_date() and UTC

Dates are one of the easiest places to make mistakes in WordPress. Posts store two dates (post_date in the site’s time zone and post_date_gmt in UTC), WordPress sets PHP’s default time zone to UTC, and the site’s time zone from Settings → General is applied only by WordPress functions. Code that mixes them is off by a few hours, typically only for half of the year.

Problem: A plugin schedules a job “at midnight”, stores dates in post meta and shows them in the admin, and the times are several hours off.

Solution: Keep timestamps in UTC (time()), store them as integers, schedule WP-Cron with them, and convert to the site’s time zone only for display with wp_date(), or with DateTimeImmutable and wp_timezone() (both new in WordPress 5.3).

The current time

<?php
$utc_ts      = time();                         // a real Unix timestamp, use it for everything
$local_mysql = current_time( 'mysql' );        // '2020-01-25 14:30:00' in the site's time zone
$utc_mysql   = current_time( 'mysql', true );  // '2020-01-25 12:30:00' (site in UTC+2)

Because WordPress sets PHP to UTC, date( 'H:i' ) and new DateTime() without a time zone give UTC, not the time of the site.

Showing a date

<?php
// A timestamp in the site's time zone and language
echo wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $utc_ts );

// The date of the current post
echo get_the_date();
echo get_the_modified_date();

wp_date() replaces date_i18n(): it takes a real timestamp and applies the time zone itself, including the change to summer time.

Converting

<?php
$tz = wp_timezone(); // DateTimeZone of the site, e.g. Europe/Berlin

// UTC timestamp -> local date and time
$local = ( new DateTimeImmutable( '@' . $utc_ts ) )->setTimezone( $tz );
echo $local->format( 'Y-m-d H:i' );

// A date the user typed in local time -> UTC timestamp
$ts = ( new DateTimeImmutable( '2020-02-01 09:00', $tz ) )->getTimestamp();

Scheduling and storing

WP-Cron expects UTC timestamps. “Every day at midnight of the site” is calculated in the site’s time zone and then passed as a timestamp:

<?php
$midnight = new DateTimeImmutable( 'tomorrow midnight', wp_timezone() );

if ( ! wp_next_scheduled( 'ha_midnight_job' ) ) {
    wp_schedule_event( $midnight->getTimestamp(), 'daily', 'ha_midnight_job' );
}

// Store the timestamp in meta as an integer, format only on output
update_post_meta( $post_id, 'ha_event_start', $ts );
$start = (int) get_post_meta( $post_id, 'ha_event_start', true );
echo esc_html( wp_date( 'j F Y, H:i', $start ) );

A UTC integer compares and sorts correctly in meta_query (with 'type' => 'NUMERIC') and does not change meaning when the site’s time zone is changed later.

Note that a “daily” event repeats every 24 hours: after the change to or from summer time it runs at 23:00 or 01:00 local time.

NOTE: Avoid current_time( 'timestamp' ). It returns the UTC timestamp plus the site’s offset, a number that is not a real timestamp, and passing it to wp_date(), date() or WP-Cron applies the offset twice. Since WordPress 5.3 core discourages it; use time() and convert on output.