Make WordPress Core

Opened 12 years ago

Closed 12 years ago

Last modified 11 years ago

#25331 closed defect (bug) (invalid)

date_i18n returns erroneous result

Reported by: davidanderson's profile DavidAnderson 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)

#1 @knutsp
12 years ago

  • Cc knut@… added

#2 @SergeyBiryukov
12 years ago

  • Component changed from General to Date/Time

#3 @johnbillion
12 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

You were right the first time around by using get_date_from_gmt(). The date_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).

#4 @johnbillion
12 years ago

#25555 was marked as a duplicate.

#5 @codix
11 years ago

  • Keywords 2nd-opinion added

Unix timestamps are always in GMT. date_i18n should translate to local timezone or change the variable names and documentation to be just $timestamp.

Note: See TracTickets for help on using tickets.