Convert Timestamp to Time Ago Format in PHP

Many web applications like social media apps and messaging apps use "Time Ago" format to display time of posts, comments and other actions users made. Learn how to convert a timestamp in PHP to time ago format.

Convert Timestamp to Time Ago Format in PHP

Showing a timestamp in "Time Ago" format in web applications has many advantages. One reason most web applications use it to avoid confusion for users in different time zones than the user who published a post or commented on a post. This post demonstrates how to create a PHP time ago function to convert timestamp to "time ago" format.


Time Ago Function in PHP

Time ago format is useful to display PHP DateTime in a human readable format which is easier to understand for website users and avoids the confusion for users from different time zones. Following are the steps to convert timestamp to time ago format in PHP:

  • Create a function to_time_ago_format().
  • Store the difference of current time and provided timestamp in a variable $ago_from_now.
  • Check if $ago_from_now is less than one, then it is a less than one section value. Return "Just now" from here and skip further code execution.
  • Declare a variable $time_ago in order to store time ago string and initially assign it a NULL value.
  • Declare variables for interval units for second, minute, hour, week, day, month and year.
  • Create an associative array $time_intervals to store interval units being the keys and values as desired or relevant string value for each interval unit.
  • Sort the  $time_intervals array in descending order so we have the largest key value as first entry and smallest key as last entry. This is important to return the correct string. You can also declare this array in reverse order to skip sorting.
  • Loop through each entry in $time_intervals array and check if $ago_from_now is greater than or equal interval unit key in array.
  • If the above condition matches then assign the correct string to $time_ago variable.
  • Finally return the $time_ago string from function.
/**
* @param int $timestamp
* @return string|null
*
*/
function to_time_ago_format(int $timestamp): ?string {
$ago_from_now = time() - $timestamp;

// If time difference is less than one second return "Just now"
if($ago_from_now < 1){
return 'Just now';
}

$time_ago = NULL;

// Define the time intervals (in seconds)
$second = 1;
$minute = 60 * $second;
$hour = 60 * $minute;
$day = 24 * $hour;
$week = 7 * $day;
$month = 30 * $day;
$year = 12 * $month;

// Create an array of time units and strings needed for specific unit
$time_intervals = [
$second => 'second',
$minute => 'minute',
$hour => 'hour',
$day => 'day',
$week => 'week',
$month => 'month',
$year => 'year',
];

// Sort array in descending order by keys
krsort($time_intervals);

foreach($time_intervals as $time => $string){
if($ago_from_now >= $time){
$unit = floor($ago_from_now / $time);

$time_ago = sprintf('%d %s%s %s', $unit, $string, $unit > 1 ? 's' : '', 'ago');
break;
}
}

return $time_ago;
}

This function will now return the time ago string for given timestamp. The function accepts a timestamp as parameter so DateTime format first needs to be converted to timestamp using strtotime() function of PHP. This is show this function can be used to display time ago format:

$time_ago = to_time_ago_format(strtotime('2020-04-03 02:35:12'));

echo $time_ago;

This functionality of converting timestamp to time ago format can be tested with following sample code snippet:

to_time_ago_format(strtotime('now')); // Just Now
to_time_ago_format(strtotime('-2 seconds')); // 2 seconds ago
to_time_ago_format(strtotime('-2 minutes')); // 2 minutes ago
to_time_ago_format(strtotime('-2 hours')); // 2 hours ago
to_time_ago_format(strtotime('-2 days')); // 2 days ago
to_time_ago_format(strtotime('-2 weeks')); // 2 weeks ago
to_time_ago_format(strtotime('-2 months')); // 2 months ago
to_time_ago_format(strtotime('-2 years')); // 2 years ago