Nicola Mustone

Happiness Lead @ Automattic



Human Readable Time Difference in WordPress

At least once in your life you have seen a site that has limited time offers, or memberships that expires on a specific date.

If you noticed, they often say “Expires in X”. That’s easy to understand for a human, but how do you do it in WordPress?

Meet human_time_diff

Ideally you would have timestamps saved in your database or generated at the moment, instead of human readable dates. Timestamps for a human are impossible to read since they show the seconds since January 1st 1970 (UTC). For example, the current timestamp is 1471946578. Would you be able to tell what is the date by reading that? Obviously no.

When you have two timestamps and want to make a time difference between them, you also would want to show it in a readable format for humans.

human_time_diff is a WordPress function that allows you to show a time difference in a human readable format.

You would use human_time_diff by writing something like this:


// Difference for between now and a date in the future
$future_timestamp = strtotime( '1st January 2020' );
$difference = human_time_diff( current_time( 'timestamp' ), $future_timestamp );
// Difference between a date in the past and now
$past_timestamp = strtotime( '1st January 2016' );
$difference = human_time_diff( $past_timestamp, current_time( 'timestamp' ) );

view raw

functions.php

hosted with ❤ by GitHub

Note that the order of arguments passed to the function is always from and to.

In the first example above, I compare a date in the future against the current date, so I use the current timestamp as first argument.

In the second example, I compare a date in the past against the current date, so the first argument would be the past date, and then the current timestamp.

Advertisements

Subscribe to This Blog

Receive new articles from this blog directly in your inbox! No spam guaranteed!

Join 651 other subscribers

Contribute to Improving This Blog

Did you enjoy this article? Was it helpful? Contribute to help me write more articles! The funds collected from this blog are reinvested directly into improving my skills or the blog so that I can provide more and better content!

One-Time
Monthly
Yearly

Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount

€5.00
€15.00
€100.00
€5.00
€15.00
€100.00
€5.00
€15.00
€100.00

Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

DonateDonate monthlyDonate yearly
Advertisements

More Posts That You Might Like…


2 responses to “Human Readable Time Difference in WordPress”

  1. Wow I didn’t know that function existed so I wrote my own. Thanks for the tip!

    1. I didn’t know it as well! I discovered it a few days ago 🙂

Leave a Reply

Advertisements

Categories

Newsletter

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 651 other subscribers

About The Author

Hi, I’m Nico! Support Lead at Automattic, championing WordPress.com & WooCommerce. Off-duty, I’m at Elemental Beacon, leading epic D&D adventures. Let’s connect, whether it’s about WordPress or a quest!

Advertisements

Don't Miss a Thing!

Receive new articles from this blog directly in your inbox!

No spam guaranteed!

Join 651 other subscribers

Continue Reading

%d bloggers like this: