[英]String to Date conversion for format DD/MM/YYYY in PHP and MYSQL is not working
将日期插入MYSQL数据库时出现问题。 考虑以下变量:
$mydate = '18/06/2016'
// dd / mm / yyyy格式
现在,我试图将此日期插入我的MYSQL数据库的日期字段(dob)中,这使我感到语法错误。 以下是我的PHP / MYSQL查询:
insert into mytable (dob) values (date('dd/mm/yyyy', strtotime($mydate)))
以前,当我过去接受日期格式为mm / dd / yyyy时,无需任何转换即可轻松插入它,例如:
insert into mytable (dob) values ($mydate)
我在这里想念的是什么?
更新:
我的实际查询是:
insert into users(fullname, dob, notes) values ('Joy', date('Y-m-d', strtotime('15/06/2016')), 'My notes');
语法错误是:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' strtotime('15/06/2016')), 'My notes')' at line 1
那可能是因为MySQL接受YYYY-MM-DD
格式。 尝试这样插入
<?php
$mydate="18/06/2016";
$date = DateTime::createFromFormat('d/m/Y', $mydate);
$dateFormat=$date->format('Y-m-d');
$sql="INSERT INTO mytable (dob) VALUES ('".$dateFormat."')";
<?php
$date ='18/06/2016';
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
$dateFormat=date("Y-m-d", strtotime($mydate));
$sql="INSERT INTO mytable (dob) VALUES ('".$dateFormat."')";
?>
你能试试这个吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.