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>
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>
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!