WordPress.org

Make WordPress Core

Opened 4 months ago

Closed 4 weeks ago

#47464 closed enhancement (fixed)

Introduce current_datetime() for better time operations

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

Description

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:

<?php
$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:

<?php
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 4 months ago.

Download all attachments as: .zip

Change History (3)

#1 @SergeyBiryukov
4 months ago

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

#2 @SergeyBiryukov
4 weeks 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.