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.
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?
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.
Last edited by Matt; 03-13-2008 at 08:05 PM.
Send your friends and site visitors to GlowHost and get $125 plus bonus!
GlowHost Affiliate Program | Read our Blog | Follow us on X |
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?
Last edited by Matt; 03-14-2008 at 10:29 AM.
Send your friends and site visitors to GlowHost and get $125 plus bonus!
GlowHost Affiliate Program | Read our Blog | Follow us on X |
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.