Home > Database > Mysql Tutorial > How to Convert ISO8601 Dates to MySQL DATE Format in PHP?

How to Convert ISO8601 Dates to MySQL DATE Format in PHP?

Linda Hamilton
Release: 2024-10-25 19:00:02
Original
742 people have browsed it

How to Convert ISO8601 Dates to MySQL DATE Format in PHP?

Converting ISO8601 to MySQL Date Format in PHP

Suppose you have a date in the ISO8601 format, such as "2014-03-13T09:05:50.240Z," and you want to convert it to a MySQL DATE format like "2014-03-13." Here's how you can achieve this conversion using PHP:

<code class="php">$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));</code>
Copy after login

The strtotime() function converts the ISO8601 string to a PHP timestamp, which can then be formatted using the date() function to obtain the desired date format.

If strtotime() returns 0, you can try the following workaround:

<code class="php">$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime(substr($date, 0, 10)));</code>
Copy after login

This alternative approach ensures compatibility with dates that may not be correctly parsed by strtotime().

The above is the detailed content of How to Convert ISO8601 Dates to MySQL DATE Format in PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template