简体   繁体   中英

Timezone: Mysql save UTC and PHP read America/Sao_Paulo

I would like to save at mysql in UTC timezone and read with PHP using America/Sao_Paulo timezone.

Im using YII framework. Its not working property, how could I do that?

main.php:

'timezone' => 'America/Sao_Paulo',

'db'=>array(
...
    'initSQLs'=>array("SET time_zone = '+00:00'"),
...
)

Try this on your mysql console or gui:

mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

Then use again:

SET time_zone='America/Sao_Paulo';

You also can change by this:

SET time_zone = 'Brazil/West';

Source: http://blog.will.eti.br/2013/mysql-unknown-or-incorrect-time-zone-americasao_paulo/

Do this in your PHP code:

date_default_timezone_set('UTC');

Now PHP operates in UTC. It's up to you to convert to user local times but you have a common ground.

And in MySQL use UTC_TIMESTAMP() for your DATETIME needs. It uses UTC time regardless of the MySQL settings so you don't need to worry anymore.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM