Home >Backend Development >PHP Tutorial >How to Convert Date Strings into MySQL DATETIME Fields?
When faced with dates stored as strings (e.g., '04/17/2009'), the task of converting them into MySQL DATETIME fields can seem daunting. However, it is possible to achieve this transformation using a combination of built-in PHP functions.
To begin with, PHP offers the strtotime() function which effortlessly converts date strings into timestamps. For instance:
<code class="php">$timestamp = strtotime('04/17/2009');</code>
This timestamp represents the number of seconds since the epoch (January 1, 1970). To obtain the MySQL DATETIME format, we employ the date() function:
<code class="php">$datetime = date("Y-m-d H:i:s", $timestamp);</code>
Here, the Y-m-d H:i:s format corresponds to the DATETIME format expected by MySQL. This final string can now be inserted into the new field for each record, completing the conversion process.
The above is the detailed content of How to Convert Date Strings into MySQL DATETIME Fields?. For more information, please follow other related articles on the PHP Chinese website!