Converting Timestamps to Descriptive Time Strings in PHP
In PHP, you can easily convert timestamps to user-friendly time strings, such as "3 minutes ago" or "1 day ago." Here's how:
Converting to Unix Format
The given timestamp, "2009-09-12 20:57:19," needs to be converted to Unix timestamp format, which represents the number of seconds since January 1, 1970 UTC. To do this, use the PHP strtotime():
$unixTimestamp = strtotime('2009-09-12 20:57:19');
Using a Time Conversion Function
There are various PHP functions available for converting time strings to a desired format. One such function is time_elapsed_string():
echo time_elapsed_string($unixTimestamp);
This will output something like "2 days ago."
The above is the detailed content of How Can I Convert PHP Timestamps to Human-Readable Time Strings?. For more information, please follow other related articles on the PHP Chinese website!