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

DDD
Release: 2024-10-20 06:15:02
Original
463 people have browsed it

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>
Copy after login

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
Copy after login

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
Copy after login

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>
Copy after login

Output:

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

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!

source:php
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!