Make WordPress Core


Ignore:
Timestamp:
09/17/2019 11:13:25 AM (5 years ago)
Author:
SergeyBiryukov
Message:

Date/Time: Introduce get_post_datetime() to retrieve post published or modified time as a DateTimeImmutable object instance.

Introduce get_post_timestamp() to retrieve post published or modified time as a Unix timestamp.

Use get_post_datetime() in get_post_time() and get_post_modified_time() to return correct GMT time if default timezone is changed from UTC.

Props Rarst, johnregan3.
Fixes #25002.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r46089 r46154  
    25162516 *                          was written. Either 'G', 'U', or php date format defaults
    25172517 *                          to the value specified in the time_format option. Default empty.
    2518  * @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
     2518 * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object.
    25192519 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
    25202520 */
     
    25542554 *                               was written. Either 'G', 'U', or php date format. Default 'U'.
    25552555 * @param bool        $gmt       Optional. Whether to retrieve the GMT time. Default false.
    2556  * @param int|WP_Post $post      WP_Post object or ID. Default is global $post object.
     2556 * @param int|WP_Post $post      WP_Post object or ID. Default is global `$post` object.
    25572557 * @param bool        $translate Whether to translate the time string. Default false.
    25582558 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
     
    25652565    }
    25662566
    2567     if ( $gmt ) {
    2568         $time = $post->post_date_gmt;
     2567    $datetime = get_post_datetime( $post );
     2568
     2569    if ( false === $datetime ) {
     2570        return false;
     2571    }
     2572
     2573    if ( 'U' === $d || 'G' === $d ) {
     2574        $time = $datetime->getTimestamp();
     2575
     2576        // Returns a sum of timestamp with timezone offset. Ideally should never be used.
     2577        if ( ! $gmt ) {
     2578            $time += $datetime->getOffset();
     2579        }
     2580    } elseif ( $translate ) {
     2581        $time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
    25692582    } else {
    2570         $time = $post->post_date;
    2571     }
    2572 
    2573     $time = mysql2date( $d, $time, $translate );
     2583        if ( $gmt ) {
     2584            $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
     2585        }
     2586
     2587        $time = $datetime->format( $d );
     2588    }
    25742589
    25752590    /**
     
    25842599     */
    25852600    return apply_filters( 'get_post_time', $time, $d, $gmt );
     2601}
     2602
     2603/**
     2604 * Retrieve post published or modified time as a `DateTimeImmutable` object instance.
     2605 *
     2606 * The object will be set to the timezone from WordPress settings.
     2607 *
     2608 * @since 5.3.0
     2609 *
     2610 * @param int|WP_Post $post  Optional. WP_Post object or ID. Default is global `$post` object.
     2611 * @param string      $field Optional. Post field to use. Accepts 'date' or 'modified'.
     2612 * @return DateTimeImmutable|false Time object on success, false on failure.
     2613 */
     2614function get_post_datetime( $post = null, $field = 'date' ) {
     2615    $post = get_post( $post );
     2616
     2617    if ( ! $post ) {
     2618        return false;
     2619    }
     2620
     2621    $time = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
     2622
     2623    if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
     2624        return false;
     2625    }
     2626
     2627    return date_create_immutable_from_format( 'Y-m-d H:i:s', $time, wp_timezone() );
     2628}
     2629
     2630/**
     2631 * Retrieve post published or modified time as a Unix timestamp.
     2632 *
     2633 * Note that this function returns a true Unix timestamp, not summed with timezone offset
     2634 * like older WP functions.
     2635 *
     2636 * @since 5.3.0
     2637 *
     2638 * @param int|WP_Post $post  Optional. WP_Post object or ID. Default is global `$post` object.
     2639 * @param string      $field Optional. Post field to use. Accepts 'date' or 'modified'.
     2640 * @return int|false Unix timestamp on success, false on failure.
     2641 */
     2642function get_post_timestamp( $post = null, $field = 'date' ) {
     2643    $datetime = get_post_datetime( $post, $field );
     2644
     2645    if ( false === $datetime ) {
     2646        return false;
     2647    }
     2648
     2649    return $datetime->getTimestamp();
    25862650}
    25872651
     
    26542718 *                               was modified. Either 'G', 'U', or php date format. Default 'U'.
    26552719 * @param bool        $gmt       Optional. Whether to retrieve the GMT time. Default false.
    2656  * @param int|WP_Post $post      WP_Post object or ID. Default is global $post object.
     2720 * @param int|WP_Post $post      WP_Post object or ID. Default is global `$post` object.
    26572721 * @param bool        $translate Whether to translate the time string. Default false.
    26582722 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure.
     
    26652729    }
    26662730
    2667     if ( $gmt ) {
    2668         $time = $post->post_modified_gmt;
     2731    $datetime = get_post_datetime( $post, 'modified' );
     2732
     2733    if ( false === $datetime ) {
     2734        return false;
     2735    }
     2736
     2737    if ( 'U' === $d || 'G' === $d ) {
     2738        $time = $datetime->getTimestamp();
     2739
     2740        // Returns a sum of timestamp with timezone offset. Ideally should never be used.
     2741        if ( ! $gmt ) {
     2742            $time += $datetime->getOffset();
     2743        }
     2744    } elseif ( $translate ) {
     2745        $time = wp_date( $d, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
    26692746    } else {
    2670         $time = $post->post_modified;
    2671     }
    2672 
    2673     $time = mysql2date( $d, $time, $translate );
     2747        if ( $gmt ) {
     2748            $datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
     2749        }
     2750
     2751        $time = $datetime->format( $d );
     2752    }
    26742753
    26752754    /**
Note: See TracChangeset for help on using the changeset viewer.