Home > Database > Mysql Tutorial > body text

How to Correctly Format Dates in ISO 8601 Using PHP?

DDD
Release: 2024-11-11 12:18:02
Original
602 people have browsed it

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]) ?>
Copy after login

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])) ?>
Copy after login

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!

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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template