Home php教程 php手册 yii2超好用的日期组件和时间组件 - 白狼栈

yii2超好用的日期组件和时间组件 - 白狼栈

May 20, 2016 am 10:14 AM

作者:白狼 出处:http://www.manks.top/yii2_datetimepicker.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。原文有图片。
日期组件,时间组件在平时开发中是必不可少的。今天我们就来谈谈在yii2中的超好用的时间组件,也省的大家各种找js插件了。

分享之前我们先预览下效果,看看到底怎么个好用法。
当然啦,好用不好用在于自我的感觉,光看上面的图片是感受不到的。再告诉你个好消息,这两款插件已经跟yii2整合了,使用起来也是灰常的简单哦。
关于日期组件跟时间组件,前者是date('Y-m-d')类型,后者是date('Y-m-d H:i:s')类型,自然不用多说。
我们先来看看时间组件扩展
既然是扩展组建,第一步当然是安装。

composer <span style="color: #0000ff;">require</span> kartik-v/yii2-widget-datetimepicker "*"
Copy after login

 

安装好之后,如果你的表单使用ActiveForm生成的,可以参考下面的代码

<span style="color: #0000ff;">use</span><span style="color: #000000;"> kartik\datetime\DateTimePicker; 
</span>= <span style="color: #800080;">$form</span>->field(<span style="color: #800080;">$model</span>, 'created_at')->widget(DateTimePicker::classname(),<span style="color: #000000;"> [ 
    </span>'options' => ['placeholder' => ''], 
    'pluginOptions' =><span style="color: #000000;"> [ 
        </span>'autoclose' => <span style="color: #0000ff;">true</span>, 
        'todayHighlight' => <span style="color: #0000ff;">true</span>,<span style="color: #000000;"> 
    ] 
]); </span>?>
Copy after login

 

有些不喜欢用ActiveForm生成表单的同学就需要参考下面的用法了

<span style="color: #0000ff;">use</span><span style="color: #000000;"> kartik\datetime\DateTimePicker; 
</span><span style="color: #0000ff;">echo</span> '<label>时间</label>'<span style="color: #000000;">; 
</span><span style="color: #0000ff;">echo</span> DateTimePicker::<span style="color: #000000;">widget([ 
    </span>'name' => 'Article[created_at]', 
    'options' => ['placeholder' => ''], 
    <span style="color: #008000;">//</span><span style="color: #008000;">注意,该方法更新的时候你需要指定value值 </span>
    'value' => '2016-05-03 22:10:10', 
    'pluginOptions' =><span style="color: #000000;"> [
        </span>'autoclose' => <span style="color: #0000ff;">true</span>, 
        'format' => 'yyyy-mm-dd HH:ii:ss', 
        'todayHighlight' => <span style="color: #0000ff;">true</span><span style="color: #000000;"> 
    ] 
]);</span>
Copy after login

 

至于哪个方便,不言而喻。

整个安装过程确实很简单,我们接下来以同样的方式进行安装日期组件。

继续阅读

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1272
29
C# Tutorial
1251
24