How does WordPress display the last update date?Recall the latest datetime code

WordPressThe website needs to consider the issue of cross time zone and time zone consistency, we can use the php time function DATE_W3C to achieve.

How does WordPress display the last update date?

There are two ways to display the last update time of the article, as follows:

  1. Displayed as "datetime" (eg May 2022, 5 15:11AM)
  2. Use "before time" form instead of date display (eg 50 minutes ago)

The date form of the article calling "datetime"

Generally, the modified file is single.php, and the modified file varies by WordPress theme.

Copy and paste the following code where you want to display the time ▼

<time class="updated" datetime="<?php echo esc_attr( get_the_modified_date( DATE_W3C ) ); ?>">
Last updated: <?php the_modified_time('F j, Y'); ?> at <?php the_modified_time('g:i a'); ?>
</time>

Where "DATE_W3C" is the php time function (time zone format issue)

Other time formats that can be used are as follows (refer toWordPress backend"Set" time zone) ▼

How does WordPress display the last update date?Recall the latest datetime code

Articles call "before time" instead of date display

Use WordPress built-in functions human_time_diff() achieve.

Copy and paste the code below where you want to display the time ▼

<time class="updated" datetime="<?php echo esc_attr( get_the_modified_date( DATE_W3C ) ); ?>">
<?php printf( __( 'Last updated: %s ago', 'ufomega' ), human_time_diff( get_the_modified_date( 'U' ), current_time( 'timestamp' ) ) ); ?>
</time>

among them,"UFOmega" is the theme name, you can change it to your theme. When set to a custom post_type name, it can be used for the corresponding post type.

PHP has a lot of parameters for dealing with time, but WordPress has its own set of parameters for dealing with time (which can deal with GMT and local time).Function:current_time(), need to be used according to its function.

current_time( 'timestamp' ) Get local time, change to current_time( 'timestamp', 1 ) Returns GMT (zero time zone) time.

WordPress timezone format issue

WordPress websiteCross-time zone issues need to be considered.

If the time zone format of the WordPress site is not uniform, when the Google engine index (data structure), the time may not be displayed or the displayed time may be wrong and inconsistent.

According to Google's official documentation, dates use the ISO 8601 standard.

According to the standard, the datetime function in UTC (International Standard Time) is DATE_W3C

The commonly used time functions in php are:

  • DATE_COOKIE – HTTP Cookies (eg Friday, 13-May-22 15:52:01 UTC)
  • DATE_ISO8601 – ISO-8601 (e.g. 2022-05-13T15:52:01+0000)
  • DATE_W3C – World Wide Web Consortium (eg 2021-05-13T15:52:01+00:00)

Hope Chen Weiliang Blog ( https://www.chenweiliang.com/ ) shared "How WordPress Displays the Last Updated Date?Recall the latest date time code" to help you.

Welcome to share the link of this article:https://www.chenweiliang.com/cwl-28047.html

Welcome to the Telegram channel of Chen Weiliang's blog to get the latest updates!

🔔 Be the first to get the valuable "ChatGPT Content Marketing AI Tool Usage Guide" in the channel top directory! 🌟
📚 This guide contains huge value, 🌟This is a rare opportunity, don’t miss it! ⏰⌛💨
Share and like if you like!
Your sharing and likes are our continuous motivation!

 

Comment

Your email address will not be published. Required fields * Callout

scroll to top