Using WordPress ‘get_post_time()’ PHP function

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

Usage

get_post_time( string $format = 'U', bool $gmt = false, int|WP_Post $post = null, bool $translate = false );

Parameters

  • $format (string) Optional: Format to use for retrieving the time the post was written. Accepts ‘G’, ‘U’, or PHP date format. Default ‘U’.
  • $gmt (bool) Optional: Whether to retrieve the GMT time. Default false.
  • $post (int|WP_Post) Optional: Post ID or post object. Default is global $post object.
  • $translate (bool) Optional: Whether to translate the time string. Default false.

More information

See WordPress Developer Resources: get_post_time()

Examples

Get post time in default Unix timestamp format

This example retrieves the post time in Unix timestamp format.

$post_time = get_post_time();
echo $post_time; // Output: Unix timestamp, e.g. 1629203421

Get post time in ‘Y-m-d H:i:s’ format

This example retrieves the post time in ‘Y-m-d H:i:s’ format.

$post_time = get_post_time('Y-m-d H:i:s');
echo $post_time; // Output: 2021-08-17 16:37:01

Get post time in GMT

This example retrieves the post time in GMT.

$post_time = get_post_time('Y-m-d H:i:s', true);
echo $post_time; // Output: 2021-08-17 10:37:01 (GMT time)

Get post time for a specific post

This example retrieves the post time for a specific post with ID 42.

$post_time = get_post_time('Y-m-d H:i:s', false, 42);
echo $post_time; // Output: 2021-08-17 16:37:01 (post time for post ID 42)

Get translated post time

This example retrieves the translated post time.

$post_time = get_post_time('F j, Y, g:i a', false, null, true);
echo $post_time; // Output: August 17, 2021, 4:37 pm (translated time)