-
Notifications
You must be signed in to change notification settings - Fork 1
/
5.php
129 lines (101 loc) · 3.27 KB
/
5.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
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
/*
General algorithm:
1. Figure out in what time zone(s) it’s 5 o'clock
2. Look up cities in that/those time zone(s)
*/
require '/usr/home/segdeha/apps/whereisitfiveoclock.beer/dbsettings.php';
// location of cached response
define('FILENAME', '_cache.json');
// always work based on UTC
date_default_timezone_set('UTC');
// read cached file
$cached_json = file_get_contents(FILENAME);
// decide whether we need to get new data
if ($cached_json) {
$obj = json_decode($cached_json);
// check last updated date/time
$expiry = $obj->{'expiry'};
// if current time is after the expiry, curl again
$curl = time() > $expiry;
}
else {
$curl = true;
}
if ($curl) {
// get current times around the world
$ch = curl_init('http://api.timezonedb.com/v2.1/list-time-zone?key=' . TIMEZONEDB_API_KEY . '&format=json');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
$fresh_json = json_decode($response);
// loop through results to find time zones where the base time (`date('G', timestamp)`) is 5
$zones = array();
foreach ($fresh_json->zones as $zone) {
if (17 == date('G', $zone->timestamp)) {
array_push($zones, $zone->zoneName);
}
}
// construct query
$query = "
SELECT city_name, country_name
FROM cities, country
WHERE time_zone IN ('" . implode("','", $zones) . "')
AND cities.country_code = country.country_code
";
// look up cities based on the time zones found in the previous step
$lnk = mysql_connect(DBSERVER, USERNAME, PASSWORD)
or die ('Not connected: ' . mysql_error());
mysql_select_db(DATABASE, $lnk)
or die ("Can’t use <strong>" . DATABASE . '</strong>: ' . mysql_error());
mysql_set_charset("utf8"); // <- required to make json_encode work
$results = mysql_query($query)
or die ('<strong>MySQL Error:</strong> ' . mysql_error() . "<pre>$query</pre>");
$cities = [];
while ($row = mysql_fetch_assoc($results)) {
array_push($cities, $row['city_name'] . ', ' . $row['country_name']);
}
mysql_close();
// construct JSON that reflects the cities
// bust server cache every 30 minutes because india
$hour = date('H');
$minutes = date('i');
if ($minutes > 29) {
$hour += 1;
$minutes = '00';
}
else {
$minutes = 30;
}
$seconds = '00';
$month = date('n');
$day = date('j');
$year = date('Y');
$expiry = mktime($hour, $minutes, $seconds, $month, $day, $year);
$api_response = array(
'status' => 'success',
'source' => 'api',
'expiry' => $expiry,
'cities' => $cities,
);
$file_response = array(
'status' => 'success',
'source' => 'server cache',
'expiry' => $expiry,
'cities' => $cities,
);
$api_json = json_encode($api_response);
$file_json = json_encode($file_response);
// cache the result
file_put_contents(FILENAME, $file_json, LOCK_EX);
// return response
header('Content-Type: application/json');
echo $api_json;
}
else {
// return response
header('Content-Type: application/json');
echo $cached_json;
}
exit;