Timezone in Yii2 - php

My computer time zone 'Asia/Tashkent'. And I set Yii2 time zone to 'Asia/Tashkent' too in config. But Yii2 is still displaying time incorrectly. I am wondering what I have missed?
$time = date("Y-m-d H:i:s", time());
echo $time; // 2018-03-07 14:10:57
echo Yii::$app->formatter->asTime($time, 'medium'); // 7:10:57 PM
echo Yii::$app->formatter->asDate($time, 'medium'); // Mar 7, 2018
echo date_default_timezone_get(); // Asia/Tashkent

If you are giving asTime() string date formatter assumes it's in default timezone which by default in Yii 2 is UTC.
If you want this to be the same as the output of PHP date() change the default timezone in Yii to yours. For example in the configuration:
'components' => [
'formatter' => [
'class' => 'yii\i18n\Formatter',
'defaultTimeZone' => 'Asia/Tashkent',
],
],

Related

Laravel - dateformat validation not working as expected

My code:
$data['from'] = '2020-03-20 20:30:00';
$data['to'] = '2020-03-21 00:45:00';
$validity = \Validator::make($data, [
'from' => ['date_format:Y-m-d H:i:s'],
'to' => ['date_format:Y-m-d H:i:s']
]);
// This if gets true, and the error message is:
The to does not match the format Y-m-d H:i:s
if($validity->fails()) {
dd($validity->errors());
}
and amazingly when I change 00:45:00 to 01:45:00 it doesn't get inside that if. How can I fix it?
Since your locale is set up to be Asia/Tehran the time 2020-03-21 00:45:00 is actually not valid. According to https://www.timeanddate.com/time/change/iran/tehran:
Saturday, 21 March 2020, 00:00:00 clocks are turned forward 1 hour to
Saturday, 21 March 2020, 01:00:00 local daylight time instead.
This means that times between 00:00:00 and 01:00:00 never occur. Since the Laravel date validator internally uses PHP's date parsing then that parsing fails and the error is raised. If you want to check only if a date is a given format ignoring any daylight savings quirks then you can use date_default_timezone_set to temporarily set the locale to one that does not observe daylight savings.
Try
$validity = \Validator::make($data, [
'from' => 'date_format:Y-m-d H:i:s',
'to' => 'date_format:Y-m-d H:i:s'
]);

Yii2: display dates in user time zone

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

How to format date from database in Yii2

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');

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

odd behavior of strftime in ZF2

I'm trying to show a localized date with strftime but it does not work.
class ExampleController extends AbstractActionController
{
public function indexAction()
{
$openDate = DateTime::createFromFormat(...);
setlocale(LC_ALL, Locale::getDefault());
Debug::dump(Locale::getDefault()); // shows 'fr_FR'
Debug::dump(strftime('%B %Y', $openDate->getTimestamp())); // shows 'August 2013' instead of 'Août 2013'
}
}
In module/Application/config/module.config.php
return array(
...
'translator' => array(
'locale' => 'fr_FR',
'translation_file_patterns' => array(
array(
'type' => 'gettext',
'base_dir' => __DIR__ . '/../language',
'pattern' => '%s.mo',
),
),
),
...
);
Is someone can tell me why the month is not translated in French ?
strftime has nothing to do with ZF2 or the intl php extension. However ZF2 does come with a DateFormat view helper that should solve your problem. The full documentation is available at:
http://framework.zend.com/manual/2.2/en/modules/zend.i18n.view.helpers.html#dateformat-helper
A quick example:
Debug::dump($this->dateFormat($openDate->getTimestamp(), IntlDateFormatter::LONG));
The default locale for the DateFormat view helper is that returned by Locale::getDefault() therefore it should return the date in french as you require.
To use your custom format:
Debug::dump($this->dateFormat($openDate->getTimestamp(), IntlDateFormatter::LONG, IntlDateFormatter::LONG, null, "dd LLLL Y - HH:mm"));
According what Tomdarkness said, I found a way to do what I wanted. I used IntlDateFormatter :
$formatter = \IntlDateFormatter::create(
\Locale::getDefault(), // fr_FR
\IntlDateFormatter::FULL,
\IntlDateFormatter::FULL,
'Europe/Paris',
\IntlDateFormatter::GREGORIAN,
'dd LLLL Y - HH:mm'
);
echo $formatter->format($openDate); // shows '20 août 2013 - 14:39'
I think maybe I will code my own dateFormat view helper based on this.

Categories