get_post_timestamp()

最后更新于:2021-11-26 22:41:54

get_post_timestamp( int|GC_Post$post=null, string$field=’date’)

Retrieve post published or modified time as a Unix timestamp.

参数

$post

(int|GC_Post) (Optional) GC_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’

响应

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

源文件

文件: gc-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();
}
<?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://docs.gechiui.com/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://gechiui.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
    }

}