summaryrefslogtreecommitdiffstats
path: root/web/lib/timezone.inc.php
blob: 9fb24331c03f0051a99ad5c7420e1092a68ad8c9 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
set_include_path(get_include_path() . PATH_SEPARATOR . '../lib');

/**
 * Generate an associative of the PHP timezones and display text.
 *
 * @return array PHP Timezone => Displayed Description
 */
function generate_timezone_list() {
	$php_timezones = DateTimeZone::listIdentifiers(DateTimeZone::ALL);

	$offsets = array();
	foreach ($php_timezones as $timezone) {
		$tz = new DateTimeZone($timezone);
		$offset = $tz->getOffset(new DateTime());
		$offsets[$timezone] = "(UTC" . ($offset < 0 ? "-" : "+") . gmdate("H:i", abs($offset)) .
			") " . $timezone;
	}

	asort($offsets);
	return $offsets;
}

/**
 * Set the timezone for the user.
 *
 * @return null
 */
function set_tz() {
	$timezones = generate_timezone_list();
	$update_cookie = false;

	if (isset($_COOKIE["AURTZ"])) {
		$timezone = $_COOKIE["AURTZ"];
	} elseif (isset($_COOKIE["AURSID"])) {
		$dbh = DB::connect();
		$q = "SELECT Timezone FROM Users, Sessions ";
		$q .= "WHERE Users.ID = Sessions.UsersID ";
		$q .= "AND Sessions.SessionID = ";
		$q .= $dbh->quote($_COOKIE["AURSID"]);
		$result = $dbh->query($q);

		if ($result) {
			$timezone = $result->fetchColumn(0);
		}

		$update_cookie = true;
	}

	if (!isset($timezone) || !array_key_exists($timezone, $timezones)) {
		$timezone = config_get("options", "default_timezone");
	}
	date_default_timezone_set($timezone);

	if ($update_cookie) {
		$timeout = intval(config_get("options", "persistent_cookie_timeout"));
		$cookie_time = time() + $timeout;
		setcookie("AURTZ", $timezone, $cookie_time, "/");
	}
}