I'm having a real problem with timezones! I want users to be able to schedule events on my site, but I want them to do that in their local time. They also need to be able to edit these events.
However when they go to edit the event, when I create the edit page, I do not know their TZ-offset on the server side, so I figure I have to write out the UTC time with PHP to a JS variable, and then try to munge that into local time. Doesn't sound too hard but I've been gnashing my teeth on this for a few hours now. I have this:-
(The events are only scheduled to the minute)
var utc_string = '<?php echo $start_date_sql ?>T<?php echo $start_hour ?>:<?php echo $start_minute ?>:00.000Z';
var utc_start = new Date(utc_string);
var local_start_milliseconds = utc_start.getTime() + tz_offset_milliseconds;
var local_start = new Date( local_start_milliseconds );
alert("Local hours " + local_start.getHours());
alert("Local time is " + local_start.toTimeString());
Now for instance I have an event stored at 2.00am UTC on a particular day and on my browser which is in GMT so UTC+1hr, when this code picks up that date, I would like the hours alert to give me 3 (Or 1 whichever! I can work that out once I sort out This problem!), but that doesn't happen, it gives 2. The reason is obvious because the second alert gives me
Local time is 02:00:00 GMT+0100 (GMT Standard Time)
How can I make it give me the actual local time rather than UTC+Whatever, that is completely useless to me!! I need to populate the date field and hours/minutes selectors with this data!
There Must be a simple way to do this but I can't seem to find it!
Any help hugely appreciated...
Perhaps I'm being really dumb but I can't see how to edit my post.
I missed off a line from the top
var tz_offset_milliseconds = new Date().getTimezoneOffset() * 60000;
and the smiley should be colon : followed by open angle bracket <
Okay I think I solved this myself, Date.parse is the key...
var local_start = new Date();
local_start.setTime(Date.parse('<?php echo $edit_sus_data['start_date_sql'] ?>T<?php echo $edit_sus_data['start_hour'] ?>:<?php echo $edit_sus_data['start_minute'] ?>:00.000Z'));
var start_date_sql = local_start.getFullYear() + "-" + pad(local_start.getMonth()+1) + "-" + pad(local_start.getDate());
var start_date = local_start.toDateString();
var start_hour = pad(local_start.getHours());
var start_minute = pad(local_start.getMinutes());