Search code examples
timeutcunix-timestamp

Get local UTC time from HTML sbmitted time


I have a HTML form which has a date and time field. I assume that when a user enters a date/time, they are doing so in their local time, but when I store it in the database, I need it in UTC, and then when displaying it I need to adjust it to the viewers time.
Can I know the clients timezone from the server (I am using PHP and Apache, but may use IIS in the future, so would rather not rely on Apache-only behavior if possible)? Or can I do UTC <-> Local conversions on the client in Javascript?

Example: A user in UTC+1 timezone submits 2012-10-18 01:30. This should get stored as the timestamp of 2012-10-18 00:30:00 (whatever that is). Then, if a client at UTC-5 with local DST views the record, they should see 2012-10-17 20:30.


Solution

  • <html>
    <head>
    <script>
    function test()
    {
        var d = new Date(2012, 6, 13, 12, 0, 0, 0);
        console.log(d);
        var hours = d.getUTCHours();
        console.log(hours);
    }
    </script>
    </head>
    <body>
    <input type="button" onclick="test()" value="test"/>
    </body>
    </html>
    

    You input d as local time, and get UTC time by using getUTCHours(), getUTCDates(), etc... Then send UTC time back to server.