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.

Works based on: get_post_time()
Basis of: the_time()
Hooks from the function

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


get_the_time( $format, $post );
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: ''
WP_Post object or ID.
Default: global $post object


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


Since 1.5.0 Introduced.

Code of get_the_time() WP 5.5.3

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      $the_time The formatted time.
	 * @param string      $format   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, $format, $post );

From tag: date time

More from category: Posts, pages...

vladlu 100vlad.lu
Editors: Kama 100
No comments
    Log In