How to get WP to use EST Timezone

In my settings, I have New York defined as the time zone. But when I run plugins using PHP date() functions, I am getting a time zone of UTC as it shows in WP Admin > Settings > General.

enter image description here

enter image description here

Edit: Here’s the mu-plugin I use to add the meta tag:

<?php
/**
 * Plugin Name: Web Services Global Meta Tag(s)
 * Plugin URI: http://example.com/wp-content/mu-plugins/global-metatag-insertion.php
 * Description: This plugin fires on every page and post in the WordPress Multisite Network. It adds custom meta tags to meets our needs. One meta tag that is added is for `last-modified` date.
 * Version: 1.0
 * Author: me
 * Author URI: http://me.example.com/
 */
defined( 'ABSPATH' ) or die( 'Direct access not allowed' );

/**
 * Insert `last-modified` date meta within `<head></head>` container
 */
function last_modified_date() {
    echo '<meta http-equiv="last-modified" content="' . date( 'D, d M Y H:i:s T', strtotime( get_the_date() .' ' . get_the_time() ) ) . '" />' . "\n";
}
add_action( 'wp_head', 'last_modified_date' );

2 s
2

date() is unreliable in WP since it always resets time zone to UTC and uses its own processing for time zones.

date_i18n() is usually preferable WP replacement, but it will also localize output, which might be unwanted.

What you want for correct unlocalized output is to:

  1. Get data out of WP API without its offset silliness.
  2. Get timezone setting out of WP.
  3. Feed those into DateTime object.

Here is brief (read it basics, not covering all possibilities) introduction to code:

d( date( 'D, d M Y H:i:s T', strtotime( get_the_date() .' ' . get_the_time() ) ) );
// Sat, 07 Jan 2012 07:07:00 UTC < wrong, date() is always wrong in WP

d( date( 'D, d M Y H:i:s T', get_the_time('U') ) );
// Sat, 07 Jan 2012 07:07:21 UTC < more short, still wrong

d( date_i18n( 'D, d M Y H:i:s T', get_the_time('U') ) );
// or simply
d( get_the_time( 'D, d M Y H:i:s T' ) );
// Сб, 07 Jan 2012 07:07:21 EEST < correct! but localized

$date = DateTime::createFromFormat( DATE_W3C, get_the_date( DATE_W3C ) );
d( $date->format( 'D, d M Y H:i:s T' ) );
// Sat, 07 Jan 2012 07:07:21 GMT+0300 < correct, but meh numeric time zone

$timezone = new DateTimeZone( get_option( 'timezone_string' ) );
$date->setTimezone( $timezone );

d( $date->format( 'D, d M Y H:i:s T' ) );
// Sat, 07 Jan 2012 06:07:21 EET < correct and not localized

I wrote more on topic in my DateTime crash course for WordPress developers post.

PS EEST / EET out of sync… I don’t even know what’s up with that difference. 🙂

Leave a Comment