Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#47464 closed enhancement (fixed)

Introduce current_datetime() for better time operations

Reported by: rarst's profile Rarst Owned by: sergeybiryukov's profile SergeyBiryukov
Milestone: 5.3 Priority: normal
Severity: minor Version:
Component: Date/Time Keywords: has-patch
Focuses: Cc:


WordPress uses current_time() function to refer to the current moment of date and time. Timestamps issues in it aside, it's not friendly to time operations, leading to convoluted and fragile code when time needs to be shifted from current, for example:

$tomorrow = gmdate( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) );

The patch introduces current_datetime() analogue that returns proper DateTimeImmutable representation of current moment in time, open to a more flexible and reliable use:

tomorrow = current_datetime()->modify('+1 day')->format('Y-m-d');

Patch needs wp_timezone() merged, see #24730

Parent issue #40657

Attachments (1)

current-datetime.patch (1.3 KB) - added by Rarst 5 years ago.

Download all attachments as: .zip

Change History (3)

#1 @SergeyBiryukov
5 years ago

  • Milestone changed from Awaiting Review to 5.3
  • Owner set to SergeyBiryukov
  • Status changed from new to reviewing

#2 @SergeyBiryukov
5 years ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 45883:

Date/Time: Introduce current_datetime() for better time operations.

Returning a DateTimeImmutable representation of the current moment in time, this allows for a more flexible and reliable use than current_time() provides.

Props Rarst.
Fixes #47464.

Note: See TracTickets for help on using tickets.