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.
Built from https://develop.svn.wordpress.org/trunk@45883


git-svn-id: http://core.svn.wordpress.org/trunk@45694 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2019-08-23 01:12:55 +00:00
parent 6f8e50c7f0
commit 6e555f0c77
3 changed files with 13 additions and 2 deletions

View File

@ -860,7 +860,7 @@ function wp_dashboard_recent_posts( $args ) {
echo '<ul>'; echo '<ul>';
$today = current_time( 'Y-m-d' ); $today = current_time( 'Y-m-d' );
$tomorrow = gmdate( 'Y-m-d', strtotime( '+1 day', current_time( 'timestamp' ) ) ); $tomorrow = current_datetime()->modify( '+1 day' )->format( 'Y-m-d' );
$year = current_time( 'Y' ); $year = current_time( 'Y' );
while ( $posts->have_posts() ) { while ( $posts->have_posts() ) {

View File

@ -79,6 +79,17 @@ function current_time( $type, $gmt = 0 ) {
return $datetime->format( $type ); return $datetime->format( $type );
} }
/**
* Retrieves the current time as an object with the timezone from settings.
*
* @since 5.3.0
*
* @return DateTimeImmutable Date and time object.
*/
function current_datetime() {
return new DateTimeImmutable( 'now', wp_timezone() );
}
/** /**
* Retrieves the timezone from current settings as a string. * Retrieves the timezone from current settings as a string.
* *

View File

@ -13,7 +13,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.3-alpha-45882'; $wp_version = '5.3-alpha-45883';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.