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!