【问题标题】:selected date input from user always stored as "1970" in MySQL用户选择的日期输入在 MySQL 中始终存储为“1970”
【发布时间】: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


【解决方案1】:

我假设

$date = date("Y-m-d",strtotime($params['the_date']));

应该是

$date = date("Y-m-d",strtotime($params['date']));

当您var_dump($params) 时,您得到的数据是否正确?

【讨论】:

  • 这就是问题所在,我使用 ['the_date'] 而不是 ['date']
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多