At least once in your life, you have seen a site with limited-time offers or memberships expiring 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 humans are impossible to read since they show the seconds since January 1, 1970 (UTC). For example, the current timestamp is 1471946578. Would you be able to tell me what the date is 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 the 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.
Leave a Reply