I am using Angular Bootstrap UI Datepicker in a form like this:
<!-- 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>
This goes via a controller like this:
$http.post('form-submit.php', $scope.formData)
.......
And the form-submit.php
add this to the database something like this:
$sql = 'INSERT INTO sample_column (date,name) VALUES (:date, :name)';
$stmt = $db->prepare($sql);
$stmt->execute(array(
'date' => $_POST['dt'],
'name' => $_POST['name']
));
}
The date column in mySql is of "date" type and when the script inserts the date value that was received from the form using Anglular Bootstrap UI's datepicker, the value in the sql is showing as 0000-00-00. I am assuming this is because of the difference in date format between the datepicker UI and SQL's date type column.
So my question is:
1) What is the correct 'type' do I need to use in the MySql Column for date fields if I am inserting Anglular Bootstrap UI's datepicker values?
2) How do I insert the date correctly in mysql?
This is what I did in the end to resolve this:
I found that the Anglular Bootstrap UI's datepicker is in ISO8601 format. So I converted the date format from 'ISO8601' to 'Date' in the php file by doing:
$date = '2014-03-13T09:05:50.240Z';
$fixed = date('Y-m-d', strtotime($date));
REF: How to convert ISO8601 to Date format in php
I then inserted this $fixed
value into MySQL DATE type field using the mySQL query. That works for me.