Home  >  Article  >  Database  >  How to Convert ISO8601 Datetime to MySQL DATE Format in PHP?

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

DDD
DDDOriginal
2024-10-25 08:21:02198browse

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

Converting ISO8601 to MySQL DATE Format in PHP

Q: How to convert the ISO8601 formatted datetime '2014-03-13T09:05:50.240Z' to the MySQL DATE formatted '2014-03-13' in PHP?

A: Here's a method that utilizes PHP's strtotime and date functions:

<code class="php">$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime($date));

echo $fixed; // Outputs '2014-03-13'</code>

For detailed documentation of the date function, refer to: http://php.net/manual/en/function.date.php

Alternatively, if strtotime returns 0, try this modified version:

<code class="php">$date = '2014-03-13T09:05:50.240Z';

$fixed = date('Y-m-d', strtotime(substr($date,0,10)));

echo $fixed; // Outputs '2014-03-13'</code>

The above is the detailed content of How to Convert ISO8601 Datetime to MySQL DATE Format in 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