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:

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.

2 replies

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.