get_post_modified_time

Advertisement

Summery Summery

Retrieve the time at which the post was last modified.

Syntax Syntax

get_post_modified_time( string $format = 'U', bool $gmt = false, int|WP_Post $post = null, bool $translate = false )

Parameters Parameters

$format

(Optional) Format to use for retrieving the time the post was modified. Either 'G', 'U', or PHP date format. Default 'U'.

Default value: 'U'

$gmt

(Optional) Whether to retrieve the GMT time.

Default value: false

$post

(Optional) WP_Post object or ID. Default is global $post object.

Default value: null

$translate

(Optional) Whether to translate the time string.

Default value: false

Return Return

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

Source Source

File: wp-includes/general-template.php

	}

	$wp_timezone = wp_timezone();

	if ( 'gmt' === $source ) {
		$time     = ( 'modified' === $field ) ? $post->post_modified_gmt : $post->post_date_gmt;
		$timezone = new DateTimeZone( 'UTC' );
	} else {
		$time     = ( 'modified' === $field ) ? $post->post_modified : $post->post_date;
		$timezone = $wp_timezone;
	}

	if ( empty( $time ) || '0000-00-00 00:00:00' === $time ) {
		return false;
	}

	$datetime = date_create_immutable_from_format( 'Y-m-d H:i:s', $time, $timezone );

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

	return $datetime->setTimezone( $wp_timezone );
}

/**
 * Retrieve post published or modified time as a Unix timestamp.
 *
 * Note that this function returns a true Unix timestamp, not summed with timezone offset
 * like older WP functions.
 *
 * @since 5.3.0
 *
 * @param int|WP_Post $post  Optional. WP_Post object or ID. Default is global `$post` object.
 * @param string      $field Optional. Published or modified time to use from database. Accepts 'date' or 'modified'.
 *                           Default 'date'.
 * @return int|false Unix timestamp on success, false on failure.
 */
function get_post_timestamp( $post = null, $field = 'date' ) {
	$datetime = get_post_datetime( $post, $field );

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

Advertisement

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Advertisement

Leave a Reply