When attempting to display a datetime as an ISO 8601 formatted string, users may encounter incorrect output. Specifically, timestamps such as "17 Oct 2008" incorrectly display as "1969-12-31T18:33:28-06:00."
The main issue lies in the structure of the provided code:
<?php echo date("c", $post[3]); ?>
Here, the second argument of the date() function is $post[3], which represents a database timestamp string. However, the date() function expects a UNIX timestamp as its second argument.
To correct this, you need to convert your database timestamp into a UNIX timestamp using the strtotime() function. The corrected code becomes:
<?php echo date("c", strtotime($post[3])); ?>
By incorporating this change, the correct ISO 8601 formatted string will be generated, resolving the incorrect display issue.
The above is the detailed content of Why is my PHP code displaying an incorrect ISO 8601 date?. For more information, please follow other related articles on the PHP Chinese website!