【发布时间】:2016-12-17 00:40:27
【问题描述】:
我在保存用户使用 AngularJS 在 input[date] 字段中选择的时间并发布到 Mysql DB 时遇到问题。
在控制台中,当我记录包含所有输入字段值的数据对象时,日期似乎采用正确的格式“2016-12-28”,但在我的 MySQL DB 中,值始终存储为“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();
}
【问题讨论】:
-
你是从哪里记录的?
-
我首先从 var data = { time:$scope.event.the_time, date:eventDate, location:$scope.event.location, place:$scope.event.place }
标签: php mysql angularjs datetime