Current Location: Home> Latest Articles> How to Convert Unix Timestamps to Relative Time Formats (Minutes Ago, Hours Ago, Days Ago) in PHP

How to Convert Unix Timestamps to Relative Time Formats (Minutes Ago, Hours Ago, Days Ago) in PHP

gitbox 2025-06-15

1. Introduction

In PHP development, Unix timestamps are commonly used. A timestamp is a representation of the number of seconds since 00:00:00 on January 1st, 1970. However, working directly with timestamps is not always intuitive. Often, we need to convert a timestamp into a more user-friendly relative time format, such as "a few minutes ago," "a few hours ago," or "a few days ago." This article will guide you through how to handle timestamps in PHP and convert them into these relative time formats.

2. Getting the Current Timestamp

Before converting timestamps, we first need to get the current timestamp. PHP provides the built-in function time() to return the current Unix timestamp.

Example code:

$currentTimestamp = time();

The time() function returns an integer representing the number of seconds since 00:00:00 on January 1st, 1970.

3. Converting Timestamps to Relative Time

3.1 Convert to Minutes Ago

To convert a timestamp to "minutes ago," you can use the following code:


function convertToTimeAgo($timestamp) {
    $difference = time() - $timestamp;
    $minutes = floor($difference / 60);
    return $minutes . " minutes ago";
}
$timestamp = 1609459200;  // Assume this is a given timestamp
echo convertToTimeAgo($timestamp);  // Output: xx minutes ago

In this code, we calculate the difference between the current timestamp and the given timestamp. Then, we divide the difference by 60 to get the number of minutes. Since the difference is a float, we use the floor() function to round it to an integer. Finally, we return the number of minutes followed by the string " minutes ago."

3.2 Convert to Hours Ago

Similarly, to convert a timestamp to "hours ago," use this code:


function convertToTimeAgo($timestamp) {
    $difference = time() - $timestamp;
    $hours = floor($difference / 3600);
    return $hours . " hours ago";
}
$timestamp = 1609459200;  // Assume this is a given timestamp
echo convertToTimeAgo($timestamp);  // Output: xx hours ago

In this case, we divide the time difference by 3600 (the number of seconds in an hour) to get the number of hours.

3.3 Convert to Days Ago

To convert a timestamp to "days ago," use the following code:


function convertToTimeAgo($timestamp) {
    $difference = time() - $timestamp;
    $days = floor($difference / 86400);
    return $days . " days ago";
}
$timestamp = 1609459200;  // Assume this is a given timestamp
echo convertToTimeAgo($timestamp);  // Output: xx days ago

In this example, we divide the time difference by 86400 (the number of seconds in a day) to get the number of days.

4. Conclusion

PHP provides powerful time-handling functions that allow us to easily convert timestamps into relative time formats. This is useful in many scenarios, such as displaying article publish times or post times on social media. By calculating the difference between the current timestamp and a given timestamp, we can convert it into minutes, hours, or days ago formats.

In practical applications, we can further optimize these functions based on specific needs and add more logic, such as considering time zones or more complex time calculations, to improve the user experience.