get_post_modified_time

函式
get_post_modified_time ( $format = 'U', $gmt = false, $post = null, $translate = false )
引數
  • (string) $format Optional. Format to use for retrieving the time the post was modified. 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_the_modified_timeget_the_modified_datethe_modified_timeget_post_datetimeget_lastpostmodified
引入
2.0.0
棄用
-

get_post_modified_time函式用於檢索文章上次修改的日期和時間。它有兩個引數:第一個引數是要使用的格式,第二個引數是文章的ID。format引數是可選的,如果未指定,函式將以預設的WordPress格式返回日期和時間。

檢索文章最後被修改的時間。

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
function get_post_modified_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, 'modified', $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 last modified.
*
* @since 2.8.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 modified.
* Accepts 'G', 'U', or PHP date format. Default 'U'.
* @param bool $gmt Whether to retrieve the GMT time. Default false.
*/
return apply_filters( 'get_post_modified_time', $time, $format, $gmt );
}
function get_post_modified_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, 'modified', $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 last modified. * * @since 2.8.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 modified. * Accepts 'G', 'U', or PHP date format. Default 'U'. * @param bool $gmt Whether to retrieve the GMT time. Default false. */ return apply_filters( 'get_post_modified_time', $time, $format, $gmt ); }
function get_post_modified_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, 'modified', $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 last modified.
	 *
	 * @since 2.8.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 modified.
	 *                           Accepts 'G', 'U', or PHP date format. Default 'U'.
	 * @param bool       $gmt    Whether to retrieve the GMT time. Default false.
	 */
	return apply_filters( 'get_post_modified_time', $time, $format, $gmt );
}

常見問題

FAQs
檢視更多 >