Home  >  Article  >  Database  >  Why am I Getting the Wrong Year When Displaying a Date in ISO 8601 Format with PHP?

Why am I Getting the Wrong Year When Displaying a Date in ISO 8601 Format with PHP?

Susan Sarandon
Susan SarandonOriginal
2024-11-09 06:20:02308browse

Why am I Getting the Wrong Year When Displaying a Date in ISO 8601 Format with PHP?

How to Display a Date as ISO 8601 Format with PHP Correctly

You're facing an issue where the date format is incorrect when attempting to display a datetime from a MySQL database using the date function. Instead of the expected year of 2008, you're getting the incorrect year of 1969.

The root cause of this issue lies in the second argument of the date function. You're mistakenly supplying a MySQL database timestamp string instead of a UNIX timestamp.

To resolve this error, you need to convert your database timestamp to a UNIX timestamp using the strtotime function. Here's the corrected code:

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

By utilizing strtotime, you accurately convert your database timestamp into a UNIX timestamp, ensuring that the date function displays the correct ISO 8601 formatted string.

The above is the detailed content of Why am I Getting the Wrong Year When Displaying a Date in ISO 8601 Format with 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