Retrieving time difference in words with human_time_diff in WordPress

The human_time_diff function in WordPress calculates the difference between two given timestamps and returns the result in a human-readable format. This can be useful for displaying how long ago a particular event occurred, such as a post being published or a comment being made, in a more user-friendly way.

By using human_time_diff, developers can easily convey the passage of time in a way that is easy for users to understand, without needing to perform complex date calculations or formatting.

Parameters Accepted by WordPress human_time_diff Function

The human_time_diff function accepts the following parameters:

  • $from (int) – This parameter is required and represents the Unix timestamp from which the time difference begins.
  • $to (int) – This parameter is optional and represents the Unix timestamp to end the time difference. If not set, the default value becomes the current time.

Value Returned by WordPress human_time_diff Function

The human_time_diff function returns a string representing the human-readable time difference.

Examples

How to use human_time_diff to display time difference

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

echo "<p>Posted " . $time_diff . " ago</p>";

This code snippet retrieves the current time and the time a post was published, calculates the difference using human_time_diff, and then displays the time difference in a human-readable format.

How to use human_time_diff in a loop

if ( have_posts() ) {
 while ( have_posts() ) {
 the_post();
 $post_time = get_the_time( 'U' );
 $time_diff = human_time_diff( $post_time, current_time( 'timestamp' ) );

 echo "<p>Posted " . $time_diff . " ago</p>";
 }
}

This code snippet demonstrates using human_time_diff within a WordPress loop to display the time difference for each post in a human-readable format.

How to use human_time_diff with a custom date

$custom_date = '2022-01-01 12:00:00';
$current_time = current_time( 'timestamp' );
$time_diff = human_time_diff( strtotime( $custom_date ), $current_time );

echo "<p>Custom date was " . $time_diff . " ago</p>";

This code snippet calculates the time difference between a custom date and the current time using human_time_diff and displays the result in a human-readable format.

Conclusion

The human_time_diff function is a valuable tool for developers looking to display time differences in a user-friendly format. By utilizing this function, developers can easily convert raw time differences into human-readable strings, making it easier for users to understand and interpret. Additionally, the flexibility of the function allows for customization based on specific project requirements.

With its straightforward implementation and clear documentation, the human_time_diff function is a great addition to any developer’s toolkit. Whether it’s for displaying time differences on a website or within an application, this function provides a simple and effective solution.

The human_time_diff function is a valuable asset for developers seeking to improve the user experience when dealing with time differences.