get_post_modified_time( string $format = 'U', bool $gmt = false, int|WP_Post $post = null, bool $translate = false ): string|int|false
Retrieves the time at which the post was last modified.
Contents
Parameters
-
$format
string Optional -
Format to use for retrieving the time the post was modified. Accepts
'G'
,'U'
, or PHP date format. Default'U'
.Default:
'U'
-
$gmt
bool Optional -
Whether to retrieve the GMT time.
Default:
false
-
$post
int|WP_Post Optional -
Post ID or post object. Default is global
$post
object.Default:
null
-
$translate
bool Optional -
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
File: wp-includes/general-template.php
.
View all references
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
You must log in before being able to contribute a note or feedback.
If you would like to get the date on your own language defined in WP, just set the parameter translate as true, example:
get_post_modified_time( ‘j \d\e F \d\e Y’, false, null, true);
For me on portuguese, it will return: 14 de novembro de 2019
This example was migrated from Codex:
Get a timestamp for the current post:
This example was migrated from Codex:
Get full date and time in current timezone:
This example was migrated from Codex:
Get full date and time in UTC:
This example was migrated from Codex:
Get full date and time in UTC with localization (as per WordPress settings):