Using get_post_time to get the post publication time in WordPress

The WordPress get_post_time function is a built-in feature that retrieves the time at which a particular post or page was published. The time is obtained from the database where the post’s information is stored. This function can be used to display the publication time of posts or pages on a WordPress website.

This function can be useful in various scenarios. For instance, if there is a need to display the time of publication on the front-end of a website for the users to see, this function can be used. It is also useful in cases where the time of publication needs to be compared with another date or time, for sorting or filtering purposes.

Parameters of the WordPress get_post_time Function

The get_post_time function in WordPress accepts four parameters, all of which are optional:

  • $format (string): This parameter is used to define the format for retrieving the time the post was written. It can accept ‘G’, ‘U’, or any PHP date format. If not specified, the default value ‘U’ will be used.
  • $gmt (bool): This parameter is used to specify whether to retrieve the GMT time. Its default value is false.
  • $post (int|WP_Post): This parameter can be either a post ID or a post object. If not specified, the global $post object will be used by default.
  • $translate (bool): This parameter is used to specify whether to translate the time string. Its default value is false.

Return Value of the get_post_time Function

The get_post_time function returns a formatted date string or Unix timestamp if the $format parameter is ‘U’ or ‘G’. In case of failure, the function returns false.

Examples

How to Display the Time of a Post’s Publication

$post_time = get_post_time('g:i a', false, $post_id, true); 
echo '<p>Post Time: ' . $post_time . '</p>';

This code snippet displays the time when the post was published. The get_post_time function retrieves the time of the post’s publication in the ‘g:i a’ format (hours:minutes am/pm). The second parameter is set to false, meaning it will not use GMT time. The third parameter is the ID of the post. The last parameter is set to true to adjust the time output based on WordPress’s timezone setting.

How to Display the Date and Time of a Post’s Publication

$post_date_time = get_post_time('F j, Y g:i a', false, $post_id, true); 
echo '<p>Post Date and Time: ' . $post_date_time . '</p>';

This code snippet displays the date and time when the post was published. The get_post_time function retrieves the date and time of the post’s publication in the ‘F j, Y g:i a’ format (Month Day, Year hours:minutes am/pm). The second parameter is set to false, meaning it will not use GMT time. The third parameter is the ID of the post. The last parameter is set to true to adjust the time output based on WordPress’s timezone setting.

How to Check if a Post was Published Today

$post_time = get_post_time('Ymd', false, $post_id, true); 
$current_time = current_time('Ymd');
if ($post_time == $current_time) {
 echo '<p>This post was published today.</p>';
}

This code snippet checks if a post was published today. The get_post_time function retrieves the date of the post’s publication in the ‘Ymd’ format (YearMonthDay). The second parameter is set to false, meaning it will not use GMT time. The third parameter is the ID of the post. The last parameter is set to true to adjust the time output based on WordPress’s timezone setting. It then compares the post’s date with the current date. If they are equal, it means the post was published today.

Conclusion

The get_post_time function in WordPress is a feature that allows you to retrieve the time at which a specific post or page was written. This function can be utilized in a variety of ways, such as displaying the publication time of posts, or for sorting and organizing posts based on their creation time. Therefore, understanding and properly implementing the get_post_time function can contribute significantly to the management and organization of content within a WordPress site.

Related WordPress Functions