Home  >  Article  >  Database  >  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
Susan SarandonOriginal
2024-11-11 13:31:02683browse

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]) ?>

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])) ?>

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!

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