Home  >  Article  >  Backend Development  >  How to Obtain UTC Time Stamps with Time Zone Offset in PHP?

How to Obtain UTC Time Stamps with Time Zone Offset in PHP?

DDD
DDDOriginal
2024-10-20 06:15:02465browse

How to Obtain UTC Time Stamps with Time Zone Offset in PHP?

Get UTC Time Stamp in PHP

When attempting to retrieve time stamps in PHP using date(), by default, Unix time stamps are returned. However, what if you need to obtain UTC/GMT time stamps with time zone offset information?

Using gmdate()

To get UTC/GMT time stamps, including time zone offsets, you can utilize the gmdate() function. The syntax is similar to date():

<code class="php">gmdate("Y-m-d H:i:s \G\M\T/UTC\P");</code>

This expression will return a time stamp in the format YYYY-MM-DD HH:MM:SS GMT/UTC±0000, where the offset is adjusted based on your local time zone settings.

For instance, if your local time zone is 4 hours behind UTC, it will output:

2023-03-08 14:30:00 GMT/UTC-0400

Alternatively, if your local time zone is 10 hours ahead of UTC, the result would be:

2023-03-08 14:30:00 GMT/UTC+1000

Example

To illustrate the usage of gmdate(), consider the following code snippet:

<code class="php"><?php

echo "Current UTC time with time zone offset:\n";
echo gmdate("Y-m-d H:i:s \G\M\T/UTC\P");

?></code>

Output:

Current UTC time with time zone offset:
2023-03-08 14:30:00 GMT/UTC-0400

The above is the detailed content of How to Obtain UTC Time Stamps with Time Zone Offset 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