Home >Database >Mysql Tutorial >How to Convert Date Strings to MySQL DATETIME?
Converting Date Strings to MySQL DATETIME Field
In MySQL, dates and times are stored in the DATETIME field. If you have records with dates formatted as strings, such as '04/17/2009', you need to convert them to the MySQL DATETIME format.
To achieve this conversion, you can use two steps:
Convert String to Timestamp:
Use the PHP strtotime() function to convert the date string into a timestamp:
$timestamp = strtotime($string);
Format Timestamp to DATETIME:
Use the date() function to format the timestamp into the MySQL DATETIME format:
$datetime = date("Y-m-d H:i:s", $timestamp);
Within your foreach loop, you can perform these steps for each date string, updating the existing record with the converted DATETIME value. This approach allows you to easily handle date string conversions to MySQL DATETIME format.
The above is the detailed content of How to Convert Date Strings to MySQL DATETIME?. For more information, please follow other related articles on the PHP Chinese website!