且构网

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

将php日期转换为mysql格式

更新时间:2023-11-29 12:11:10

$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