Need help with Kartik Timepicker

Hi

I am using http://demos.krajee.com/widget-details/timepicker

my database field is declared as :

time_from time NOT NULL

CREATE ACTION

in view :




echo $form->field($model, 'time_from')->widget(TimePicker::classname(), []);



in controller :


 

$model->time_from = Yii::$app->formatter->asDate($model->time_from, 'php:H:i:s P');

$model->save();



When I choose 1.30AM time, it stores in db as "01:30:00"

If I choose 1.30PM time, it stores in db as "13:30:00"

EDIT ACTION via controller/update




$model = $this->findModel($id);

return $this->render('update', ['model' => $model,]);



The form display 1.30AM correctly as it is.

but if the time is in PM (1.30PM) keeps showing the "current server time in AM". I can see the 13:30:00 flickers for 0.001 seconds in the field and it changes to show whatever the current time is in AM.

Is this a bug in the Timepicker or I saved the format incorrectly or display the field wrongly?

Please help thanks.

the data

use this in your model class

public function afterFind()

{


    // convert to display format


    $this->end_time=date('g:i a', strtotime($this->end_time));


       $this->start_time=date('g:i a', strtotime($this->start_time));


  


    parent::afterFind();


}





  public function beforeSave($insert)


{


    // convert to storage format


       $this->start_time=date('H:i:s', strtotime($this->start_time));


        $this->end_time=date('H:i:s', strtotime($this->end_time));


   





         if (parent::beforeSave($insert)) {


        return true;


    } else {


        return false;


    }


}

the am / pm (php:H:i:s P <- the P) part of this is your problem. I ran into this awhile back. I’ll see if i can dig up the fix. It was super easy and just some widget settings.

update found my datetime picker it’s different widget but same concept and i used a behavior to handel it.

The behavior handles the save and the view of it for the form. edit it accordingly to handle just time for your situation but it will do what you need it to do with some tweeks. I didn’t want to edit it because i know all of the below code works.




  <?=

    	$form->field($model, 'date')->widget(DateTimePicker::className(), [

        	'readonly' => true,

        	'pluginOptions' => [

            	'minView' => 0,

            	'maxView' => 6,

            	'minuteStep' => 5,

            	'autoclose' => true,

            	'showMeridian' => true,

            	'startDate' => date('M jS, Y H:i:s'),

            	'todayBtn' => true,

            	'autoclose' => true,

            	'todayHighlight' => true,

            	'format' => 'MM dd, yyyy HH:ii P',

        	],

        	'options' => [

            	'placeholder' => 'Select Date with Calendar',

        	],

    	]);

    	?>



model




  public function behaviors() {

    	return [


        	'FormatDate' => [

            	'class' => 'common\components\behaviors\FormatDate',

            	'attributes' => ['date'],

            	'viewformat' => 'M d, Y g:iA',

        	],

    	];

	}



the behavior




<?php


namespace common\components\behaviors;


use yii\base\Behavior;

use yii\db\ActiveRecord;

use yii\db\Expression;

use yii\helpers\Html;


//'FormatDate' => [

//	'class' => 'common\components\behaviors\FormatDate',

//	'attributes' => ['created_at', 'updated_at'],

//	'saveformat' => 'Y-M-d h:i:s',//defults to new Expression('NOW()')

//	'saveAsMySql'=>false,//must be set to false to change save format;

//	'viewformat' => 'Y-M-d h:i:s'

//]

/*

 * This is a behavior for formatting dates for display and saving in MySql.

 *

 * @property boolean $saveAsMySql

 * @property string $saveformat

 * @property string $viewformat

 * @property string $attributes

 */


class FormatDate extends Behavior {


	public $attributes;

	public $viewformat = 'Y-m-d H:i:s';

	public $saveformat = 'Y-m-d H:i:s';

	public $saveAsMySql = TRUE;


	public function events() {

    	return [

        	ActiveRecord::EVENT_BEFORE_INSERT => 'beforeSave',

        	ActiveRecord::EVENT_BEFORE_UPDATE => 'beforeSave',

        	ActiveRecord::EVENT_AFTER_FIND => 'afterFind',

    	];

	}


	public function beforeSave($event) {

    	foreach ($this->attributes as $attribute) {

   		if (isset($this->owner->$attribute) && !empty($this->owner->$attribute) && $this->owner->$attribute != null) {

        	$this->owner->$attribute = (!$this->saveAsMySql) ? date($this->saveformat, strtotime($this->owner->$attribute)) : date('Y-m-d H:i:s', strtotime($this->owner->$attribute));

        	}

    	}

	}


	public function afterFind($event) {

    	foreach ($this->attributes as $attribute) {

        	//check to see if data is set in database!

        	if (isset($this->owner->$attribute) && !empty($this->owner->$attribute) && $this->owner->$attribute != null) {

        	//if date is set then format date!

            	$this->owner->$attribute = date($this->viewformat, strtotime($this->owner->$attribute));

        	} else {

            	$this->owner->$attribute = 'Not Set';

        	}

    	}

	}


}