Make WordPress Core

Opened 8 years ago

Closed 6 years ago

Last modified 5 years ago

#37440 closed defect (bug) (fixed)

current_time function outputs local time when it should output GMT time

Reported by: bynicolas's profile bynicolas Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.1 Priority: normal
Severity: normal Version: 4.5.3
Component: Date/Time Keywords: has-patch has-unit-tests
Focuses: Cc:


on line 68 when $gmt is true, the function returns local time by using date( $type ) when it should be using gmdate( $type )

Attachments (2)

37440.patch (596 bytes) - added by rahulsprajapati 8 years ago.
changed date function to gmdate as gmt is true.
trac-37440.diff (1.6 KB) - added by Rarst 6 years ago.
Change to gmdate() in implementation with a unit test.

Download all attachments as: .zip

Change History (13)

8 years ago

changed date function to gmdate as gmt is true.

#1 @rahulsprajapati
8 years ago

  • Keywords has-patch added

#2 follow-up: @SergeyBiryukov
8 years ago

Hi @bynicolas, welcome to Trac!

Line 68 looks correct to me as is. WordPress works with dates in UTC internally, see wp-settings.php.

#3 in reply to: ↑ 2 @bynicolas
8 years ago

Hi @SergeyBiryukov, you are right, I didn't see that setting for the UTC timezone.

But wouldn't it be better to still use gmdate(). If for any reason the default timezone was changed by a user, then current_time( 'format', true) wouldn't return the GMT time (the expected value) since date() only formats a local time and would then be affected by date_default_timezone_set() change.

#5 @Rarst
6 years ago

I agree with a switch to gmdate(), since implied UTC context can be affected by plugins and is unreliable.

However all of date() use needs to be switched, and the function is very very broken as it is. We need to be moving towards deprecating it altogether, but I will look into if functionality can be fixed up a bit.

#6 @Rarst
6 years ago

#38940 was marked as a duplicate.

6 years ago

Change to gmdate() in implementation with a unit test.

#7 @Rarst
6 years ago

  • Keywords has-unit-tests added

Added a unit test.

The function is still thoroughly broken, but one step (and unit test) at a time. :)

#8 @SergeyBiryukov
6 years ago

  • Milestone changed from Awaiting Review to 5.0

#9 @SergeyBiryukov
6 years ago

  • Owner set to SergeyBiryukov
  • Resolution set to fixed
  • Status changed from new to closed

In 43594:

Date/Time: Make sure current_time() always calculates the offset from GMT/UTC.

Previously, UTC context was implied, but could be unreliable due to being affected by plugins.

Props Rarst, rahulsprajapati, marco.marsala.
Fixes #37440.

#10 @johnbillion
6 years ago

  • Milestone changed from 5.0 to 5.1
Note: See TracTickets for help on using tickets.