jui/datepicker dateformat issue - php

I am using \yii\jui\DatePicker in my view.
when I provide input as 31/10/2017 it takes it as 31/Oct/2017.
However, when I provide input as 01/10/2017 it takes it as 10/Jan/2017 instead of 01/oct/2017 .
How can I specify input date format.
My code for it is:
echo $form->field($model, 'target_end_date')->widget(\yii\jui\DatePicker::classname(), [
'dateFormat' => 'php:d/M/Y',
'options'=>['style'=>'width:250px;', 'class'=>'form-control','readOnly'=>'readOnly', 'placeholder'=>'Select end date.']
]);

dateFormat is no longer the part of clientOptions and should be specified as follows:
<?= $form->field($model, 'target_end_date')->widget(\yii\jui\DatePicker::className(), [
'dateFormat' => 'php:d-m-Y',
]); ?>
Or, an alternative in ICU format:
'dateFormat' => 'dd/MM/yyyy',
See official docs for https://www.yiiframework.com/extension/yiisoft/yii2-jui/doc/api/2.0/yii-jui-datepicker#$dateFormat-detail property.

Simply Change your code
echo $form->field($model, 'target_end_date')->widget(\yii\jui\DatePicker::classname(), [
'dateFormat' => 'php:d/M/Y',
'options'=>['style'=>'width:250px;', 'class'=>'form-control','readOnly'=>'readOnly', 'placeholder'=>'Select end date.']
]);
To
echo $form->field($model, 'target_end_date')->widget(\yii\jui\DatePicker::classname(), [
'dateFormat' => 'php:d/m/Y',
'options'=>['style'=>'width:250px;', 'class'=>'form-control','readOnly'=>'readOnly', 'placeholder'=>'Select end date.']
]);
And for more date related formate refer below link.
http://php.net/manual/en/function.date.php

Related

Yii2: how to format a value in ActiveField?

On my form I have this:
echo $form->field($model, 'amount')->textInput();
If the value coming from the database is 42.5 I want to have it formatted with two decimals in the input field like this 42.50. How do I do that?
I don't find any info about formatting in ActiveField docs...
You could use formatter functionalities
$form->field($model, 'amount',
['inputOptions' => ['value' => Yii::$app->formatter->asDecimal($model->amount)]])
http://www.yiiframework.com/doc-2.0/yii-i18n-formatter.html
http://www.yiiframework.com/doc-2.0/guide-output-formatting.html
As an alternative to the #scaisEdge answer, you can also override/modify the value in the ActiveField renderer.
Here's an example for an ActiveField::textInput()
<?= $form->field($model, 'vat_rate')
->textInput([
'maxlength' => true,
'placeholder' => '0.00',
'value' => (float) $model->vat_rate * 100,
]) ?>

Yii2 kartik-datecontrol timepicker Update

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).

Yii2 - jui datepicker extension - display current timestamp by default

i'm using Yii2 extension - Jui DatePicker.
Is there a way to show by default the current timestamp in the text box, so the user does not need to input it every time it fills de form fields?
My view, and the form in Yii2:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
'inline' => false,
'clientOptions' =>[
'dateFormat' => 'yyyy-MM-dd',
'showAnim'=>'fold',
'yearRange' => 'c-25:c+0',
'changeMonth'=> true,
'changeYear'=> true,
'autoSize'=>true,
'showOn'=> "button",
'buttonText' => 'clique aqui',
//'buttonImage'=> "images/calendar.gif",
]])
?>
In Yii1 i used to write the code like this (inside the widget), and the timestamp appeared by default:
'htmlOptions'=>array('size'=>12, 'value'=>CTimestamp::formatDate('d/m/Y')),
Is there an equivalent in Yii2?
Many thanks...
You can still use value:
<?= $form->field($model, 'data')->widget(DatePicker::className(),
[
'language' => 'en',
...
'value' => date('Y-m-d'),
As noted in a comment (haven't confirmed this)
'value' => date('Y-m-d') parameter will work only if widget is used without model: DataPicker::widget(['value' => date('Y-m-d')])
Therefore you can use clientOptions to set defaultDate :
'clientOptions' => ['defaultDate' => date('Y-m-d')]
Or better yet you can set $model->data to a default value of the current timestamp, either in your view or in the model.
if (!$model->data) $model->data = date('Y-m-d');

Yii2: Correct Way to use Jui DatePicker

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() ?>

how to disable previous date in yii datepicker

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(),

Categories