#25331 closed defect (bug) (invalid)
date_i18n returns erroneous result
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.6.1 |
Component: | Date/Time | Keywords: | 2nd-opinion |
Focuses: | Cc: |
Description
My WP timezone is set to London. The time (when I ran my test) was 14:57 local time (13:57 UTC).
Here's the time I'm using to test:
$unixtime = 1379339852;
Results of "/usr/bin/date --date=@1379339852" on my local machine are: "Mon Sep 16 14:57:32 BST 2013".
I used to use this method to get local times, before I knew of date_i18n():
echo get_date_from_gmt( gmdate('Y-m-d H:i:s', $unixtime), 'Y-m-d G:i');
Result (correct): 2013-09-16 14:57
When I switch to date_i18n (in the same WP install, running this code one line later than the above):
echo date_i18n('Y-m-d G:i', $unixtime);
Result (wrong): 2013-09-16 13:57
Here's a complete terminal session so that you get the full picture:
$ cat d.php <?php require('wp-load.php'); $unixtime = 1379339852; echo get_date_from_gmt( gmdate('Y-m-d H:i:s', $unixtime), 'Y-m-d G:i')."\n"; echo date_i18n('Y-m-d G:i', $unixtime)."\n"; $ php d.php 2013-09-16 14:57 2013-09-16 13:57
Change History (5)
Note: See
TracTickets for help on using
tickets.
You were right the first time around by using
get_date_from_gmt()
. Thedate_i18n()
function is used for formatting a date according to the current locale, not for converting a date to your blog's timezone.The incredibly convoluted code to output a GMT/UTC date in your blog's timezone and format it according to the current locale is:
echo date_i18n( 'Y-m-d G:i', get_date_from_gmt( date( 'Y-m-d H:i:s', $unixtime ), 'U' ) );
We definitely need a new function for this I think (in another ticket).