13

I've got a WordPress cron job that sends an email periodically and saves the timestamp when it was sent as an option, and I'd like to display a date on a settings page. Something like, "The last email was sent on 'x'". I'm on the west coast of the US, so our time is currently seven hours off of UTC.

My expected output from date_i18n(), passing it the timestamp, would be a locally formatted date with a seven hour adjustment from UTC. However, it returns the time in UTC. Even trying to get the current time doesn't return what I would think would be the expected output.

For example: echo date_i18n('F d, Y H:i'); outputs April 05, 2013 11:36 as expected, but echo date_i18n('F d, Y H:i',time()); outputs April 05, 2013 18:36.

Is this intentional? How can I return a locally formatted date from a preexisting time stamp? Thanks for any help.

  • Have you set your timezone in Settings->General? – vancoder Apr 5 '13 at 19:16
  • Yes, to Los Angeles. – Andrew Bartel Apr 5 '13 at 19:51
29

I know I'm three months late, but the function you want here is get_date_from_gmt().

The function accepts a GMT/UTC date in Y-m-d H:i:s format as the first parameter, and your desired date format as the second parameter. It'll convert your date to the local timezone as set on the Settings screen.

Example usage:

echo get_date_from_gmt( date( 'Y-m-d H:i:s', $my_unix_timestamp ), 'F j, Y H:i:s' );

  • 2
    Killer thanks, saw this popup in my notifications just now. I switched it to the correct answer. – Andrew Bartel Jul 22 '13 at 18:45
  • I added the date and time configuration: echo get_date_from_gmt ( date( 'Y-m-d H:i:s', $my_timestamp ), get_option('date_format') . ' - '. get_option('time_format') ); – Nabil Kadimi Jul 21 '14 at 23:31
4

From the codex:

current_time('timestamp') should be used in lieu of time() to return the blog's local time. In WordPress, PHP's time() will always return UTC and is the same as calling current_time('timestamp', true).

Try this:

define( 'MY_TIMEZONE', (get_option( 'timezone_string' ) ? get_option( 'timezone_string' ) : date_default_timezone_get() ) );
date_default_timezone_set( MY_TIMEZONE );
echo date_i18n('F d, Y H:i', 1365194723);

This sets the default PHP date to WP's timezone_string option, if available, for the duration of the script.

  • 1
    Right, but what if I have an arbitrary timestamp? Say from a couple days ago, how do I get the adjusted time rather than UTC time? – Andrew Bartel Apr 5 '13 at 19:49
  • does date_i18n('F d, Y H:i', $your_timestamp) not work? – vancoder Apr 5 '13 at 20:41
  • Nope, even tried it on a vanilla WP install with 2012 just running this echo date_i18n('F d, Y H:i',1365194723) statement at the top of index.php, that gives me UTC rather than US West Coast time. – Andrew Bartel Apr 5 '13 at 20:47
  • You're right, it looks like date_i18n is mainly for local formatting of dates, rather than date adjustment. I have updated my answer. – vancoder Apr 5 '13 at 21:01
  • Yea, I was hoping to avoid having to set the timezone manually, but if that's the only way, so be it. Marked as answered, thanks for the help. – Andrew Bartel Apr 5 '13 at 21:21
0

date_i18n($format, $timestamp) formats according to the locale, but not the timezone. get_date_from_gmt($datestring, $format) formats according to the timezone, but not the locale. To get formatting according to both the timezone and the locale, I am doing the following:

function local_date_i18n($format, $timestamp) {
    $timezone_str = get_option('timezone_string') ?: 'UTC';
    $timezone = new \DateTimeZone($timezone_str);

    // The date in the local timezone.
    $date = new \DateTime(null, $timezone);
    $date->setTimestamp($timestamp);
    $date_str = $date->format('Y-m-d H:i:s');

    // Pretend the local date is UTC to get the timestamp
    // to pass to date_i18n().
    $utc_timezone = new \DateTimeZone('UTC');
    $utc_date = new \DateTime($date_str, $utc_timezone);
    $timestamp = $utc_date->getTimestamp();

    return date_i18n($format, $timestamp, true);
}

Example program:

$format = 'F d, Y H:i';
$timestamp = 1365186960;
$local = local_date_i18n($format, $timestamp);
$gmt = date_i18n($format, $timestamp);
echo "Local: ", $local, " UTC: ", $gmt;

Output for the timezone of Los Angeles:

Local: April 05, 2013 11:36 UTC: April 05, 2013 18:36

0

Add timezone offset to your timestamp.

$offset = get_option( 'gmt_offset' ) * HOUR_IN_SECONDS;
return date_i18n( get_option( 'date_format' ), $ts + $offset );

or better;

$tz = new DateTimeZone( get_option( 'timezone_string' ) );
$offset_for_that_time = timezone_offset_get ( $tz , new DateTime("@{$ts}") );
return date_i18n ( get_option( 'date_format' ), $ts + offset_for_that_time );
-1

This is what seems to work on my machine (none of the other stuff worked):

$tz = new DateTimeZone(get_option('timezone_string'));
$dtz = new DateTimeZone('GMT');
foreach($posts as $key => $post){
    $gmt_date = DateTime::createFromFormat('Y-m-d H:i:s', $post->PostDateGMT, $dtz);
    $gmt_date->setTimeZone($tz);
    $posts[$key]->PostDateGMT = $gmt_date->format('Y-m-d H:i:s');
}

Original code: https://www.simonholywell.com/post/2013/12/convert-utc-to-local-time/

It's not using date_l18n() but I guess one could use it later-on...

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.