我正在使用php,需要解析格式化为dd/mm/yyyy
的日期字符串并将其存储在MySql中。
如何将字符串转换为MySql表中的datetime变量?
最佳答案
最好的方法可能是使用这个:http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_str-to-date
SELECT STR_TO_DATE('04/31/2004', '%m/%d/%Y');
-> '2004-04-31'
或类似的PHP函数,如:
http://www.php.net/manual/en/function.date-parse-from-format.php(来自PHP5.3)
一般的PHP函数看起来像
function convertDate($dateString) {
return date('Y-m-d H:i:s',strtotime(str_replace('/','-',$dateString)));
}
或
function convertDate($dateString) {
$a = explode($dateString('/'));
return "{$a[2]}-{$a[1]}-{$a[0]} 00:00:00";
}