php - 从用户输入的选定日期在 MySQL 中始终存储为 "1970"

标签 php mysql angularjs datetime

我在使用 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/

相关文章:

mysql - 在 MySQL 表中存储大量文本?

angularjs - 从 angularjs ui-grid 菜单中删除聚合菜单项

PHP MySQL 服务从多个 id 获取数据

php - 使 bootstrap 模式利用与/sql 表分开

php - 从多个表中检索内容..可以吗?

angularjs - 如何使用 AngularJS POST 二进制文件(带上传 DEMO)

angularjs - 您可以记录 Angular 数据绑定(bind)错误吗?

php - WooCommerce:随机显示一些 5 星级产品评论

php pdo函数连接到数据库并进行查询

php - 计算表中包含 file_name 的行数