Using WordPress ‘get_the_time()’ PHP function

The get_the_time() WordPress PHP function retrieves the time at which the post was written.

Usage

echo get_the_time($format, $post);

Parameters

  • $format (string) Optional: Format to use for retrieving the time the post was written. Accepts ‘G’, ‘U’, or PHP date format. Defaults to the ‘time_format’ option. Default: ''
  • $post (int|WP_Post) Optional: Post ID or post object. Default is global $post object. Default: null

More information

See WordPress Developer Resources: get_the_time()

Examples

Basic usage

Retrieve the time of the current post using the WordPress default format and display it using the PHP echo command.

echo get_the_time();

Retrieve time for a specific post

Retrieve the time of the post with ID $post->ID in the WordPress default format.

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

Getting Unix Timestamp

Assign the local time of the current post in seconds (since January 1, 1970, known as the Unix Epoch) to the variable $local_timestamp.

$local_timestamp = get_the_time('U');

Getting GMT Unix Timestamp

Get the epoch time for GMT (rather than for the local time zone) using the get_post_time() function, setting the $gmt option to true.

$gmt_timestamp = get_post_time('U', true);

Custom date format

Retrieve the time of the current post using a custom PHP date format.

echo get_the_time('F j, Y, g:i a');