Convert Timestamp to Time Ago Format in PHP

Many web applications like social media apps and messaging apps use the "Time Ago" format to display the time of posts, comments and other actions users made. Learn how to convert a timestamp in PHP to a 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 is 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 a timestamp into "time ago" format.


PHP Function to Convert Timestamp to 'Time Ago' Format

The time ago format is useful to display PHP's DateTime in a human-readable format that is easier to understand for website users and avoids confusion for users from different time zones. The following are the steps to convert a timestamp to time ago format in PHP:

  • Create a function with the name to_time_ago_format().
  • Store the difference between the current time and the provided timestamp in an $ago_from_now variable.
  • Check if $ago_from_now is less than one, then it is a less than one-second value. Return "Just now" from here and skip further code execution.
  • Declare a variable $time_ago to store the 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 as keys and values as desired or relevant string values for each interval unit.
  • Sort the  $time_intervals array in descending order so we have the largest key value as the first entry and the smallest key as the 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 the $time_intervals array and check if $ago_from_now is greater than or equal to the interval unit key in the array.
  • If the above condition matches, then assign the correct string to the $time_ago variable.
  • Finally, return the $time_ago string from the 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;
}

Usage of The Time Ago Function 

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

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

echo $time_ago;

PHP Time Ago Examples 

The function we created to convert a timestamp to time ago format will now work as shown in the snippet below:

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

We just created a PHP function to convert timestamps to a time ago format string, making the time of content human-readable and localizing the time for the user.