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';
}
}
}
}