white analog clock with black hands on a blue background

Displaying ‘Time Since Posted’ on WordPress

In this post we’re going to create a function that displays time since a post was posted. The human_time_diff() function allows you to get the difference between two given times in a readable format; for example: 5 minutes, 10 hours, 9 days, etc.

This mapping is often found in social networks such as Twitter and Facebook, instead of the date of publication. This allows readers to understand the “freshness” of the content quickly, without thinking about what day it is.

This time format has increasingly appeared in themes for WordPress. This makes it easy to use the built-in WordPress function human_time_diff() to display time since posted on WordPress.

Arguments

Function human_time_diff() takes only two arguments:

  • $From – the mandatory argument, the beginning of the time range
  • $To – the end of the time range, which is the current time by default

Both arguments use time in the timestamp format  – the number of seconds since January 1, 1970.

PHP and WordPress have built-in values for the function. For PHP use time() and mktime(), and for WordPress use get_the_time()  and current_time().

an analog clock on a table covered in paper covered in typed prose

Example 1:

The function human_time_diff() can return the difference between the two marks as a number of seconds, minutes, hours, days, weeks, months, or years.

echo human_time_diff(time() - 60); // 1 minute

echo human_time_diff(time() - 3600); // 1 hour

echo human_time_diff(time() - 259200); // 3 days

Please note that these examples do not specify the second argument $to. In this case, the mark uses WordPress current time using the time() function.

Example 2:

Let’s look at a simple example that uses human_time_diff() to display our new time-since metric on a post by editing the Loop:

while (have_posts()) {

the_post();

// ... This header, content, etc.

 

$ Human_time = human_time_diff(get_the_time('U'), current_time ('timestamp'));

printf('%s Posted ago.', $human_time);

}

Function get_the_time() allows you to record the timestamp published in the required format, but please note that this function uses the time zone settings that can be changed under Settings→ General in the administration panel WordPress.

That is why we set the second argument to the function human_time_diff() and used the built-in WordPress function current_time(), which takes into account the time zone setting, unlike the default time() function.

In other words, make sure that both values in human_time_diff() are in the same time zone. You can also use the function get_post_time() instead of get_the_time(). It allows you to mark time in the time zone GMT/UTC:

$Human_time = human_time_diff(get_post_time('U', true));

You can specify the second argument to the function human_time_diff() as the default function of time(), which also returns the timestamp in the time zone GMT/UTC.

an analog clock in a public building

Example 3:

Many search engine spiders pay special attention to the date of the publication, but the format “x  days ago” they do not perceive. Because of this, search engine optimization often uses micro-formats.

$Human_time = human_time_diff(get_post_time('U', true));

$Iso8601_time = get_the_time('c');

printf ('<span class = "entry-date published" datetime = "% s"> Posted by% s ago. </ span>', $iso8601_time, $human_time);

With this, visitors will see the date in a convenient format human_time_diff() and search engines can read the date in ISO 8601 format.

Example 4:

The last example is a conclusion date format if the article was published over a week ago:

$Human_time = human_time_diff(get_post_time('U', true));

$Regular_time = get_the_time(get_option('date_format'));

$Output_time = sprintf('%s ago', $human_time);

 

if (time()> get_post_time('U', true) + 7 * DAY_IN_SECONDS)

$Output_time = $regular_time;

printf ('Posted on%s.', $output_time);

If the article has been published for more than seven days, this code will display the date in the format that is set in the site settings.

Function human_time_diff() can be found in the file WP-includes/formatting.php. It uses the function of the location and the presence of any translation packages for WordPress and copes well with the plural form of numbers (1 day, 2 days, 5 days). To learn more about the functions get_the_time() and get_post_time(), you can read the Codex.

Get started.

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.