2016-11-29 58 views
2

如何添加日期至日期yii2?當我輸入checkin和hari時,然後在checkout = checkin + hari值。 你能幫助我嗎? 謝謝。如何在yii2中添加日期?

這是我在形式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>' 
 
       ] 
 
     ]);?>

this is my view in form yii2

+0

您是否需要這種格式? –

+0

不,最重要的是結帳日期的輸出@EdvinTenovimas –

+1

在這種情況下...... –

回答

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時間戳。

+0

非常感謝,這是工作@Edvin Tenovimas –