Home  >  Article  >  Backend Development  >  How to Get UTC Time in PHP?

How to Get UTC Time in PHP?

DDD
DDDOriginal
2024-10-20 06:09:291089browse

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!

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