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

How to Convert UNIX Timestamps to ISO 8601 Formatted Date Strings in PHP?

Mary-Kate Olsen
Release: 2024-11-12 10:57:02
Original
780 people have browsed it

How to Convert UNIX Timestamps to ISO 8601 Formatted Date Strings in PHP?

Converting UNIX Timestamps to Formatted Date Strings in PHP

When working with timestamps in PHP, it is often necessary to convert them into human-readable date strings. This is particularly useful for displaying dates and time in a consistent and visually appealing format. One common date format used is the ISO 8601 format, which expresses dates and times in the form:

YYYY-MM-DD[T]hh:mm:ss[Z][+/-hh:mm]
Copy after login

In this format, the Z suffix indicates that the time is in UTC (Coordinated Universal Time).

Converting a UNIX Timestamp to ISO 8601 Format

To convert a UNIX timestamp to an ISO 8601-formatted date string, you can use the gmdate() function in PHP. The gmdate() function takes two arguments: a date format string and a timestamp.

For example, to convert the UNIX timestamp 1333699439 to an ISO 8601-formatted date string, you would use the following code:

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

This would output the date string:

2008-07-17T09:24:17Z
Copy after login

The Y-m-dTH:i:sZ format string specifies the following date format elements:

  • Y: Year as a 4-digit number
  • m: Month as a 2-digit number
  • d: Day as a 2-digit number
  • T: Separator between date and time
  • H: Hour as a 2-digit number
  • i: Minute as a 2-digit number
  • s: Second as a 2-digit number
  • Z: UTC time zone indicator

The above is the detailed content of How to Convert UNIX Timestamps to ISO 8601 Formatted Date Strings in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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