get_post_timestamp( int|WP_Post $post = null, string $field = 'date' )

Retrieve post published or modified time as a Unix timestamp.


Description Description

Note that this function returns a true Unix timestamp, not summed with timezone offset like older WP functions.


Top ↑

Parameters Parameters

$post

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

Default value: null

$field

(string) (Optional) Published or modified time to use from database. Accepts 'date' or 'modified'.

Default value: 'date'


Top ↑

Return Return

(int|false) Unix timestamp on success, false on failure.


Top ↑

Source Source

File: wp-includes/general-template.php

function get_post_timestamp( $post = null, $field = 'date' ) {
	$datetime = get_post_datetime( $post, $field );

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

	return $datetime->getTimestamp();
}


Top ↑

Changelog Changelog

Changelog
Version Description
5.3.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Sam Kent

    How to get the published and modified date with get_post_timestamp() and then convert it into a readable format.

    <?php
    function published_modified_date() {
    
    	// UNIX published date
    	$unix_published_date = get_post_timestamp( '', 'date' );
    
    	// UNIX modified date
    	$unix_modified_date = get_post_timestamp( '', 'modified' );
    	
        // Convert from UNIX timestamp into readable date
        // Reference: https://developer.wordpress.org/reference/functions/date_i18n
    	$published_date = date_i18n( get_option( 'date_format' ), $unix_published_date );
    	$modified_date = date_i18n( get_option( 'date_format' ), $unix_modified_date );
    	
        // Convert from UNIX timestamp into full date/time (ISO)
        // Reference: https://wordpress.org/support/article/formatting-date-and-time
    	$full_published_date = date_i18n( 'c', $unix_published_date );
    	$full_modified_date = date_i18n( 'c', $unix_modified_date );
    	
        ?>
    
    	<span class="published"><time datetime="<?php echo $full_published_date; ?>"><?php echo $published_date; ?></time></span>
    	
        <?php
        // If modified date is greater than published date by 1 day 
        if ( $unix_modified_date > $unix_published_date + 86400 ) { ?>
    		<span class="modified">Modified on: <time datetime="<?php echo $full_modified_date; ?>"><?php echo $modified_date; ?></time></span>
    	    <?php
        }
    
    }

You must log in before being able to contribute a note or feedback.