我很难在MySQL Date列中插入正确的日期。我的代码如下。
对于日期字段,我使用的是Air Datepicker插件
<!-- HTML-->
<div class="<?php ab_form_group_class( 'availability' ) ?>">
<label for="age" class="control-label">What is your availability date?</label>
<input type="text" name="availability" class="datepicker-here form-control" id="availability"
placeholder="dd/mm/yyyy" <?php ab_post_value('availability') ?>>
<?php ab_form_error( 'availability' ) ?>
</div>
<!-- Javascript -->
$('.datepicker-here').datepicker({
language: 'en',
todayButton: true,
inline: true,
autoClose: true,
language: {
days: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'],
daysShort: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'],
daysMin: ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
months: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
monthsShort: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'],
today: 'Today',
clear: 'Clear',
dateFormat: 'dd/mm/yyyy', // I have tried setting yyyy-mm-dd but this too didn't solves
timeFormat: 'hh:ii aa',
firstDay: 0
}
});
<?php
// SQL Column
`availability` DATE NOT NULL,
// PHP Code
$availability = $_POST[ 'availability' ];
// when I set input format to dd/mm/yyyy
$availability = date('Y-m-d', strtotime($availability));
我在MySQL中获得的结果是 0000-00-00
,甚至我尝试使用普通文本字段(没有datepicker)那时它插错了日期值。
在@bansi建议之后,我试过这个。它正在正确转换格式,但仍插入0000-00-00
$available_date = DateTime::createFromFormat( 'd/m/Y', $availability );
$availability = $available_date->format( 'Y-m-d' );