Home >Backend Development >PHP Tutorial >How Can I Convert PHP Timestamps to Human-Readable Time Strings?

How Can I Convert PHP Timestamps to Human-Readable Time Strings?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-29 04:35:09646browse

How Can I Convert PHP Timestamps to Human-Readable Time Strings?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn