Home  >  Article  >  Backend Development  >  How to Obtain UTC Time Stamps with Specific Offsets in PHP?

How to Obtain UTC Time Stamps with Specific Offsets in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-20 06:13:02409browse

How to Obtain UTC Time Stamps with Specific Offsets in PHP?

Obtaining UTC Time Stamps with PHP

In PHP, the date() function is used to retrieve localized timestamps. However, to obtain UTC time stamps with specific offsets, a different approach is required.

Getting UTC Time Stamps with gmdate()

To retrieve UTC time stamps in PHP, you can use the gmdate() function. The syntax is similar to that of date(), but it always returns timestamps in UTC. For example:

<code class="php">$timestamp = gmdate("Y-m-d H:i:s"); // Returns UTC timestamp</code>

The gmdate() function allows you to use the same formatting specifiers as date(), so you can easily format the timestamp to a desired string.

Adding GMT/UTC Offset to the Timestamp

To add a GMT/UTC offset to the timestamp, you can use the following string format:

GMT/UTC+/-####

where #### represents the offset in minutes. For example, to add an offset of 4 hours (GMT 0400), you would use the following format:

GMT/UTC+0400

You can then append this offset string to the gmdate() result using the . operator:

<code class="php">$timestamp_with_offset = gmdate("Y-m-d H:i:s") . " GMT/UTC+0400";</code>

This will output a timestamp in UTC with the specified offset.

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