且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

将php日期转换为mysql格式

更新时间:2023-11-29 12:24:16

$date = mysql_real_escape_string($_POST['intake_date']);

1.如果您的MySQL列为DATE,请输入:

1. If your MySQL column is DATE type:

$date = date('Y-m-d', strtotime(str_replace('-', '/', $date)));

2.如果您的MySQL列为DATETIME,请输入:

2. If your MySQL column is DATETIME type:

$date = date('Y-m-d H:i:s', strtotime(str_replace('-', '/', $date)));

您不必工作strototime(),因为它不适用于破折号-分隔符,它将尝试进行减法.

You haven't got to work strototime(), because it will not work with dash - separators, it will try to do a subtraction.

更新,日期的格式设置不能使用strtotime(),请改用以下代码:

Update, the way your date is formatted you can't use strtotime(), use this code instead:

$date = '02/07/2009 00:07:00';
$date = preg_replace('#(\d{2})/(\d{2})/(\d{4})\s(.*)#', '$3-$2-$1 $4', $date);
echo $date;

输出:

2009-07-02 00:07:00