Yii framework 2.0 asDate() 1 hour earlier - php

Working with Yii framework 2.0 I get records from my database. In each table I have a column named created_at. Let's say created_at = '2015-12-25 01:00:00'. I use the following Yii formatter.
Yii::$app->formatter->asDate($model->created, 'php:d-m-Y h:i:s');
The result is: 25-12-2015 02:00:00. It is 1 hour earlier. I don't know why. What should I configure to fix this?

#Soju: I have tried your solution. I needed to change your answer a little bit since it solves the problem with the following configuration.
'components' => [
'formatter' => [
'defaultTimeZone' => 'Europe/Paris',
],
],

You should set formatter defaultTimeZone and timeZone in your components config, for example if you store UTC in your database and want to display Europe/Paris :
'components' => [
'formatter' => [
'defaultTimeZone' => 'UTC',
'timeZone' => 'Europe/Paris',
],
],
Read more : http://www.yiiframework.com/doc-2.0/guide-output-formatter.html#time-zones

Related

Yii2 framework: How to change English default language to Spanish

I am using the Yii2 Framework and I am translating all texts of buttons, labels, messages, etc.
Then I read this article http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html that shows how to do it automatically but I don't understand it.
I want to translate to Spanish from Argentina: es-AR or at least to any Spanish.
So I think I need to change from en-US to es-AR but I would like to know which files should I change.
Also I am using the great Gii code generator where I can see a checkbox called Enable I18N.
I watched these files but I am not sure if I am looking the right files:
vendor/yiisoft/yii2/base/Application.php
vendor/yiisoft/yii2/i18n/I18N.php
common/config/main-local.php
Add language propery and i18n component in application config. For advanced application template in common/config/main.php
return [
'language' => 'es-AR',
...
'components' => [
...
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
],
],
],
...
],
]
Use Yii::t() for all user messages (model labels, views, error messages etc).
echo \Yii::t('app', 'Friend');
Create directory messages/es-AR. Create file app.php in this directory and add translations
return [
'Friend' => 'Amigo',
'Girl' => 'Сhica',
...
];
Try to look into the official documentation, it is best tutorial for you. http://www.yiiframework.com/doc-2.0/guide-tutorial-i18n.html
Also, look at this answer yii2 basic multiple language
You can change default language by changing 'language' parameter of your main configuration file. Like this:
return
[
// set target language to be English
'language' => 'en-US',
]
Where instead 'en-US' you must to set needed locale code, e.g. 'es-AR'

Yii2 Multiple caching storage

Is it possible to use two or more caching storage in yii2 framework? I already setup a Memcache for my web app but I wanted also to use a FileCache since I will be dealing with a large chunk of data.
hope someone can help. Thanks!
You can set any cache. Just set it at config file.
'components' => [
'cache' => [
'class' => 'yii\caching\FileCache',
],
'memCache' => [
'class' => 'MEMCACHE CLASS HERE',
],
.... ANY cache you want ...
]
You can register multiple cache application components. The component named cache is used by default by many cache-dependent classes (e.g. yii\web\UrlManager).
Official link
'components' => [
'cache' => [
'class' => 'yii\caching\MemCache',
],
'fileCache' => [
'class' => 'yii\caching\FileCache',
]
]

how to setup multiple language in yii2 in simple way

I am working on yii2 framework.This is new framework for me. I want to setup multiple language. I tried some way but didn't got success. Can anyone please suggest me simplest way ? What should i have to do ?
I am using this reference link
http://techisworld.com/working-with-multiple-languages-app-in-yii2-framework-using-i18n-system.html
1- For dynamic content (coming from database) I usually use this:
webvimark/multilanguage
It is very easy and isolated from your app DB tables structure and code, that gives flexibility in adding/removing languages at the long term.
2- For static content (words inside the markup) in frontend as an example:
add the lines in your frontend/config/main.php file,
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\PhpMessageSource',
'basePath' => '#app/messages',
'sourceLanguage' => 'en_US',
'fileMap' => [
'app' => 'app.php'
],
],
],
],
Put you translation file app.php file inside /frontend/messages, as any Yii translation file it returns an array of translations in a key-value pairs.
Then you can translate your static content using:
Yii::t('app', 'text to be translated')

How to automatically insert new translation into database in Yii 2

I just started using Yii 2 and already running into some issues I can't figure out myself.
I set up the translator, to use my DB instead of files like this ( and two tables message and source_message):
'i18n' => [
'translations' => [
'app*' => [
'class' => 'yii\i18n\DbMessageSource',
//'basePath' => '#app/messages',
//'sourceLanguage' => 'en',
],
],
],
I even set the translator parameter to true:
public $forceTranslation = true;
Now I create the text to translate like this:
<?=Yii::t('app', 'Congratulations!');?>
When I check the debug info, I see, that there has been only one query:
SELECT
"t1"."message" AS "message",
"t2"."translation" AS "translation"
FROM
"source_message" "t1",
"message" "t2"
WHERE
t1.id = t2.id AND
t1.category = 'app' AND
t2.language = 'en'
This doesn’t return anything, since there are no translations present. Now, what I can't figure out, how to make the system auto-create a new translation, for the missing translations.
Is such a feature already built in or do I need to create one my self? Because I couldn't find anything on the internet.
And, if there is not, what is the right way to do so?
I found this script by Aleksandr Zelenin that did exactly, what I needed: https://github.com/zelenin/yii2-i18n-module
Component uses yii\i18n\MissingTranslationEvent for auto-add of missing translations to database

data.timezone in fpm/php.ini has no effect

sorry for the mistakes I've made, I'm not Englishman.
Now I'm trying to set my timezone in php.ini correctly (/etc/php5/fpm/php.ini). Whatever I typed opposite date.timezone (UTC, GMT, Europe/Moscow etc) no one is worked. However, when I set timezone by date_default_timezone_set() in my base file everything becomes ok. What do I wrong?
If you are facing problems in changing it via the php.ini file, you would be better off setting it via protected/config/main.php using 'timeZone' => 'UTC',in the returned array.
Should look something like this -
return array(
'timeZone' => 'UTC',
'basePath'=>dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
To do the same for the session cookie lifetime, add the following in the components array-
'components' => array(
...
'session' => array(
'cookieParams' => array(
'lifetime' => 300,
),
),
),

Categories