Converting timestamps to local time with date_l18n()

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.

6

I know I’m three months late, but the function you want here is WordPress’ 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' );

Leave a Comment