. Good day! Please tell me a technical question, in GridView Yii2, code:
['attribute' => 'request_date_create',
'value' => function ($model) {
return Yii::$app->formatter->asDateTime($model->request_date_create, 'php: d.m.Y H:i:s');
},
],
Does it display the date 3 hours later than the one taken from the database? And this code displays normal :
['attribute' => 'request_date_create',
'value' => function ($model) {
return Yii::$app->formatter->asDateTime(strtotime($model->request_date_create), 'php: d.m.Y H:i:s');
},
],
And this code generally displays nonsense :
[
'attribute' => 'request_date_create',
'format' => ['date', 'php: d.m.Y H:i:s']
],
Why? There is some nuance, but I cannot understand what
[
'attribute' => 'request_date_create',
'format' => ['datetime', 'php:d.m.Y H:i:s']
],
Try to use datetime format and remove whitespace after php:
Looks like PHP time zone is different. See docs https://www.yiiframework.com/doc/api/2.0/yii-i18n-formatter#$defaultTimeZone-detail and example https://www.yiiframework.com/wiki/684/save-and-display-datetime-fields-in-different-formats-in-yii2#tip-3-controlling-global-formats how to set formater time zone, date and time formats
Related
I am trying to convert a date from yyyy-mm-dd to mm-dd-yyyy Yii2 GridView; however I don't know how the date function requires a timestamp, and I can't get a timestamp from this string.
[
'attribute' => 'created_at',
'format' => ['datetime', 'php:m/d/Y H:i:s']
],
I am using a DateControl widget by Kartik in my Yii2-powered system. The widget correctly saves the time I've selected. However, when I tried to update the data, it just always shows "12:30" as the time and not the time from the database. I am still new to Yii2 and I there's not much information on the Internet regarding this issue. Thank you for the help!
Code for my form:
<?= $form->field($model, 'class_start_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
<?= $form->field($model, 'class_end_time')->widget(DateControl::classname(), [
'type'=>DateControl::FORMAT_TIME,
])
?>
Code for the config:
'displaySettings' => [
Module::FORMAT_DATE => 'dd-MM-yyyy',
Module::FORMAT_TIME => 'HH:mm a',
Module::FORMAT_DATETIME => 'dd-MM-yyyy HH:mm:ss a',
],
// format settings for saving each date attribute (PHP format example)
'saveSettings' => [
Module::FORMAT_DATE => 'php:U', // saves as unix timestamp
Module::FORMAT_TIME => 'php:H:i:s',
Module::FORMAT_DATETIME => 'php:Y-m-d H:i:s',
],
I found the solution. The displaySettings should be hh:mm a and not HH:mm a. There is a mismatch in the format which causes the display to be in error when the time is in PM (or greater than 12:00:00).
I have a timestamp column, and I simply want to format it. In GridView I have the following:
[
'attribute' => 'timestamp',
'filter' => false,
'value' => function($model, $key, $index, $column) {
// How to get current timestamp value here???
}
],
Documentation says, $model and $column both return objects, but I still could not find methods that provide column's data. How would I go about this?
You can call any attribute through getter in closure using $model and that will return attribute for current model (according to row in GridView):
[
'attribute' => 'timestamp',
'filter' => false,
'value' => function($model, $key, $index, $column){
return $model->timestamp;
}
],
Obviously such return doesn't make any sense, but you can format it somehow you want. There are some built-in options for date / datetime formatting in Yii2, you can check them in official docs here:
Formatter asDate()
Formatter asDateTime()
I use the following with Yii 1:
[
'attribute' => 'timestamp',
'filter' => false,
'value' => 'Custom::formatDateTime($data->date_stamp)',
],
Obviously formatDateTime is a custom method I created.
My Jui Date Time Picker was working perfectly, I am not sure if I have made any changes in config anywhere, but now the date-picker field is showing the format as if it is asking for time with the date.
This is my code for the date field:
<?= $form->field($model, 'joining_date')->widget(DatePicker::className(),
['clientOptions' =>[
'dateFormat' => 'dd-mm-yyyy',
'showAnim'=>'fold',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
'buttonImage'=> "images/calendar.gif",
'htmlOptions'=>[
'style'=>'width:80px;',
'font-weight'=>'x-small',
],]]) ?>
in my Web.php config the relevant code for dates is like
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Asia/Kolkata',
'dateFormat' => 'php:d-m-Y',
'datetimeFormat'=>'php:d-M-Y H:i:s'
],
Now I am trying to enter the date I am getting the field like 12/07/2014:
and colon after the field as if I need to add the time part, where from it is coming, I couldn't find.
date-picker
Recently I ran into the same problem.
dateFormat is no longer the part of clientOptions and should be specified like that:
<?= $form->field($model, 'joining_date')->widget(DatePicker::className(), [
'dateFormat' => 'php:d-m-Y',
] ?>
Alternative in ICU format:
'dateFormat' => 'dd/MM/yyyy',
See official docs for $dateFormat property.
You may help this...
Yii Default date format is taken from configuration (thats the the power of YII::))
under component you have formate date....something how!!!
'formatter'=>[
'class'=>'yii\i18n\Formatter',
//'dateFormat' =>'MM/dd/yyyy',
'dateFormat' => 'php:d M Y',
'datetimeFormat' => 'php:d-M-Y H:i:s',
'timeFormat' => 'php:H:i:s',
],
use yii\jui\DatePicker;
<?php echo $form->field($ShipmentTypeForm, 'shipmendate')->widget(DatePicker::className(), [
'options' => ['class' => 'form-control input-sm','readOnly'=>'readOnly'],]); ?>
Alternatively use Bootstrap datepicker. You just need to include appropriate JS file and call something like this:
<?= $form->field($model, 'inputName', ['inputOptions'=>['class'=>'form-control datepicker']])->textInput() ?>
I am using datepicker in yii and I want to disable the previous dates on the calendar to avoid picking them.
Here's my code:
$this->widget('zii.widgets.jui.CJuiDatePicker', array(
'model'=>$model,
'attribute'=>'arrival_date_as_per_recorded_travel',
'name'=>'arrival_date_as_per_recorded_travel',
'value'=>$eta_date_formatted,
// additional javascript options for the date picker plugin
'options'=>array(
'startDate'=>date("yy-mm-dd"),
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'2013:2100',),
'htmlOptions'=>array(
'id'=>'arrival_date_as_per_recorded_travel',
'style'=>'height:20px;width:150px',
'value'=>$eta_date_formatted,
'onblur'=>'if(this.value=="")this.value=""'
),
));
As for Yii2 (added this as answer as the question is not tagged under yii 1.x)
<?php
use yii\jui\DatePicker;
?>
<?=
DatePicker::widget([
'name' => 'to_date',
'dateFormat' => 'dd/MM/yyyy',
'clientOptions' => [
'minDate' => 0
]
])
?>
change your options with the following code
'options'=>array(
'startDate'=>date("yy-mm-dd"),
'minDate'=>'0', // this will disable previous dates from datepicker
'showAnim'=>'fold',
'dateFormat'=>'yy-mm-dd',
'changeMonth'=>'true',
'changeYear'=>'true',
'yearRange'=>'2013:2100',),
'minDate'=>'0' will disable the previous dates...
You can take look of Jquery Date Picker API (minDate)
Hope it may help you...
I have tried previous answers but nothing worked.
Here is how i solved this
The minDate is not found in the documentations https://www.malot.fr/bootstrap-datetimepicker/
There is something like minDate no it's called startDate but it accepts only Date.
startDate Date. Default: Beginning of time The earliest date that may be selected; all earlier dates will be disabled. as in Offical documentations.
So here you go.
<?php
$now = new DateTime();
echo $form->field($data['model'], 'expire_date' ,
['template' =>
'{label}<p class="sub-label">' . \Yii::t("main","After this date people won't be able to bid on this job no more.").'</p> {input}{error}{hint}'])
->widget(DateTimePicker::classname(),
[
'options' => [
'placeholder' => \Yii::t("main","Enter a date..."),
'autoComplete' => 'off',
],
//'convertFormat' => true,
'language' => "Yii::$app->language;",
'pluginOptions' => [
'autoclose'=>true,
'format' => 'yyyy-mm-dd hh:ii:ss',
'startDate' => date_format($now, 'Y-m-d'), //startDate Date. Default: Beginning of time The earliest date that may be selected; all earlier dates will be disabled.
]
]);?>
Adding the following to your options array should help.
minDate: new Date(),