get_post_timestamp()
Gets the publication or modification time of a post as a Unix timestamp (1270995315).
This function retrieves the Unix timestamp (without the site's timezone offset).
Also read: Date/Time in WordPress.
Uses: get_post_datetime()
No Hooks.
Returns
Int|false. Unix timestamp on success, false on failure.
Usage
get_post_timestamp( $post, $field );
- $post(int|WP_Post)
- Object WP_Post or post ID.
Default: null (global $post) - $field(string)
Which database field to use as the source of the time. Accepts:
date.modified.
Default: 'date'
Examples
#1 Compare different variants of getting post time tags
$post = get_post( 31 ); $patt = "%s - %s - %s\n"; $mysql = 'Y-m-d H:i:s'; echo sprintf( $patt, strtotime( $post->post_date ), $post->post_date, 'date' ); echo sprintf( $patt, strtotime( $post->post_date_gmt ), $post->post_date_gmt, 'date_gmt' ); echo sprintf( $patt, strtotime( $post->post_modified ), $post->post_modified, 'modified' ); echo sprintf( $patt, strtotime( $post->post_modified_gmt ), $post->post_modified_gmt, 'modified_gmt' ); echo "\n\nget_post_timestamp()\n\n"; $unix_time = get_post_timestamp( $post ); $unix_modified = get_post_timestamp( $post, 'modified' ); echo sprintf( $patt, $unix_time, date( $mysql, $unix_time ), 'date' ); echo sprintf( $patt, $unix_modified, date( $mysql, $unix_modified ), 'modified' );
We get it:
1271013315 - 2010-04-11 19:15:15 - date 1270998915 - 2010-04-11 15:15:15 - date_gmt 1629940365 - 2021-08-26 01:12:45 - modified 1629922365 - 2021-08-25 20:12:45 - modified_gmt get_post_timestamp() 1270995315 - 2010-04-11 14:15:15 - date 1629922365 - 2021-08-25 20:12:45 - modified
#2 Demo
$post_id = 31; $time = get_post_timestamp( $post_id ); // int(1270995315) $time = get_post_timestamp( $post_id, 'modified' ); // int(1629922365) // it is possible to pass the object of the post: $post = get_post( $post_id ); $time = get_post_timestamp( $post ); // int(1270995315) $time = get_post_timestamp( $post, 'modified' ); // int(1629922365)
Changelog
| Since 5.3.0 | Introduced. |
get_post_timestamp() get post timestamp code WP 6.9
function get_post_timestamp( $post = null, $field = 'date' ) {
$datetime = get_post_datetime( $post, $field );
if ( false === $datetime ) {
return false;
}
return $datetime->getTimestamp();
}