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

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

Patricia Arquette
Patricia ArquetteOriginal
2024-11-10 09:06:02696browse

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

Displaying Dates in ISO 8601 Format Using PHP

When retrieving a datetime value from a MySQL database, it's common to want to display it in the standardized ISO 8601 format. However, using the date() function directly may lead to incorrect results.

Issue:

When attempting to convert a datetime value from a MySQL database to ISO 8601 using date("c", $post[3]), the year may be inaccurate. This is because date() expects a UNIX timestamp as its second argument, not a database timestamp string.

Solution:

To resolve this issue, convert the database timestamp to a UNIX timestamp using strtotime(). Here's the corrected code:

<?php echo date("c", strtotime($post[3])); ?>

By converting the database timestamp to a UNIX timestamp before passing it to date(), it will now correctly format the date in ISO 8601 format, including the correct year.

The above is the detailed content of How to Display Dates in ISO 8601 Format from a MySQL Database in 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