Home » Functions » human_time_diff

Function Name: human_time_diff

Explanation: The human_time_diff function is a WordPress function that is used to calculate and display the time difference between two given timestamps in a human-readable format. It returns a string that represents the time difference in a user-friendly way, such as "2 hours ago", "3 days ago", or "1 week ago".

This function is particularly useful in scenarios where you want to show the elapsed time since a specific event occurred on your WordPress website. It provides a more intuitive representation of time, making it easier for users to understand when something happened.

Example Usage:

Let’s say you have a blog post on your WordPress website and you want to display the time since it was published. You can use the human_time_diff function to calculate and format the time difference:

$post_id = get_the_ID(); // Get the ID of the current blog post
$timestamp = get_the_time('U', $post_id); // Get the timestamp of the post
$time_diff = human_time_diff($timestamp, current_time('timestamp')); // Calculate the time difference

echo 'Published ' . $time_diff . ' ago'; // Display the time difference

In the above example, we first retrieve the ID of the current blog post using the get_the_ID function. Then, we use the get_the_time function to get the timestamp of the post. We pass the timestamp and the current timestamp (using current_time('timestamp')) to the human_time_diff function to calculate the time difference. Finally, we display the time difference by echoing it along with some text.

This will output something like "Published 2 hours ago" or "Published 3 days ago", depending on the actual time difference between the post’s timestamp and the current time.

Remember to adjust the code according to your specific use case and where you want to display the time difference.

So the human_time_diff function is an essential tool for presenting time differences in a human-readable format, enhancing the user experience on your WordPress website.

Learn More on

WordPress snippets using the human_time_diff function

Register an account to save your snippets or go Pro to get more features.