没有不值得去解决的问题,也没有不值得去学习的技术!

How to Add Time Selection to CJuiDatePicker in Yii

The implementation effect after adding hours, minutes, and seconds!
The initial implementation effect!
The initial implementation effect!

The initial code:

$this->widget(‘zii.widgets.jui.CJuiDatePicker’, array(
‘name’=>’mendtime’,
‘value’=>date(‘Y-m-d’, time()),
‘language’=>Yii::app()->getLanguage(),
‘options’=>array(
‘showAnim’=>’slideDown’,
‘changeYear’=>true,
‘changeMonth’=>true,
‘showWeek’=>true,
‘autoSize’=>true,
‘dateFormat’=>’yy-mm-dd’,
‘maxDate’=>’0’,
‘minDate’=>’-10d’,
),
‘htmlOptions’=>array(
‘readonly’=>’readonly’,
‘class’=>’txt’,
‘style’=>’width:200px’
)
));

The final implementation effect:

The implementation effect after adding hours, minutes, and seconds!
The implementation effect after adding hours, minutes, and seconds!

Implementation process:

1. Download the extension from the official Yii website: http://www.yiiframework.com/extension/datetimepicker/

2. Copy the CJuiDateTimePicker folder to the protected\extensions directory;

3. Download the language pack from https://github.com/trentrichardson/jQuery-Timepicker-Addon, because the Yii extension does not include a language pack;

4. In the file CJuiDateTimePicker.php, find: $cs->registerScriptFile($assets.self::ASSETS_NAME.’.js’,CClientScript::POS_END); and add the following line below it:

$cs->registerScriptFile($assets.’/i18n/jquery-ui-timepicker-‘ . $this->language . ‘.js’,CClientScript::POS_END);, to load the language pack;

5. Code in the view:

<?php
Yii::import(‘application.extensions.CJuiDateTimePicker.CJuiDateTimePicker’);
$this->widget(‘CJuiDateTimePicker’, array(
‘name’=>’mendtime’,
‘value’=>date(‘Y-m-d H:m:s’, time()),
‘language’=>Yii::app()->getLanguage(),
‘options’=>array(
‘showAnim’=>’slideDown’,
‘changeYear’=>true,
‘changeMonth’=>true,
‘showWeek’=>true,
‘autoSize’=>true,
‘dateFormat’=>’yy-mm-dd’,
‘timeFormat’=>’hh:mm:ss’,
‘maxDate’=>’0’,
‘minDate’=>’-10d’,
),
‘htmlOptions’=>array(
‘readonly’=>’readonly’,
‘class’=>’txt’,
‘style’=>’width:200px’
)
));
?>

Notes:

1. $this->language must match the one in the js file. It defaults to zh-CN, but if $this->language is set to zh_cn, you must update all instances to match $this->language; otherwise, it will not run correctly;

2. The date and time format settings are different from the date function in PHP. Instead, they are:

‘dateFormat’=>’yy-mm-dd’,
‘timeFormat’=>’hh:mm:ss’,

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系