How to get WP to use EST Timezone

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

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)