get_post_time

函数
get_post_time ( $format = 'U', $gmt = false, $post = null, $translate = false )
参数
  • (string) $format Optional. Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format. Default 'U'.
    Required:
    Default: 'U'
  • (bool) $gmt Optional. Whether to retrieve the GMT time. Default false.
    Required:
    Default: false
  • (int|WP_Post) $post Post ID or post object. Default is global `$post` object.
    Required:
    Default: null
  • (bool) $translate Whether to translate the time string. Default false.
    Required:
    Default: false
返回值
  • (string|int|false) Formatted date string or Unix timestamp if `$format` is 'U' or 'G'. False on failure.
定义位置
相关方法
get_post_datetimeget_post_typeget_post_metaget_post_timestamp_get_last_post_time
引入
2.0.0
弃用
-

get_post_time: 这个函数以指定的格式检索一个文章的时间。它接受一个格式字符串和一个文章ID或文章对象作为其参数,并返回指定格式的文章时间。

检索文章的写作时间。

function get_post_time( $format = 'U', $gmt = false, $post = null, $translate = false ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$source   = ( $gmt ) ? 'gmt' : 'local';
	$datetime = get_post_datetime( $post, 'date', $source );

	if ( false === $datetime ) {
		return false;
	}

	if ( 'U' === $format || 'G' === $format ) {
		$time = $datetime->getTimestamp();

		// Returns a sum of timestamp with timezone offset. Ideally should never be used.
		if ( ! $gmt ) {
			$time += $datetime->getOffset();
		}
	} elseif ( $translate ) {
		$time = wp_date( $format, $datetime->getTimestamp(), $gmt ? new DateTimeZone( 'UTC' ) : null );
	} else {
		if ( $gmt ) {
			$datetime = $datetime->setTimezone( new DateTimeZone( 'UTC' ) );
		}

		$time = $datetime->format( $format );
	}

	/**
	 * Filters the localized time a post was written.
	 *
	 * @since 2.6.0
	 *
	 * @param string|int $time   Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
	 * @param string     $format Format to use for retrieving the time the post was written.
	 *                           Accepts 'G', 'U', or PHP date format.
	 * @param bool       $gmt    Whether to retrieve the GMT time.
	 */
	return apply_filters( 'get_post_time', $time, $format, $gmt );
}

常见问题

FAQs
查看更多 >