Reputation: 5237
How do you get the current time zone offset in hours with javascript, store it in a database, and compare with other time zone rows in the database to get the time zone difference.
Upvotes: 1
Views: 99
Reputation: 5520
Not going to write the full code because that would take hours but here is the gist of it...
First get the offset with JS. Here is a nice sample on the MDN
var x = new Date();
var currentTimeZoneOffsetInHours = x.getTimezoneOffset() / 60;
Then pass that to the server-side using AJAX. jQuery allows you to do this nicely..
https://api.jquery.com/jquery.post/
$.post(
"ajax/store-date.php",
{
offset: currentTimeZoneOffsetInHours
},
function( data ) {
// success callback.
});
On the PHP side store this in the database. There are many ways to do this.. here is a basic one..
http://www.w3schools.com/php/php_mysql_insert.asp
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Offsets (offset) VALUES (" . floatval($_POST['offset']) . ")";
if ($conn->query($sql) === TRUE) {
echo "Success!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
To compare use SQL such as...
SELECT SUM(offset) FROM Offsets
WHERE id IN (1,10) /* add the 2 entry id's you want to compare */
As stated before, there is TONNES of extra code missing, but that is irrelevant to the scope of the question.
Upvotes: 1