Home > Backend Development > PHP Tutorial > How do I convert a PHP time() timestamp to a human-readable time in a specific time zone?

How do I convert a PHP time() timestamp to a human-readable time in a specific time zone?

Linda Hamilton
Release: 2024-11-25 04:33:59
Original
708 people have browsed it

How do I convert a PHP time() timestamp to a human-readable time in a specific time zone?

PHP time() and GMT/UTC Timestamps

The time() function in PHP returns a UNIX timestamp, which represents the number of seconds that have elapsed since January 1, 1970 UTC. This timestamp is independent of any specific time zone.

UTC vs. Time Zone

Although UNIX timestamps are often referred to as "UTC timestamps," they do not inherently specify a time zone. UTC (Coordinated Universal Time) is a standard reference time used for global timekeeping and is based on the Greenwich Mean Time (GMT) time zone.

Converting to Human-Readable Time

To convert a UNIX timestamp into a human-readable time in a specific time zone, you need to use a function like date(). For example:

$timestamp = time();
$datetime = date("Y-m-d H:i:s", $timestamp);
echo "Current date and time in London: $datetime";
Copy after login

Using date_default_timezone_set()

If you want to set a default time zone for your application, you can use the date_default_timezone_set() function. This will affect how PHP functions such as date() and time() interpret timestamps. For example:

date_default_timezone_set("Europe/London");
$datetime = date("Y-m-d H:i:s");
echo "Current date and time in London using default timezone set: $datetime";
Copy after login

Conclusion

PHP time() returns a UNIX timestamp that is independent of time zones. To convert this timestamp into a human-readable time in a specific time zone, you need to use a function like date() or set the default time zone using date_default_timezone_set().

The above is the detailed content of How do I convert a PHP time() timestamp to a human-readable time in a specific time zone?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template