我在使用 AngularJS 保存用户在输入 [日期] 字段中选择的时间并将其发布到 Mysql 数据库时遇到问题。
在控制台中,当我记录包含所有输入字段值的数据对象时,日期似乎采用正确的格式“2016-12-28”,但在我的 MySQL 数据库中,值始终存储为“1970-01- 01".
我的前端:
<input type="date" placeholder="Datum" class="form-control" ng-model="event.the_date">
AngularJS:
$scope.AddEvent = function(){
var eventDate = new Date($scope.event.the_date).toISOString().slice(0,10);
var data = {
time:$scope.event.the_time,
date:eventDate, <-- this gets the right value from the input field ex. "2016-12-28"
location:$scope.event.location,
place:$scope.event.place
}
//continue with $http.post
我的后端:
$params = json_decode(file_get_contents('php://input'), true);
$date = date("Y-m-d",strtotime($params['the_date']));
try{
$sql = "INSERT INTO events (the_date,the_time,place,location) VALUES (:the_date,:the_time,:place,:location)";
$query = $con->prepare($sql);
$query->execute(array(
':the_date'=>$date, <-- this get always the 1970 date!
':the_time'=>$params['the_time'],
':place'=>$params['place'],
':location'=>$params['location'],
));
}catch(PDOException $e){
echo $e->getMessage();
}
最佳答案
假设
$date = date("Y-m-d",strtotime($params['the_date']));
应该是
$date = date("Y-m-d",strtotime($params['date']));
当您var_dump($params)
时,您是否获得了正确的数据?
关于php - 从用户输入的选定日期在 MySQL 中始终存储为 "1970",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41194294/