The get_post_timestamp() WordPress PHP function retrieves the post’s published or modified time as a Unix timestamp.
Usage
To use the get_post_timestamp() function, provide the post ID or post object and the type of time (published or modified).
$unix_timestamp = get_post_timestamp($post, 'date');
Parameters
- $post (int|WP_Post) Optional: Post ID or post object. Default is the global
$post
object. Default: null - $field (string) Optional: Published or modified time to use from the database. Accepts ‘date’ or ‘modified’. Default ‘date’. Default: ‘date’
More information
See WordPress Developer Resources: get_post_timestamp()
Examples
Displaying Published and Modified Dates
This code snippet retrieves and displays the published and modified dates for a post in a human-readable format.
function display_published_modified_dates() { // Get UNIX timestamps $unix_published_date = get_post_timestamp('', 'date'); $unix_modified_date = get_post_timestamp('', 'modified'); // Convert to readable dates $published_date = date_i18n(get_option('date_format'), $unix_published_date); $modified_date = date_i18n(get_option('date_format'), $unix_modified_date); // Display dates echo 'Published: ' . $published_date; echo 'Modified: ' . $modified_date; }
Displaying Post Age
This code snippet calculates and displays the age of a post in days.
function display_post_age() { // Get published UNIX timestamp $unix_published_date = get_post_timestamp('', 'date'); // Calculate post age in days $post_age = floor((time() - $unix_published_date) / 86400); // Display post age echo 'Post Age: ' . $post_age . ' days'; }
Checking if a Post was Modified
This code snippet checks if a post has been modified after publishing.
function is_post_modified() { // Get UNIX timestamps $unix_published_date = get_post_timestamp('', 'date'); $unix_modified_date = get_post_timestamp('', 'modified'); // Check if modified if ($unix_modified_date > $unix_published_date) { echo 'This post has been modified.'; } else { echo 'This post has not been modified.'; } }
Displaying Time Since Last Modification
This code snippet calculates and displays the time since a post was last modified in hours and minutes.
function display_time_since_modification() { // Get modified UNIX timestamp $unix_modified_date = get_post_timestamp('', 'modified'); // Calculate time since modification $time_since_modification = time() - $unix_modified_date; $hours = floor($time_since_modification / 3600); $minutes = floor(($time_since_modification % 3600) / 60); // Display time since modification echo 'Time Since Modification: ' . $hours . ' hours, ' . $minutes . ' minutes'; }