How to change mysql timezone with phpmyadmin on AppFog - php

My app is on appfog (AWS asia). I want to change default timezone to get correct time when using DEFAULT_TIMESTAMP.
I tried
SET GLOBAL time_zone = "Asia/Calcutta";
But It gives error
#1227 - Access denied; you need (at least one of) the SUPER privilege(s) for this operation

In the AppFog infrastructure, many users, in many timezones, all share the same database engine. Because of this, individual users are not allowed to change the global timezone setting for all the different databases running on a given server.
However, if you read http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html you will see that the time zone can be changed for a given connection with SET time_zone = <timezone>, so if you wrap your mysql statements in that, you will have less timezone conversion logic to do.

Wouldn't it be better to save everything in UTC/GMT time and then add time zone difference while displaying the time?
This way your time data will remain correct even if there is some change in server timezone while moving to some other service.

Related

MySQL timestamp showing in wrong timezone

I am saving a timestamp in my MySQL database with the creation time of a record. When fetching it using CodeIgniter (a PHP framework) it shows in UTC while my timezone is UTC+2. I think that timestamps are stored always in UTC, that's ok, but I don't know how to display it in UTC+2. Same CodeIgniter application in local shows in UTC+2, probably because my computer is in UTC+2 while my server is in UTC.
Is it possible to change a timestamp to another timezone using PHP or a CodeIgniter function, without changing the timezone of either my MySQL server or the web server itself? (I am using a shared hosting, that's why changing the timezone is probably not possible).
Thank you!
You can set the MySQL time zone for your session with something like
SET time_zone = 'Europe/Berlin';
If you give this command immediately after you make your MySql connection each time, you'll always see your TIMESTAMP values rendered in your local time. This uses the so-called zoneinfo database, which is kept current with temporopolitical changes.
The list of zone names is here.
You don't have to change MySQL's global time zone setting to do this.

Syncing MySQL and PHP times

I am trying to sync my PHP and MySQL times. Currently the times are different (outputs below):
'SELECT CURRENT_TIMESTAMP' returns '2013-03-16 13:14:28'
'date('Y-m-d g:i:s')' returns '2013-03-16 10:14:28'
I have had success, reading from other users, on changing the MySQL time using SET time_zone='-7:00';. The problem is the time will be changing for different queries based on the user's location and I would rather not change the time_zone for every query.
Should I just set the time_zone once, making it the same time as the server, and then change the time based on the user's timezone in PHP? Or is there a better way to go about this?
Thank you in advance!
Should I just set the time_zone once, making it the same time as the server, and then change the time based on the user's timezone in PHP? Or is there a better way to go about this?
Yes. You also ought not to set timezones via their GMT zone because of daylight savings time. You can write yourself functions to do this conversion by setting date_default_timezone_set('UTC'); and then to your user's timezone.
$time = strtotime('2013-03-16 13:14:28'.' UTC');
echo date('Y-m-d g:i:s',$time); //2013-03-16 9:14:28
Another alternative is to store mysql times in "Unix Time" and then convert only once with PHP.
You definitely want to use a single timezone in your application/database. Only change the timezone of the data (not in the db or your php default tz) if you have to like when displaying to the user - and then only do that right before you about to display it. otherwise, use a single timezone. I prefer GMT
to set the mysql server timezone: in my.conf
have somehthing like this: default-time-zone='GMT'
and then restart mysql
see: http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
For php, somewhere at the top of each page you should execute this line:
date_default_timezone_set("GMT");

Issues with MySQL (phpmyadmin) time zones

I have a website stored on a shared server and the timezone is set based on the operating system of the server, which is two hours ahead.
I changed my php.ini and tried:
SET time_zone='-7:00'
SET time_zone='Canada/Mountain'
In MySQL but it does not work. Is there a way around this? To override or set the timzone?
My hosting company support said I should run the "SET time_zone" at the start of every mysql session, but I dont understand what they mean and they are very vague about it.
You need to do it just after you connected with your database.
For EX (This is just example if you are using PDO you can change it accordingly):
mysql_query("SET time_zone='-7:00'");
I would also like to mention here the +/- sign is essential — even for zero.
Also from Mysql docs:
"The current session time zone setting affects display and storage of time values that are zone-sensitive.This includes the values displayed by functions such as NOW() or CURTIME(), and values stored in and retrieved from TIMESTAMP columns.
The current time zone setting does not affect values displayed by functions such as UTC_TIMESTAMP() or values in DATE, TIME, or DATETIME columns."
Hope this help !
I had similar problems with time and date when i move from php4 to php5, all my time function stop work. Solution is to put date_default_timezone_set('America/Los_Angeles'); in first line of code.

Timezone settings in MySQL - Using NOW()?

SOmewhat related to Doing calculations in MySQL vs PHP
Right now, our database assumes that the system time is in UTC and uses that to calculate NOW(). PHP explicitly sets the timezone as UTC (so its impervious to server time zone shifts).
An accidental shift of timezones on the server messed this relationship up at the database level and i'm now trying to figure out the ideal congiguration:
configure Mysql to be in UTC, but also from the perspective that:
our application may be on someone else's server where they might have a different TZ (so i cant set the timezone at the mysql/server level). How do i configure it at the specific database level?
You should check out UTC_TIMESTAMP(); this ensures that a timestamp field is always UTC in the database. Have your PHP script do the offset from the users browser information. This can also be done using DATE_ADD/DATE_SUB or better CONVERT_TZ().
Have PHP set it before you store it in the database. Have it set to GMT and then calculate when displaying based on the users timezone as needed.

Dealing with PHP server and MySQL server in different time zones

For those of us who use standard shared hosting packages, such as GoDaddy or Network Solutions, how do you handle datetime conversions when your hosting server (PHP) and MySQL server are in different time zones?
Also, does anybody have some best practice advice for determining what time zone a visitor to your site is in and manipulating a datetime variable appropriately?
As of PHP 5.1.0 you can use date_default_timezone_set() function to set the default timezone used by all date/time functions in a script.
For MySql (quoted from MySQL Server Time Zone Support page)
Before MySQL 4.1.3, the server operates only in the system time zone set at startup. Beginning with MySQL 4.1.3, the server maintains several time zone settings, some of which can be modified at runtime.
Of interest to you is per-connection setting of the time zones, which you would use at the beginning of your scripts
SET timezone = 'Europe/London';
As for detecting the client timezone setting, you could use a bit of JavaScript to get and save that information to a cookie, and use it on subsequent page reads, to calculate the proper timezone.
//Returns the offset (time difference) between Greenwich Mean Time (GMT)
//and local time of Date object, in minutes.
var offset = new Date().getTimezoneOffset();
document.cookie = 'timezoneOffset=' + escape(offset);
Or you could offer users the chioce to set their time zones themselves.
Store everything as UTC. You can do conversions at the client level, or on the server side using client settings.
php - date
mysql - utc-timestamp
RE the answer from Željko Živković, timezone descriptors like 'Europe/London' only work if the mySQL admin has added the timezone tables to the system, and keeps them updated.
Otherwise you are limited to numeric offsets like '-4:00'. Fortunately the php date('P') format provides it (as of 5.1.3)
So in say an app config file you might have
define('TZ', 'US/Pacific');
....
if (defined('TZ') && function_exists('date_default_timezone_set')) {
date_default_timezone_set(TZ);
$mdb2->exec("SET SESSION time_zone = " . $mdb2->quote(date('P')));
}
This means PHP and mySQL will agree on what timezone offset to use.
Always use TIMESTAMP for storing time values. The column is actually stored as UNIX_TIME (epoch) but implicitly converted from current time_zone offset when written, and back when read.
If you want to display times for users in other time zones, then instead of a global define(), set their given timezone in the above. TIMESTAMP values will be automatically converted by mySQL by the time your app sees the result set (which sometimes can be a problem, if you need to actually know the original timezone of the event too then it needs to be in another column)
and as far as, "why not just store all times as int's", that does lose you the ability to compare and validate dates, and means you always have to convert to date representation at the app level (and is hard on the eyes when you are looking at the data directly - quick, what happened at 1254369600?)
I save all my dates as a bigint due to having had issues with the dateTime type before. I save the result of the time() PHP function into it, now they count as being in the same timezone :)
In php set timezone by in the php.ini file:
ini_set("date.timezone", "America/Los_Angeles");
or in particular page you can do like:
date_default_timezone_set("America/Los_Angeles");
In mysql you can do like:
SET GLOBAL time_zone = 'America/Los_Angeles';

Categories