Skip to content

Instantly share code, notes, and snippets.

@samkent
Last active August 13, 2021 08:38
Show Gist options
  • Save samkent/a526702f8243f8a15dc66a782dc59e71 to your computer and use it in GitHub Desktop.
Save samkent/a526702f8243f8a15dc66a782dc59e71 to your computer and use it in GitHub Desktop.
How to get the published and modified date with the WordPress function get_post_timestamp() and then convert it into a readable format
<?php
/**
* Get published and modified date with the WordPress function get_post_timestamp() and convert it into a readable format
* https://developer.wordpress.org/reference/functions/get_post_timestamp
*/
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
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment