what is the best way to set the default timezone for php scripts?
I have attempted:
but that doesn't seem to do the trick.PHP Code:
ini_set("date.timezone","America/Chicago");
Any suggestions would be greatly appreciated.
Printable View
what is the best way to set the default timezone for php scripts?
I have attempted:
but that doesn't seem to do the trick.PHP Code:
ini_set("date.timezone","America/Chicago");
Any suggestions would be greatly appreciated.
Hey, Scott
What PHP version we're talking about? I presume PHP5...
You can try also using
bool date_default_timezone_set ( string $timezone_identifier )
Timezones list: PHP: List of timezones in the group America - Manual
Documentation on your issue: PHP: date_default_timezone_set - Manual
However, I'd suggest saving all date\time value in UTC & than add timezone offset before the output. This is better than to write to users from Japan that all times are UTC -6 or something like that.
Let me know how it goes
Alright, I got the php part to work but I am now having trouble with MySQL now().
Here is the code that I have so far:
Per the MySQL docs, I should be able to tell MySQL the timezone that I am in by name, so instead of writing:PHP Code:
echo "<p>PHP 1: ".date("Y-m-d h:i:s")."</p>"; // outputs system time, ok
$q = "select now()";
$r = mysql_query($q);
$d = mysql_fetch_assoc($r);
echo "<p>MySQL 1: ".$d['now()']."</p>"; // outputs system time, ok
putenv("TZ=America/Chicago"); // change php timezone
echo "<p>PHP 2: ".date("Y-m-d h:i:s")."</p>"; // outputs my timezone, ok
$q = "select now()";
$r = mysql_query($q);
$d = mysql_fetch_assoc($r);
echo "<p>MySQL 2: ".$d['now()']."</p>"; // outputs system time, didn't expect this to work for my timezone but why not try
$q = "set time_zone = '-5:00'"; // change mysql timezone
$r = mysql_query($q);
$q = "select now()";
$r = mysql_query($q);
$d = mysql_fetch_assoc($r);
echo "<p>MySQL 3: ".$d['now()']."</p>"; // outputs my timezone, ok
I should be able to write:PHP Code:
$q = "set time_zone = '-5:00'";
or:PHP Code:
$q = "set time_zone = 'America/Chicago'";
However, both of those attempts when using names fail and MySQL complains about them.PHP Code:
$q = "set time_zone = 'US/Central'";
Per MySQL doc:
Named time zones can be used only if the time zone information tables in the mysql database have been created and populated.
Can we get these tables loaded or is there another solution to get now() to work as expected?
No takers?
Am I the only one with this issue?
What are others doing to work around this?
I just use #'s. I only have to report one or two timezones, so I have no need for this sort of complication, so Im not a taker, sorry.
Here are the tables.
MySQL :: Time zone description tables
You can open a supprt ticket and give that link then make a request to have it installed in the mysql DB.
So you do this ...
"set time_zone = '-5:00'"
How do you handle daylight savings time?
I don't. Which is probably why one of my programmers had issues this weekend. I have always used PHP for the time, and it wasn't until this weekend that I got a sense of the ugliness of the fact that Mysql doesn't seem to get the time from the same place PHP does.
He handles my auction launcher for eBay, and many users had their scheduled auctions go out an hour late. Ughh....
I have been meaning to look further into this, and this may be the time.
But even if the tables get populated, I came accross indications that the tables would need to be constantly updated for that very reason. I didnt read hard enough or look at the structure to see how it handles that.
We are in the same boat.
I was also looking for a way to set a domains timezone through CPanel because server logs are also annoying with the timestamps written to system time. Or possiblely through some Apache directive that could be added to htaccess file.
I am going to check further on the Apache thing and post back results.
Well you can set your env variable using an Apache directive. Place this line in your .htaccess file in your document root directory:
SetEnv TZ America/Chicago
After placing this in my .htaccess file, PHP works like a champ and you no longer have to make the adjustment in your PHP code. However, MySQL still requires the per connection manipulation.
The scripts I have seen, have a checkbox which adjusts the script settings/clock by one hour when it is checked, or not checked respectively. But really, if PHP is using the server time then the time should always be right since that clock is updated by rdate.
As for MySQL, I have no idea, if anyone finds it, I'd be interested in it for a script I am working on as well.
After discussing this further off-line, I have come to the conclusion that I believe it would always be best to handle any recording of time into MySQL as GMT. Then to simply let PHP handle the displaying of time into your intended time zone.
This would allow you to change the time zone domain wide by using the Apache directive. Then as Matt has pointed out and you have visitors that require the time to be displayed differently, you can override the default time zone set by Apache with PHP.
As Mysql is concerned you can then make the adjustment on a per connection basis by setting the time zone to "0".
I believe this would also aide in making your code more portable.
This is my conclusion, any other thoughts or holes in my train of thought here?
I think I too have heard that the way to do it is in fact, GMT. It does ring a bell anyways.
The only portability problem I see is that not everyone uses Apache webserver.
I suppose it would not much matter for you unless you plan on making this application for sale or distribution, and then, I still wonder. I think the more you can keep out of .htaccess the better though, because different web servers either do not read that file, or may interpret it differently than Apache does.
Why not let PHP do all the time processing based on GMT in MySQL?
Well, yes, PHP uses server local time. You can obtain if local server time is in DST (call date('I')). As for MySQL - it depends on configuration. Check MySQL manual: A.4.6. Time Zone Problems.
However, from the PHP script you can't determine if (let's say the server is in Florida) if in New Ziland is used DST right now. See: About Daylight Saving Time. That is why many PHP applications have 'Use DST' in options (it simply adds 1 hour to the user timezone).
JM, Scott, any comments?
Very interesting situation arises, when user says his timezone is GMT +2 & he is in country that uses DST..... so what the current user time is? Anybody knows for sure?
Finally, after few hours of searching, found & tested a script. Seems working good:
Timezone and Daylight Saving Time Script
However, I wasn't able to check many timezones... Have a look at it
From his site:
I believe this was passed this year. Though I forget what it was, I think what it entailed was a change to the date that DST was recognized on.Quote:
At the time of writing this, I am aware that the United States is considering passing legislation that would change their daylight savings time rules with Canada to possibly follow suit. Once these new rules are made official, I will make an effort to update this script to reflect the changes.
Can anyone confirm or deny that the changes have already been made?