SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Convert UTC to local time not working as I would like!

    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.

    So I figured out I needed to convert the date-time they send to UTC store the datetime in the DB in UTC. On the entry form, they select a date with a jquery datepicker thing, and hours/minutes on two dropdowns. There is a hidden form field where I stash their tz-offset from javascript. This gets sent to the server together with the date/hours/minutes the user selected. Some php munging and that is converted to UTC time and written to the mysql db. That much is working fine.

    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...

  2. #2
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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 <

  3. #3
    SitePoint Member
    Join Date
    Oct 2013
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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());


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •