【问题标题】:How to insert Angular Datepicker UI value to MySQL Column?如何将 Angular Datepicker UI 值插入 MySQL 列?
【发布时间】:2023-04-07 02:44:02
【问题描述】:

我正在以这样的形式使用Angular Bootstrap UI Datepicker

<!-- DATE -->
<div id="date-group" class="form-group" ng-class="{ 'has-error' : (errorDt) || (userForm.dt.$invalid && submitted) }"> 
    <label>Date</label>    
    <div class="input-group">
     <input type="text" class="form-control" datepicker-popup="{{format}}" name="dt" ng-model="formData.dt" is-open="datepickers.dt" datepicker-options="dateOptions" ng-required="true" close-text="Close" />
      <span class="input-group-btn">
        <button class="btn btn-default" ng-click="open($event,'dt')"><i class="glyphicon glyphicon-calendar"></i></button>
      </span>
    </div>      <!--Closing of input-group-->          

    <span class="help-block" ng-show="errorDt"> {{ errorDt }} </span>
    <span ng-show="userForm.dt.$invalid && submitted" class="help-block">Date is required.</span>
</div>

这通过这样的控制器进行:

$http.post('form-submit.php', $scope.formData)
.......

form-submit.php 将其添加到数据库中,如下所示:

$sql = 'INSERT INTO sample_column (date,name) VALUES (:date, :name)';           

                $stmt = $db->prepare($sql);

                $stmt->execute(array(
                                    'date' => $_POST['dt'],
                                    'name' => $_POST['name']
                                    ));



    }

mySql 中的日期列是“日期”类型,当脚本使用 Angular Bootstrap UI 的日期选择器插入从表单接收到的日期值时,sql 中的值显示为 0000-00-00。我假设这是因为 datepicker UI 和 SQL 的日期类型列之间的日期格式不同。

所以我的问题是:

1) 如果我要插入 Angular Bootstrap UI 的日期选择器值,我需要在 MySql 列中为日期字段使用什么正确的“类型”?

2) 如何在mysql中正确插入日期?

【问题讨论】:

    标签: php jquery mysql angularjs twitter-bootstrap


    【解决方案1】:

    这就是我最终为解决这个问题所做的:

    我发现 Angular Bootstrap UI 的日期选择器是 ISO8601 格式。所以我在php文件中将日期格式从'ISO8601'转换为'Date':

    $date = '2014-03-13T09:05:50.240Z';
    
    $fixed = date('Y-m-d', strtotime($date));
    

    参考号:How to convert ISO8601 to Date format in php

    然后,我使用 mySQL 查询将此 $fixed 值插入 MySQL DATE 类型字段。这对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 2021-02-07
      • 1970-01-01
      • 1970-01-01
      • 2020-02-03
      • 1970-01-01
      相关资源
      最近更新 更多