How to Get UTC Timestamps with Timezone Offset in PHP?

Susan Sarandon
Release: 2024-10-20 06:09:02
Original
983 people have browsed it

How to Get UTC Timestamps with Timezone Offset in PHP?

Obtaining UTC Timestamps in PHP with gmdate

To retrieve a UTC/GMT timestamp with a specified timezone offset in PHP, you can utilize the gmdate() function. Unlike date() which provides timestamps based on the local timezone, gmdate() consistently outputs timestamps in UTC.

The syntax of gmdate() is analogous to that of date():

<code class="php">gmdate(format, timestamp);</code>
Copy after login

Where:

  • format: String specifying the date and time format.
  • timestamp: Optional timestamp; if not provided, the current UTC time is used.

Example:

To obtain a UTC timestamp in the format "YYYY-MM-DD HH:MM:SS" with the timezone offset "GMT/UTC 0400":

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

This will print the current UTC timestamp with the specified timezone offset appended.

The above is the detailed content of How to Get UTC Timestamps with Timezone 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
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!