get_post_modified_time()WP 2.0.0

Retrieve the time at which the post was last modified.

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

Return

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

Usage

get_post_modified_time( $format, $gmt, $post, $translate );
$format(string)
Format to use for retrieving the time the post was modified. Accepts 'G', 'U', or PHP date format.
Default: 'U'
$gmt(true|false)
Whether to retrieve the GMT time.
Default: false
$post(int|WP_Post)
Post ID or post object.
Default: global $post object
$translate(true|false)
Whether to translate the time string.
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 Get the date on your own language

If you would like to get the date on your own language defined in WP, just set the parameter $translate = true:

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

For portuguese, it will return: 14 de novembro de 2019.

Changelog

Since 2.0.0 Introduced.

get_post_modified_time() code WP 6.5.2

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