Home >Backend Development >PHP Tutorial >How Can I Calculate and Display Elapsed Time in a Human-Readable Format (e.g., 'xx Minutes Ago') in PHP?
Calculating Time Elapsed Since a Date Time in PHP
Determining the elapsed time since a given date and time, such as "2010-04-28 17:25:43," is a common task in programming. The goal is to generate a user-friendly output indicating the time difference in a concise format like "xx Minutes Ago" or "xx Days Ago."
Personalized Time Representation
While converting the date string to a time object is a necessary step, many answers overlook the desired output format. To achieve the "xx Minutes Ago" format, a customized function is required.
Humanizing Time Calculations
One effective approach is to utilize the humanTiming() function, which calculates the time difference between the current time and the provided timestamp. Within the function:
Example Usage
To use the function:
$time = strtotime('2010-04-28 17:25:43'); echo 'event happened '.humanTiming($time).' ago';
Output
Depending on the time difference, the output will be in the desired format, such as:
The above is the detailed content of How Can I Calculate and Display Elapsed Time in a Human-Readable Format (e.g., 'xx Minutes Ago') in PHP?. For more information, please follow other related articles on the PHP Chinese website!