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.
Related
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.
What is the best way to store date and time in the database? I tried using now() on localhost: it works, but on live server it counts far back as 8 hours ago and current timestamp does the same thing. What is the most accurate approach?
$d="INSERT INTO `users`(`time`) VALUES (now())";
You are storing the time in the most accurate method. It might not be in the correct timezone, but it is (as far as the server can tell) exactly that time when saved. If the timezone is incorrect, you will have to tell MySQL which timezone is considered "local" by executing something like SET time_zone = 'YOUR_TIME_ZONE' as soon as you open the connection.
A good trick to help with dealing with times is to make your storage layer only deal with times in UTC, and translate it to the local timezone in the presentation layer.
The only thing that you had wrong was your time zone
For example, use this :
SET time_zone = 'America/New_York';
with your time zone
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 have one website which does not use US timings. Is it possible to change PHP and MySQL timings for one website? If so what do I need to do?
you can do it per connection , when you start connection to this website set it,
look at :
http://dev.mysql.com/doc/refman/5.6/en/time-zone-support.html
#
Per-connection time zones. Each client
that connects has its own time zone
setting, given by the session
time_zone variable. Initially, the
session variable takes its value from
the global time_zone variable, but the
client can change its own time zone
with this statement:
mysql> SET time_zone = timezone;
but be aware that :
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. Values for
TIMESTAMP columns are converted from
the current time zone to UTC for
storage, and from UTC to the current
time zone for retrieval.
The current time zone setting does not
affect values displayed by functions
such as UTC_TIMESTAMP() or values in
DATE, TIME, or DATETIME columns. Nor
are values in those data types stored
in UTC; the time zone applies for them
only when converting from TIMESTAMP
values. If you want locale-specific
arithmetic for DATE, TIME, or DATETIME
values, convert them to UTC, perform
the arithmetic, and then convert back.
an example how to set is in :
http://www.electrictoolbox.com/mysql-set-timezone-per-connection
For PHP:
<?php
date_default_timezone_set('Europe/London');
?>
See the list of PHP supported timezones here: http://www.php.net/manual/en/timezones.php
For MySQL you'll need to execute query like this:
SET time_zone = timezone;
See detailed information in the MySQL documentation: http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html
Yes, you should do all time zone conversions inside PHP, not mysql.
For each column in your database which stores a date/time, either
a) Store it in GMT and do the necessary conversion OR
b) Store it in local time always
In neither case does MySQL need to know, but your developers do so be sure that you document this in your schema if you have a mixture.
It is often necessary to store local time date/time values. MySQL does not remember a time zone in a datetime column (nor could it easily; other DBs attempt to in some cases).
If you are storing a precise point in time which has already happened (e.g. now) then you should always store it in UTC to avoid confusion about when exactly it happened. Some apps use epoch / unix time for this purpose.
A future time you may have to store in local time because you can't tell (yet) exactly what time it will have to happen, that may depend on the decision of politicans who might not have been elected yet.
Time zones are tricky things because politicians control them. They tend to change with little warning (typically only a few years)
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';