get_post_modified_time()WP 2.0.0

Gets the time when the post was modified. By default, it gets the Unix timestamp, but any format can be specified.

This function is used in other functions and is foundational. It is often better to use a wrapper function instead of this one.

1 time — 0.00003 sec (very fast) | 50000 times — 0.34 sec (very fast) | PHP 7.0.8, WP 4.6.1
Hooks from the function

Returns

String|Int|false. The date in the specified format or Unix timestamp if $id is 'U' or 'G'. False on failure.

Usage

get_post_modified_time( $d, $gmt, $post, $translate );
$d(string)
The format in which to get the date or time of the post modification. Possible formats.
Default: 'U'
$gmt(boolean)
true - get GMT time, false - site time.
Default: false
$post(int/WP_Post)
ID or object of the post whose modification time needs to be retrieved.
Default: null (current post)
$translate(boolean)
Whether to translate the specified format. Strings like November, Saturday are translated.
Default: false

Examples

0

#1 Examples showing what the function outputs

echo get_post_modified_time(); //> 1472693996

echo get_post_modified_time( 'j M Y' ); //> 1 Sep 2016

echo get_post_modified_time( 'j\ M Y', 0, null, $translate = true ); //> 1 Sep 2016

echo get_post_modified_time( 'H:i:s' ); //> 01:39:56
0

#2 Date in your language

If you need to get the date in the language specified in WP, set the parameter $translate = true:

get_post_modified_time( 'j \d\e F \d\e Y', false, null, true );

For Portuguese, this will be: 14 de novembro de 2019.

Changelog

Since 2.0.0 Introduced.

get_post_modified_time() code WP 6.9.1

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