Home >Backend Development >PHP Tutorial >How to Convert UNIX Timestamps to ISO 8601 Formatted Dates in PHP?

How to Convert UNIX Timestamps to ISO 8601 Formatted Dates in PHP?

Linda Hamilton
Linda HamiltonOriginal
2024-11-14 09:50:021084browse

How to Convert UNIX Timestamps to ISO 8601 Formatted Dates in PHP?

Convert UNIX Timestamps to ISO 8601 Formatted Dates in PHP

Converting UNIX timestamps into formatted date strings like "2008-07-17T09:24:17Z" in PHP requires a simple technique. To transform a timestamp into this format, follow these steps:

PHP Implementation Using gmdate() Function

The gmdate() function provides a straightforward way to achieve this conversion. It takes a timestamp as an argument and returns a formatted date string according to the specified format. To obtain the ISO 8601 format, use the following syntax:

gmdate("Y-m-d\TH:i:s\Z", $timestamp);

Where:

  • $timestamp is the UNIX timestamp to be converted.
  • "Y-m-dTH:i:sZ" specifies the ISO 8601 format.

Example:

Consider the timestamp 1333699439. Using the gmdate() function, you can convert it to an ISO 8601 string as follows:

$timestamp=1333699439;
echo gmdate("Y-m-d\TH:i:s\Z", $timestamp);

This will output the result: "2008-07-17T09:24:17Z".

The above is the detailed content of How to Convert UNIX Timestamps to ISO 8601 Formatted Dates 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