函数文档

get_post_modified_time()

💡 云策文档标注

概述

get_post_modified_time() 函数用于获取文章最后修改的时间,支持自定义格式、时区和本地化选项。它比 get_the_modified_time() 更底层,但提供更多参数控制。

关键要点

  • 函数返回文章最后修改的时间,可指定格式(如 'U' 表示 Unix 时间戳或 PHP 日期格式)、是否使用 GMT 时间、文章对象和是否翻译。
  • 参数包括:$format(默认 'U')、$gmt(默认 false)、$post(默认 null,使用全局 $post)和 $translate(默认 false)。
  • 返回值可以是格式化日期字符串、Unix 时间戳(当 $format 为 'U' 或 'G' 时)或失败时返回 false。
  • 内部使用 get_post_datetime() 获取 DateTimeImmutable 对象,并通过 apply_filters('get_post_modified_time', ...) 提供钩子。

代码示例

// 获取当前文章的最后修改时间戳
echo get_post_modified_time(); // 输出如 1490121048

// 获取本地时区的完整日期和时间
echo get_post_modified_time('F d, Y g:i a'); // 输出如 "March 21, 2017 12:02 pm"

// 获取 UTC 时区的完整日期和时间
echo get_post_modified_time('F d, Y g:i a', true); // 输出如 "March 21, 2017 7:02 pm"

// 获取本地化翻译的时间(例如葡萄牙语)
get_post_modified_time('j \d\e F \d\e Y', false, null, true); // 输出如 "14 de novembro de 2019"

注意事项

  • 当 $format 为 'U' 或 'G' 且 $gmt 为 false 时,函数会返回时间戳加上时区偏移量,这可能不是标准做法,需谨慎使用。
  • 函数自 WordPress 2.0.0 版本引入,相关函数包括 get_the_modified_date() 和 get_the_modified_time()。

📄 原文内容

Retrieves the time at which the post was last modified.

Parameters

$formatstringoptional
Format to use for retrieving the time the post was modified. Accepts 'G', 'U', or PHP date format. Default 'U'.
$gmtbooloptional
Whether to retrieve the GMT time.

Default:false

$postint|WP_Postoptional
Post ID or post object. Default is global $post object.

Default:null

$translatebooloptional
Whether to translate the time string.

Default:false

Return

string|int|false Formatted date string or Unix timestamp if $format is 'U' or 'G'.
False on failure.

More Information

One level »lower« than get_the_modified_time() , but can take more arguments.

Source

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 );
}

Hooks

apply_filters( ‘get_post_modified_time’, string|int $time, string $format, bool $gmt )

Filters the localized time a post was last modified.

Changelog

Version Description
2.0.0 Introduced.

User Contributed Notes