WordPress at a glance

get_post_time() WP 2.0.0

Gets the time of the current post creation in the specified format. You can specify a return format (the default is Unix format). Must be used in WordPress loop.

The function gets a result for processing in php, and does not display it to the screen. Use the_time() to display the result.

Is the basis for: get_the_time()
Works based on: mysql2date()
Hooks from function:
Return

String/Int/false. Formatted date string or Unix timestamp if $id is 'U' or 'G'. False on failure.

Usage

get_post_time( $d, $gmt, $post, $translate );
$d(string)
Format to use for retrieving the time the post was written. Either 'G', 'U', or php date format.
Default: 'U'
$gmt(true/false)
Whether to retrieve the GMT time.
Default: false
$post(int/WP_Post)
WP_Post object or ID.
Default: global $post object
$translate(true/false)
Whether to translate the time string.
Default: false

Examples

#1 Get the timestamp of the post

To get a Unix timestamp of a post adjusted for the GMT time zone (the second parameter), you can use the function like so:

$gmt_timestamp = get_post_time( 'U', true );
echo $gmt_timestamp; //> 1521992006

Code of get_post_time: wp-includes/general-template.php VER 4.9.8

<?php
function get_post_time( $d = 'U', $gmt = false, $post = null, $translate = false ) {
	$post = get_post($post);

	if ( ! $post ) {
		return false;
	}

	if ( $gmt )
		$time = $post->post_date_gmt;
	else
		$time = $post->post_date;

	$time = mysql2date($d, $time, $translate);

	/**
	 * Filters the localized time a post was written.
	 *
	 * @since 2.6.0
	 *
	 * @param string $time The formatted time.
	 * @param string $d    Format to use for retrieving the time the post was written.
	 *                     Accepts 'G', 'U', or php date format. Default 'U'.
	 * @param bool   $gmt  Whether to retrieve the GMT time. Default false.
	 */
	return apply_filters( 'get_post_time', $time, $d, $gmt );
}

Related Functions

From tag: date time

More from category: Posts, pages...

More from Template Tags: Posts, Pages, ...

No comments
    Hello, !     Log In . Register