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

Susan Sarandon
Release: 2024-10-20 06:13:02
Original
408 people have browsed it

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

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+/-####
Copy after login

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

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

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!

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
Latest Articles by Author
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!