Converting ISO8601 Timestamps to MySQL DATE Format in PHP
In this article, we'll explore how to efficiently convert timestamps in ISO8601 format to the MySQL DATE format using PHP.
Problem Statement
Given an ISO8601 timestamp, such as "2014-03-13T09:05:50.240Z," our goal is to transform it into the MySQL DATE format, which represents only the date component (e.g., "2014-03-13").
Solution
To achieve this conversion, we will utilize PHP's built-in functions:
<code class="php">$date = '2014-03-13T09:05:50.240Z'; $fixed = date('Y-m-d', strtotime($date));</code>
Additional Note
Some timestamps in ISO8601 format may not be recognized by the strtotime function. In such cases, you can manually extract the date component using substr:
<code class="php">$date = '2014-03-13T09:05:50.240Z'; $fixed = date('Y-m-d', strtotime(substr($date, 0, 10)));</code>
The above is the detailed content of How to Convert ISO8601 Timestamps to MySQL DATE Format in PHP?. For more information, please follow other related articles on the PHP Chinese website!