Home  >  Article  >  Backend Development  >  How to Get UTC Timestamps with Timezone Offset in PHP?

How to Get UTC Timestamps with Timezone Offset in PHP?

Susan Sarandon
Susan SarandonOriginal
2024-10-20 06:09:021068browse

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>

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>

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!

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