Make WordPress Core

Opened 4 years ago

Closed 19 months ago

Last modified 13 months ago

#37440 closed defect (bug) (fixed)

current_time function outputs local time when it should output GMT time

Reported by: bynicolas Owned by: 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 4 years ago.
changed date function to gmdate as gmt is true.
trac-37440.diff (1.6 KB) - added by Rarst 21 months ago.
Change to gmdate() in implementation with a unit test.

Download all attachments as: .zip

Change History (13)

4 years ago

changed date function to gmdate as gmt is true.

#1 @rahulsprajapati
4 years ago

  • Keywords has-patch added

#2 follow-up: @SergeyBiryukov
4 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
4 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
21 months 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
21 months ago

#38940 was marked as a duplicate.

21 months ago

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

#7 @Rarst
21 months 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
19 months ago

  • Milestone changed from Awaiting Review to 5.0

#9 @SergeyBiryukov
19 months 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
18 months ago

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