Yii2 Date Components and time components _php tips

Source: Internet
Author: User

date component, time component in peacetime development is essential." Today we will talk about in the yii2 of the super useful time components, but also the province of everyone to find a variety of JS plug-ins.

> Before we share, we'll preview the effect and see how good it is.
Of course, good use is not in the sense of the ego, light to look at the picture above is not felt. To tell you the good news, these two plug-ins have been integrated with the YII2, the use of the ash is often simple oh.
about the date component and the time component, the former is the date (' y-m-d ') type, the latter is the date (' y-m-d h:i:s ') type, which naturally doesn't need to be said.
Let's take a look at the time component Extensions
Now that you are extending the build, the first step is, of course, installation.
compo Ser require kartik-v/yii2-widget-datetimepicker "*"

Once installed, if your form is generated using ActiveForm, you can refer to the following code

Use Kartik\datetime\datetimepicker; 
<?= $form->field ($model, ' Created_at ')->widget (Datetimepicker::classname (), [ 
 ' Options ' => [] Placeholder ' => '], 
 ' pluginoptions ' => [ 
  ' AutoClose ' => true, 
  ' Todayhighlight ' => true, 
 ] 
]); ?>

Some students who do not like to use ActiveForm to generate forms will need to refer to the following usage.

Use Kartik\datetime\datetimepicker; 
Echo ' <label> time </label> '; 
Echo datetimepicker::widget ([ 
 ' name ' => ' article[created_at] ', 
 ' options ' => [' placeholder ' => '], 
 //Note that when this method is updated you will need to specify value ' 
 => ' 2016-05-03 22:10:10 ', 
 ' pluginoptions ' => [  ' autoclose ' = > True, 
  ' format ' => ' Yyyy-mm-dd HH:ii:ss ', 
  ' todayhighlight ' => true 
 ] 
];

As for which convenient, it goes without saying.

The entire installation process is really simple, and we next install the date component in the same way.

Composer require kartik-v/yii2-widget-datepicker "@dev"

Once installed, we started using the date component.

Use Kartik\date\datepicker; 
<?php echo datepicker::widget ([ 
 ' name ' => ' article[created_at] ', 
 ' options ' => [' placeholder ' => '] ... '], 
 //value value updates need to add 
 ' value ' => ' 2016-05-03 ', 
 ' pluginoptions ' => [ 
  ' AutoClose ' => true, 
  ' format ' => ' Yyyy-mm-dd ', 
  ' todayhighlight ' => true, 
 ] 
];?>

Looking at the above code, presumably some students have guessed the use of the date component generated for ActiveForm. Yes, it is so used, si not si very simple.

<?= $form->field ($model, ' Created_at ')->widget (Datepicker::classname (), [ 
 ' Options ' => [] Placeholder ' => '], 
 ' pluginoptions ' => [ 
  ' AutoClose ' => true, 
  ' Todayhighlight ' => true, 
  ' Format ' => ' Yyyy-mm-dd ', 
 ] 
];?>

Of course, the above time format can be adjusted, but the date component, since it is the date, it is certainly only support ymd three types of parameters, or the time component and what difference.

Some small partners say why their own English, it is inconvenient to use it, I would like to ask a question, your project is not configured language is Chinese? Have not configured the classmate only need to add ' language ' => ' ZH-CN ' in your configuration file, can.

Finally, let's look at the final effect chart.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.