首页 > 解决方案 > Wordpress How to change date format for _get_last_post_time function

问题描述

I am using the _get_last_post_time() function to show the date of a last added post.

_get_last_post_time( $timezone, array('date','modified'), $post_type);

So far this is all clear, but the output is in 'Y-m-d H:i:s' format. How do I modify this in a different date format?

Source:

https://core.trac.wordpress.org/browser/tags/5.8/src/wp-includes/post.php#L7076

标签: phpwordpressdatewordpress-themingcustom-wordpress-pages

解决方案


From the _get_last_post_time Docs
"This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions."

Use this function instead: get_lastpostdate Docs

get_lastpostdate will return a string. After that you could use strtotime function to convert it to time format. Then you could use date function to format its output any way you need to, for example:

$last_date = get_lastpostdate();

echo date("M d Y H:i:s", strtotime($last_date));

The above format outputs something like this:

Jul 05 2021 16:30:13

Another example:

echo date("H:i:s", strtotime($last_date));

The above format outputs something like this:

16:30:13

Here is the documentation for formatting date and time in wordpress:

Customizing the Time and Date Format in Wordpress Docs


推荐阅读