Home  >  Article  >  Database  >  How to Correctly Format Dates in ISO 8601 Using PHP?

How to Correctly Format Dates in ISO 8601 Using PHP?

DDD
DDDOriginal
2024-11-11 12:18:02603browse

How to Correctly Format Dates in ISO 8601 Using PHP?

Using PHP to Format Dates in ISO 8601

You've encountered an issue where dates in your MySQL database are being displayed incorrectly in ISO 8601 format. Specifically, a date like "17 Oct 2008" is showing as "1969-12-31T18:33:28-06:00."

Problem Analysis

The code you're using for formatting dates is:

<?= date("c", $post[3]) ?>

However, the second argument to the date() function should be a UNIX timestamp, not a database timestamp string. A UNIX timestamp is a numerical representation of the number of seconds that have passed since January 1, 1970.

Solution

To correct this, you need to convert your database timestamp to a UNIX timestamp using the strtotime() function:

<?= date("c", strtotime($post[3])) ?>

This will ensure that the date() function receives a valid UNIX timestamp and correctly formats the date in ISO 8601 format.

The above is the detailed content of How to Correctly Format Dates in ISO 8601 Using 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