Converting PHP Date to MySQL Format (0000-00-00)
You have a PHP date field that needs to be converted to the MySQL format of 0000-00-00 for database storage. While you've attempted various methods, including using date('Y-m-d' strtotime($date);, they haven't been successful.
The issue lies in the date separator "-" causing problems with strototime(). The correct approach depends on the data type of the MySQL column:
<code class="php">$date = date('Y-m-d', strtotime(str_replace('-', '/', $date)));</code>
<code class="php">$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));</code>
If the date is formatted differently (e.g., "02/07/2009 00:07:00"), you can use a regular expression to reformat it as follows:
<code class="php">$date = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '-- ', $date);</code>
Output:
2009-07-02 00:07:00
By implementing these steps, you can convert PHP dates to the MySQL format 0000-00-00. The specific method to use depends on the MySQL column data type and the formatting of the PHP date.
The above is the detailed content of How to Convert a PHP Date to MySQL\'s 0000-00-00 Format?. For more information, please follow other related articles on the PHP Chinese website!