如何添加日期yii2?当我输入checkin和hari时,然后在checkout = checkin + hari中输入值。 你能帮助我吗? 感谢。
这是我在yii2表单中的代码,如果日期选择器tanggal_masuk和字段jumlah_hari输入数据,则日期选择器= tanggal_masuk + jumlah_hari
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'TANGGAL_MASUK',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yy',
'startDate' => date('d-M-y'),
'prepend' => '<i class="icon-calendar"></i>'
]
]);?>
<?php $data =
['1' => '1 Malam',
'2' => '2 Malam',
'3' => '3 Malam',
'4' => '4 Malam',
'5' => '5 Malam',
'6' => '6 Malam',
'7' => '7 Malam',
'8' => '8 Malam',
'9' => '9 Malam',
'10' => '10 Malam',
'11' => '11 Malam',
'12' => '12 Malam',
'13' => '13 Malam',
'14' => '14 Malam',
'15' => '15 Malam']; ?>
<?= $form->field($model, 'JUMLAH_HARI')->widget(Select2::classname(), [
'data' => $data,
'language' => 'en',
'options' => ['placeholder' => 'Hari'],
'pluginOptions' => [
'allowClear' => true
],
]); ?>
<font size="2"><b>Check-Out</b></font>
<?= DatePicker::widget([
'model' => $model,
'attribute' => 'TANGGAL_KELUAR',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'dd-M-yy',
'startDate' => date('d-M-y'),
'prepend' => '<i class="icon-calendar"></i>'
]
]);?>
答案 0 :(得分:2)
小工具设置:
DatePicker::widget([
'model' => $model,
'attribute' => 'TANGGAL_MASUK',
'template' => '{addon}{input}',
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-m-d',
'startDate' => date('d-M-y'),
'prepend' => '<i class="icon-calendar"></i>'
]
]);
区别在于:'format' => 'yyyy-m-d',
(格式已更改)。
现在我们从这个插件中检索值。我们说,我们从Yii::$app->request->post()['Model']['TANGGAL_MASUK']
获得此值:
// Assigned to $time for easier access and converted to UNIX timestamp with strtotime()
$time = strtotime(Yii::$app->request->post()['Model']['TANGGAL_MASUK']);
// Let's calculate the value by adding the value of 2 days (in seconds)
$newTime = $time + 2 * 60 * 60 * 24;
// Let's convert back to your desired format (like: 29-Nov-16)
$newDate = date('y-M-d', $newTime);
现在我们有$newDate
变量,其中包含已添加的2天。请注意,如果您希望能够使用(添加/减去或插入到数据库中),则必须更改格式或使用UNIX时间戳。