How to Display “12 Minutes Ago” in PHP Webpages
Time is an essential aspect of user interfaces, especially in web applications where displaying the relevant time elapsed since an event enhances the user’s experience. A common way to do this is to show messages like “12 minutes ago” or “5 seconds ago.” In this blog post, we will explore how to implement this feature in your PHP application through a handy function.
Why Use Time Elapsed Formatting?
Displaying time in an elapsed format has several advantages, including:
- Clarity: Instead of showing a specific date and time, which may confuse users, elapsed time is intuitive.
- Relevance: It provides immediate context related to events, helping users understand currency of information (e.g., how recent a post is).
- Brevity: Shortened time messages keep your interface clean and user-friendly.
The Solution: Implementing the Time Elapsed Function
To display messages like “12 minutes ago,” we can create a PHP function called time_since()
that takes the number of seconds since an event as input and returns a human-readable string representing how long ago it occurred.
Here is how the function looks in PHP:
function time_since($since) {
$chunks = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
array(1 , 'second')
);
for ($i = 0, $j = count($chunks); $i < $j; $i++) {
$seconds = $chunks[$i][0];
$name = $chunks[$i][1];
if (($count = floor($since / $seconds)) != 0) {
break;
}
}
$print = ($count == 1) ? '1 '.$name : "$count {$name}s";
return $print;
}
Breaking Down the Function
-
Defining Time Chunks: The function defines an array called
$chunks
. This array holds different time intervals and their corresponding labels (i.e., years, months, weeks, days, hours, minutes, seconds). These intervals help determine how to express the elapsed time. -
Calculating Time Elapsed: A loop iterates through the
$chunks
array to find the largest time unit that corresponds to the number of seconds passed. The function calculates how many of that unit fits into the given time. -
Formatting the Output: Once the suitable time unit is found, the function formats the return string. If the time is singular (e.g., “1 minute”), it avoids pluralizing the unit. Otherwise, it adds an ’s’ at the end (e.g., “2 minutes”).
Example Usage
To use this function, simply call it with the number of seconds as the argument. For example:
echo time_since(720); // Outputs: "12 minutes"
echo time_since(5); // Outputs: "5 seconds"
This method makes it easy to display how long ago an event occurred in a format that is easy for users to understand at a glance.
Conclusion
Implementing a simple time_since()
function can significantly enhance the way time is presented in your PHP application. Not only does it provide necessary context, but it also creates a more engaging and friendly user experience. By leveraging time elapsed formatting, you can create a compelling interface that helps users easily understand the timeliness of the content they interact with.
Feel free to adapt and expand upon this function to suit your needs, and soon your web application will be more user-friendly and informative. Happy coding!