get_post_timestamp()
Retrieves 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.
Uses: get_post_datetime()
No Hooks.
Return
Int|false
. Unix timestamp on success, false on failure.
Usage
get_post_timestamp( $post, $field );
- $post(int|WP_Post)
- Post ID or post object.
Default: global $post object - $field(string)
- Published or modified time to use from database. Accepts 'date' or '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.7.1
function get_post_timestamp( $post = null, $field = 'date' ) { $datetime = get_post_datetime( $post, $field ); if ( false === $datetime ) { return false; } return $datetime->getTimestamp(); }