how to add day to date yii2? when i enter checkin and hari, then value in checkout = checkin+hari. can you help me? thanks.
This is my code in form yii2, if date picker tanggal_masuk anda field jumlah_hari enter the data, then date picker = 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>'
]
]);?>
Widget settings:
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>'
]
]);
The difference is this: 'format' => 'yyyy-m-d',
(changed format).
Now we retrieve value from this plugin. Let's say, we get this value from 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);
Now we have $newDate
variable that contains with added 2 days. Note that if you want to be able to use (add/substract or insert into DB), you'll have to change the format or use UNIX timestamp.