Changeset 46154 for trunk/src/wp-includes/general-template.php
- Timestamp:
- 09/17/2019 11:13:25 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/general-template.php
r46089 r46154 2516 2516 * was written. Either 'G', 'U', or php date format defaults 2517 2517 * 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 $postobject.2518 * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object. 2519 2519 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure. 2520 2520 */ … … 2554 2554 * was written. Either 'G', 'U', or php date format. Default 'U'. 2555 2555 * @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 $postobject.2556 * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object. 2557 2557 * @param bool $translate Whether to translate the time string. Default false. 2558 2558 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure. … … 2565 2565 } 2566 2566 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 ); 2569 2582 } 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 } 2574 2589 2575 2590 /** … … 2584 2599 */ 2585 2600 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 */ 2614 function 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 */ 2642 function 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(); 2586 2650 } 2587 2651 … … 2654 2718 * was modified. Either 'G', 'U', or php date format. Default 'U'. 2655 2719 * @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 $postobject.2720 * @param int|WP_Post $post WP_Post object or ID. Default is global `$post` object. 2657 2721 * @param bool $translate Whether to translate the time string. Default false. 2658 2722 * @return string|int|false Formatted date string or Unix timestamp if `$d` is 'U' or 'G'. False on failure. … … 2665 2729 } 2666 2730 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 ); 2669 2746 } 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 } 2674 2753 2675 2754 /**
Note: See TracChangeset
for help on using the changeset viewer.