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.
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' );
From the codex:
Try this:
This sets the default PHP date to WP’s timezone_string option, if available, for the duration of the script.
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:Example program:
Output for the timezone of Los Angeles:
References:
DateTimeZone
docs in php.netDateTime
docs in php.net, includingsetTimestamp
,getTimestamp
andformat
date_i18n
docsConverting UTC to string in timezone, language and format in WordPress’ options
I’ve created a well-document function that converts a date-time string in UTC to a pretty date-time string in the correct language, format and timezone. Feel free to copy it.
For example, passing in
"2019-05-30 18:06:01"
(in UTC) would return"Maggio 30, 2019 10:06 am"
.References:
get_date_from_gmt
docsDateTimeZone
docs in php.netDateTime
docs in php.net, includinggetTimestamp
date_i18n
docsdate_default_timezone_set( 'UTC' );
regardless of what timezone the administrator selected in the user interface, so bear that in mind when determining the behaviour of built-in PHP functions. Seedate_default_timezone_set
docs in php.net.Add timezone offset to your timestamp.
or better;
This is what seems to work on my machine (none of the other stuff worked):
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…Timestamps do not have a timezone, so you almost never need to operate on them, mathematically.
Since WordPress v5.3, the easiest solution for specific timestamps is
wp_date()
:For the current date & time, use the aptly named
current_datetime
: