WordPress at a glance

get_the_time() WP 1.5.0

Gets the time at which the current post was written in the specified format. Used inside the Loop of WordPress.

Is the basis for: the_time()
Works based on: get_post_time()
Hooks from function:
Return

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

Usage

get_the_time( $d, $post );
$d(string)
Format to use for retrieving the time the post was written. Either 'G', 'U', or php date format defaults to the value specified in the time_format option.
Default: ''
$post(int/WP_Post)
WP_Post object or ID.
Default: global $post object

Examples

#1 Basic usage

Output the time of publication of the current post in the loop. The date format will be as set in the WP settings:

<?php $local_timestamp = get_the_time('U'); ?>

Do the same, just indicate the desired post:

<?php echo get_the_time('', $post->ID ); ?>

#2 Getting the time stamp in Unix format

To get the Unix timestamp of the post written date (the number of seconds passed from January 1, 1970 to the written date), the first parameter must be set to "U".

<?php $local_timestamp = get_the_time('U'); ?>

#3 The time in the GMT time zone

Sometimes you want to get the time in the GMT (UTC) time zone, not the current date of publication (written). To do this, specify the second parameter:

<?php $gmt_timestamp = get_post_time('U', true); ?>

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

<?php
function get_the_time( $d = '', $post = null ) {
	$post = get_post($post);

	if ( ! $post ) {
		return false;
	}

	if ( '' == $d )
		$the_time = get_post_time(get_option('time_format'), false, $post, true);
	else
		$the_time = get_post_time($d, false, $post, true);

	/**
	 * Filters the time a post was written.
	 *
	 * @since 1.5.0
	 *
	 * @param string      $the_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 value specified
	 *                              in 'time_format' option. Default empty.
	 * @param int|WP_Post $post     WP_Post object or ID.
	 */
	return apply_filters( 'get_the_time', $the_time, $d, $post );
}

Related Functions

From tag: date time

More from category: Posts, pages...

No comments
    Hello, !     Log In . Register