Page 1 of 1

Daylight Savings Time

Posted: Tue Feb 03, 2009 11:07 pm
by msurabbott
I have been fighting an issue now for over a year and it seems simple but I am failing miserably.

I have an application that is written in PHP, on this particular page I am doing some simple date math then using JavaScript to store those fields into form elements. This all works just fine.

Then I submit the form that those elements are in to a MySQL database - NOW the date entered is off by one hour if it is between March and .. October or whenever Daylight Savings ends. So the PHP is write, the JS is write, but when its written to MySQL its wrong..

Is there a date setting in MySQL I'm not aware of or am I doing something else wrong?

Re: Daylight Savings Time

Posted: Tue Feb 03, 2009 11:21 pm
by requinix
msurabbott wrote:am I doing something else wrong?
msurabbott wrote:I am doing some simple date math
There's your problem. If you want to do the math yourself then you have to know and follow all the rules. When you don't follow the rules you have problems.

Do the math in PHP, not JavaScript. And when I say "do the math" I mean "use PHP functions to do the math for you". Functions like strtotime.

Re: Daylight Savings Time

Posted: Tue Feb 03, 2009 11:43 pm
by msurabbott
Hmm.. well I appreciate your quick response, although I don't appreciate your clear "You're an idiot" tone..

There is no JavaScript date math going on here, its all calculated using PHP, using strtotime so that it is accurate. I then set that value to an HTML Element so it can be submitted with the form.

When it is received it is just pulled from the form element, and stuck into an SQL statement. This isn't some stupid mistake with date math, I'm not an idiot, and your response was useless. Next time you want to attempt to help a community provide some valuable constructive feedback - it's people like you that cause people not to post questions with the fear of sounding dumb.

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 12:20 am
by joel24
not sure if its what your after, don't fully understand what you need without seeing the code....
you can set the timezone in mysql?

Code: Select all

 
    $timezone = '+11:00'; //AEDST
    $update_tz = @mysql_query("SET time_zone = '$timezone';");
 
i'm sure MYSQL would allow for timezones such as 'Sydney/Australia' also... You'll have to try it.

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 12:30 am
by msurabbott

Code: Select all

var d = 2009,04,10
var t = 08,00,00
var timezone = new Date();
 
// forcing eastern time which is what the 300 * 60 is for
var pu_time = (Date.UTC(d[0], d[1] - 1, d[2], t[0], t[1], t[2], 0) / 1000) + (300 * 60);
after this code is run pu_time has a value of 1239368400 which is Fri Apr 10 09:00:00 2009.. It should be 8 am

So apparently this is a JavaScript issue.. Hopefully this will help someone, help me hah

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 1:54 am
by requinix
msurabbott wrote:Hmm.. well I appreciate your quick response, although I don't appreciate your clear "You're an idiot" tone.
And you're going to remember it for quite a while, right? If you remember that someone insulted you because you tried to calculate a time yourself then my process works.

What happens to pu_time? Does it get passed to PHP directly?

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 8:31 am
by msurabbott

Code: Select all

$('start_time').value = pu_time;
the value is now stored in an HTML element, the form is submitted then handled..

Code: Select all

$start = $_POST['start_time'];
$startDate = date("Y-m-d H:i:s", $start);
From here $startDate is passed to an SQL Statement

But all this seems irrelevant to me as the error is occurring in the JavaScript Date object, it is making a conversion because it realizes that the date I'm passing it is within Daylight Savings time..

So I need to figure out how to convince JavaScript not to do that, or I have to somehow convert it by adding or subtracting an hour depending on whether or not it is within the dates that Daylight Savings occurs

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 12:53 pm
by requinix
UTC doesn't do daylight savings time, that's why it's an hour off during that half of the year. (DST ends early November in the US, by the way.)

If you don't do anything with pu_code but pass it to PHP then there's no need for JavaScript: just let PHP do it for you.

Code: Select all

ini_set("date.timezone", "America/PickACity");
$startDate = date("Y-m-d H:i:s");
Cities are listed here.

Re: Daylight Savings Time

Posted: Wed Feb 04, 2009 1:30 pm
by msurabbott
I've already got the ini set correctly..

I am using javascript because those fields are not set in any AJAX like the rest of the page, I suppose I will just need to add ajax to do this so that I can use PHP rather than JS for this date..