If I have function like this
protected function getAreaValues($model)
{
return
[
[
$model->getAttributeLabel('EXPDATE'),TbArea::findOne($model->KODE)->EXPDATE
],
];
}
where EXPDATE IS date data
How to format it in d-M-Y ?
I add formatter in web.php in config like this
'formatter' => [
'class' => 'yii\i18n\Formatter',
'nullDisplay' => '-',
'dateFormat' => 'd-M-Y',
'datetimeFormat' => 'd-M-Y H:i:s',
'timeFormat' => 'H:i:s',
],
But still not working
You can format date like below.
echo Yii::$app->formatter->asDate('2017-03-30', 'd-M-Y'); // 30-Mar-2017
try this.
and you can check Yii2 doc
Yii2 Formatters
I think this will work
$DateTime = DateTime::createFromFormat('Y-m-d', $yourOldDateString);
$newDateString = $DateTime->format('d/M/Y');
Related
I have a factory for generating the event
public function definition()
{
return [
'event_type' => $this->faker->randomElement(['class','event']),
'event_name' => $this->faker->sentence(1,2),
'date' => $this->faker->dateTimeBetween($startDate = '+1 days', $endDate = '+5 days'),
'start_time' => $this->faker->dateTimeBetween('+0 hours', '+2 hours'),
'end_time' => $this->faker->dateTimeBetween('+3 hours', '+10 hours'),
];
}
Here I need start_time and date to have the same value generated in date
For example if date='2003-03-15 02:00:49'. I wanted the start_time to be 02:00:49 but I get some other value because it is generated randomly.
Note: date is a timestamp and start_time is in time format in database
You can re-use faker data by putting it into a variable first and get the timestamp of a DateTime object by calling its getTimestamp() method:
public function definition()
{
$date = $this->faker->dateTimeBetween($startDate = '+1 days', $endDate = '+5 days');
return [
'event_type' => $this->faker->randomElement(['class','event']),
'event_name' => $this->faker->sentence(1,2),
'date' => $date->getTimestamp(),
'start_time' => $date,
'end_time' => $this->faker->dateTimeBetween('+3 hours', '+10 hours'),
];
}
. 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
In my DB I store all datetime fields in UTC format. Also, I have the ability to change default time zone by users. Each user can have own time zone, different from UTC.
How shall I display all model datetime fields in this case?
I have an idea. To do this action for each ActiveRecord model:
public function init()
{
parent::init();
$this->on(ActiveRecord::EVENT_AFTER_FIND, function($event) {
$this->created_date = (new \DateTime('now', new \DateTimeZone("Europe/Kiev")))->format("Y-m-d H:i:s");
});
}
But I'm not sure it's the best way for big amount of models...
if the dates are stored in UTC why not append the string UTC along the time and display it
$time = strtotime($this->created_at.' UTC');
date("Y-m-d H:i:s", $time);
Your code will look like this
public function init()
{
parent::init();
$this->on(ActiveRecord::EVENT_AFTER_FIND, function($event) {
$time = strtotime($model->create_at.' UTC');
$this->created_date = date("Y-m-d H:i:s", $time);
});
}
if I would do it I would just create a separate Helper and use it to display the date in the local format rather than EVENT_AFTER_FIND
Another alternative is to use this Extension
Search for frontend/config/main.php
Try putting in
return [
...
'components' => [
...
a FORMATTER part like
'formatter' => [
'dateFormat' => 'dd/MM/yyyy',
'datetimeFormat' => 'dd/MM/yyyy H:i:s',
'timeFormat' => 'H:i:s',
'locale' => 'it-IT',
'decimalSeparator' => ',',
'thousandSeparator' => '.',
'currencyCode' => 'EUR',
'numberFormatterSymbols' => [
NumberFormatter::CURRENCY_SYMBOL => '€',
],
'timeZone' => 'Europe/Rome',
],
Set your parameters like TimeZone, Currency, etc...
NB: I dont remember but maybe the NumberFormatter part need some other setup so delete the numberFormatterSymbols part if it give to you an error
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).
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() ?>