-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetoffset.php
26 lines (25 loc) · 998 Bytes
/
getoffset.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
<?php
/** Returns the offset from the origin timezone to the remote timezone, in seconds.
* @param $remote_tz;
* @param $origin_tz; If null the servers current timezone is used as the origin.
* @return int;
*/
function get_timezone_offset($remote_tz, $origin_tz = null) {
if($origin_tz === null) {
if(!is_string($origin_tz = date_default_timezone_get())) {
return false; // A UTC timestamp was returned -- bail out!
}
}
$origin_dtz = new DateTimeZone($origin_tz);
$remote_dtz = new DateTimeZone($remote_tz);
$origin_dt = new DateTime("now", $origin_dtz);
$remote_dt = new DateTime("now", $remote_dtz);
$offset = $origin_dtz->getOffset($origin_dt) - $remote_dtz->getOffset($remote_dt);
return $offset;
}
header("Content-Type: application/json");
try {
print json_encode(array("offset" => get_timezone_offset($_GET['r'], $_GET['o'])));
} catch (exception $e) {
print json_encode(array("error" => "Invalid Timezone"));
}