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";
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";
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!