Home > Database > Mysql Tutorial > How to Display Dates in ISO 8601 Format from MySQL Using PHP?

How to Display Dates in ISO 8601 Format from MySQL Using PHP?

Susan Sarandon
Release: 2024-11-11 13:31:02
Original
761 people have browsed it

How to Display Dates in ISO 8601 Format from MySQL Using PHP?

Displaying Dates in ISO 8601 Format with PHP

When attempting to display a datetime retrieved from a MySQL database as an ISO 8601 formatted string using PHP, an issue may arise where the output is incorrect, as in the incorrect year being displayed.

The code snippet being used is:

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

where $post[3] contains the datetime (CURRENT_TIMESTAMP) from the database.

Solution

The issue lies in the fact that the second argument to the date function expects a UNIX timestamp, whereas the $post[3] variable contains a database timestamp string. To resolve this, the database timestamp must be converted to a UNIX timestamp using strtotime.

The corrected code is:

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

The above is the detailed content of How to Display Dates in ISO 8601 Format from MySQL 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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template