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.

Used By: the_time()
Hooks from the function

Return

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

Usage

get_the_time( $format, $post );
$format(string)
Format to use for retrieving the time the post was written. Accepts 'G', 'U', or PHP date format. option.
Default: 'time_format'
$post(int|WP_Post)
Post ID or post object.
Default: global $post object

Examples

0

#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 ); ?>
0

#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'); ?>
0

#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); ?>

Changelog

Since 1.5.0 Introduced.

get_the_time() code WP 6.1.1

function get_the_time( $format = '', $post = null ) {
	$post = get_post( $post );

	if ( ! $post ) {
		return false;
	}

	$_format = ! empty( $format ) ? $format : get_option( 'time_format' );

	$the_time = get_post_time( $_format, false, $post, true );

	/**
	 * Filters the time a post was written.
	 *
	 * @since 1.5.0
	 *
	 * @param string|int  $the_time Formatted date string or Unix timestamp if `$format` is 'U' or 'G'.
	 * @param string      $format   Format to use for retrieving the time the post
	 *                              was written. Accepts 'G', 'U', or PHP date format.
	 * @param WP_Post     $post     Post object.
	 */
	return apply_filters( 'get_the_time', $the_time, $format, $post );
}