我试图将DateRangePicker使用的文本输入转换为日期时间输入,因为我的所有日期都是日期时间格式。我的代码如下:
<!-- Include Required Prerequisites -->
<script type="text/javascript" src="//cdn.jsdelivr.net/jquery/1/jquery.min.js"></script>
<script type="text/javascript" src="//cdn.jsdelivr.net/momentjs/latest/moment.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap/latest/css/bootstrap.css" />
<!-- Include Date Range Picker -->
<script type="text/javascript" src="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/bootstrap.daterangepicker/2/daterangepicker.css" />
<script type="text/javascript">
$(function() {
$('input[name="date_start"]').daterangepicker({
autoUpdateInput: false,
locale: {
cancelLabel: 'Clear'
},
format: "DD/MM/YYYY",
startDate: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
endDate: new Date(new Date().getTime() + 24 * 60 * 60 * 1000),
minDate: new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
});
$('input[name="date_start"]').on('apply.daterangepicker', function(ev, picker) {
$(this).val(picker.startDate.format('DD/MM/YYYY') + ' - ' + picker.endDate.format('DD/MM/YYYY'));
$("#date_start").val(picker.startDate.format('DD/MM/YYYY'));
$("#date_end").val(picker.endDate.format('DD/MM/YYYY'));
});
$('input[name="date_start"]').on('cancel.daterangepicker', function(ev, picker) {
$(this).val('');
});
});
</script>
<div class="container" style="margin-top: 70px;">
<?= $this->Flash->render('auth') ?>
<?php $this->Form->templates([
'inputContainer' => '<div class="form-group">{{content}}</div>',
'inputContainerError' => '<div class="error" style="color:red;">{{content}}{{error}}</div>'
]);?>
<div class="col-sm-2">
</div>
<div class="col-sm-8">
<div class="container col-sm-12" style="border-radius: 10px; border: 1px solid;" >
<?= $this->Form->create() ?>
<div class="col-sm-6">
<?= $this->Form->input('date_start', ['class'=>'form-control', 'type'=>'text', 'label'=>'Session Dates',
'placeholder'=>'Please select the start and end dates for your session.']);?>
</div>
<input type="text" name="date_start" id="date_start" value="" />
<input type="text" name="date_end" id="date_end" value="" />
<input id='date_start' class='form_control' name='date_start'/>
<input id='date_end' class='form_control' name='date_end'/>
<script>
<?php $date_starttext ?> = document.getElementById('date_start').value;
<?php $date_endtext ?> = document.getElementById('date_end').value;
<?php $date_startdt = DateTime::createFromFormat('d/M/Y', $date_starttext)->format('Y-M-d'); ?>
<?php $date_enddt = DateTime::createFromFormat('d/M/Y', $date_endtext)->format('Y-M-d'); ?>
</script>
<div>
<?= $this->Form->input('sessions.0.date_start', ['class'=>'form-control', 'type'=>'hidden', 'value'=>$date_startdt]); ?>
</div>
<div>
<?= $this->Form->input('sessions.0.date_end', ['class'=>'form-control', 'type'=>'hidden', 'value'=>$date_enddt]);?>
</div>
<input type="text" name="sessions.0.date_start" id="sessions.0.date_start" value="" />
<input type="text" name="date_start5" id="sessions.0.date_start" value="" />
<input type="text" name="sessions.0.date_end" id="date_end5" value="" />
<?= $this->Form->end() ?>
</div>
</div>
<div class="col-sm-2">
</div>
</div>
但是,当我尝试使用此代码加载页面时,我收到此错误:
在布尔值
上调用成员函数format()
错误消息引用此行:
<?php $date_startdt = DateTime::createFromFormat('d/M/Y', '$date_starttext')->format('Y-M-d'); ?>
我不完全确定错误。据推测,DateTime:createFromFormat下面也有同样的错误。
答案 0 :(得分:0)
根据文档,DateTime :: createFromFormat方法在失败时返回一个新的DateTime实例或FALSE,所以我猜你正在传递一个无效的字符串进行解析,并且该方法返回boolean值为false。