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.
Related
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.
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");
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.
I've got a little script I'm working on which saves a comment to a mysql database, the database having a field of type "timestamp" and default set to "CURRENT_TIMESTAMP"
On the client side, I'm using the timeago jquery plugin (http://timeago.yarp.com/) wihch takes a UTC timestamp and converts it to a relative time format like "5 minutes ago"
However, my server time is set in a different time zone. This means that, if I'm posting a comment at 4 o'clock, the timestamp being put in the timestamp field is that of the server time, which is behind my local time. Thus when I'm posting a comment which is only a few minutes old, the jquery timeago plugin is showing something like "four hours ago"
How do I solve this problem. I'm stumped.
Use the UTC_TIMESTAMP function: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_utc-timestamp
All your timestamps should be in UTC, regardless of your server's local time. It's probably being converted somewhere along the way... e.g. as you're retrieving it.
I have all my timestamps stored as unixtime (unsigned integers) and convert them when they need to be displayed.
You must tell mysql to not be dependant on the timezone of your server.
One way to do that is to execute the following command :
mysql> SET time_zone = '+00:00';
this way, your requests with time will all be on the UTC timezone.
check this documentation for further explanations.
your problem comes from the fact that CURRENT_TIMESTAMP gives you the timestamp of the current timezone of the mysql server. If not set, the timezone of the mysql server inherits the timezone of the server.
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';