human_time_diff

Determines the difference between two timestamps.

Description

(string) human_time_diff( (int) $from, (string) $to = '' ); 

The difference is returned in a human readable format such as 1 hour,, "5 mins", 2 days..

Returns (string)

Human readable time difference.

Parameters (2)

0. $from (int)
Unix timestamp from which the difference begins.
1. $to — Optional. (string) => ''
Unix timestamp to end the time difference. Default becomes time() if not set.

Usage

  1. if ( !function_exists( 'human_time_diff' ) ) { 
  2. require_once ABSPATH . WPINC . '/formatting.php'; 
  3.  
  4. // Unix timestamp from which the difference begins. 
  5. $from = -1; 
  6.  
  7. // Optional. Unix timestamp to end the time difference. Default becomes time() if not set. 
  8. $to = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = human_time_diff($from, $to); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/formatting.php  
  1. function human_time_diff( $from, $to = '' ) { 
  2. if ( empty( $to ) ) { 
  3. $to = time(); 
  4.  
  5. $diff = (int) abs( $to - $from ); 
  6.  
  7. if ( $diff < HOUR_IN_SECONDS ) { 
  8. $mins = round( $diff / MINUTE_IN_SECONDS ); 
  9. if ( $mins <= 1 ) 
  10. $mins = 1; 
  11. /** translators: Time difference between two dates, in minutes (min=minute). 1: Number of minutes */ 
  12. $since = sprintf( _n( '%s min', '%s mins', $mins ), $mins ); 
  13. } elseif ( $diff < DAY_IN_SECONDS && $diff >= HOUR_IN_SECONDS ) { 
  14. $hours = round( $diff / HOUR_IN_SECONDS ); 
  15. if ( $hours <= 1 ) 
  16. $hours = 1; 
  17. /** translators: Time difference between two dates, in hours. 1: Number of hours */ 
  18. $since = sprintf( _n( '%s hour', '%s hours', $hours ), $hours ); 
  19. } elseif ( $diff < WEEK_IN_SECONDS && $diff >= DAY_IN_SECONDS ) { 
  20. $days = round( $diff / DAY_IN_SECONDS ); 
  21. if ( $days <= 1 ) 
  22. $days = 1; 
  23. /** translators: Time difference between two dates, in days. 1: Number of days */ 
  24. $since = sprintf( _n( '%s day', '%s days', $days ), $days ); 
  25. } elseif ( $diff < MONTH_IN_SECONDS && $diff >= WEEK_IN_SECONDS ) { 
  26. $weeks = round( $diff / WEEK_IN_SECONDS ); 
  27. if ( $weeks <= 1 ) 
  28. $weeks = 1; 
  29. /** translators: Time difference between two dates, in weeks. 1: Number of weeks */ 
  30. $since = sprintf( _n( '%s week', '%s weeks', $weeks ), $weeks ); 
  31. } elseif ( $diff < YEAR_IN_SECONDS && $diff >= MONTH_IN_SECONDS ) { 
  32. $months = round( $diff / MONTH_IN_SECONDS ); 
  33. if ( $months <= 1 ) 
  34. $months = 1; 
  35. /** translators: Time difference between two dates, in months. 1: Number of months */ 
  36. $since = sprintf( _n( '%s month', '%s months', $months ), $months ); 
  37. } elseif ( $diff >= YEAR_IN_SECONDS ) { 
  38. $years = round( $diff / YEAR_IN_SECONDS ); 
  39. if ( $years <= 1 ) 
  40. $years = 1; 
  41. /** translators: Time difference between two dates, in years. 1: Number of years */ 
  42. $since = sprintf( _n( '%s year', '%s years', $years ), $years ); 
  43.  
  44. /** 
  45. * Filters the human readable difference between two timestamps. 
  46. * @since 4.0.0 
  47. * @param string $since The difference in human readable text. 
  48. * @param int $diff The difference in seconds. 
  49. * @param int $from Unix timestamp from which the difference begins. 
  50. * @param int $to Unix timestamp to end the time difference. 
  51. */ 
  52. return apply_filters( 'human_time_diff', $since, $diff, $from, $to );