Home > Article > Backend Development > How to Get UTC Time in PHP?
Get UTC Time in PHP
When working with globalized applications, it's often necessary to handle time information in Coordinated Universal Time (UTC). This guide demonstrates how to obtain a UTC timestamp using PHP's date() function.
Using date() for Local Time
The time() function returns the current time as the number of seconds since the epoch (January 1, 1970 at 00:00:00 UTC). The date() function can then be used to format that timestamp into a string. For example:
<code class="php">date("Y-m-d H:i:s", time()); </code>
However, this code snippet will return the local time stamp, which may not be in UTC.
Using gmdate() for UTC
To obtain a UTC timestamp, you can use the gmdate() function, which works similarly to date(). The syntax is identical:
<code class="php">gmdate("Y-m-d H:i:s"); </code>
This will always return a UTC date and time, regardless of the local time zone.
The above is the detailed content of How to Get UTC Time in PHP?. For more information, please follow other related articles on the PHP Chinese website!