Last active
December 15, 2015 22:48
-
-
Save alxfv/5335130 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$ttl = 5 * 3600; | |
$refresh_time_limit = strtotime('2013-04-07 13:00'); // variable_get | |
$limit = 10; // variable_get | |
$max_limit = 450; // variable_get | |
$now = time(); // 12:15 | |
$is_hour = ($now - $refresh_time_limit) > 3600; // true | |
$city_name = 'Чебоксары'; | |
if ($limit < $max_limit) { | |
$refresh_time_city = strtotime('2013-04-07 08:00'); | |
$is_city_ttl_over = ($now - $refresh_time_city) > $ttl; // true | |
if ($is_city_ttl_over) { | |
$city = get_city_from_api($city_name); | |
save_to_database($city); | |
print_city($city); | |
} | |
else { | |
$city = load_from_database($city_name); | |
print_city($city); | |
} | |
} | |
else { | |
if ($is_hour) { | |
$refresh_time_limit = $now; | |
$limit = 0; | |
$city = get_city_from_api($city_name); | |
save_to_database($city); | |
print_city($city); | |
} | |
else { | |
echo '<h1>Пустой город</h1>'; | |
} | |
} | |
function get_city_from_api($city_name) { | |
return 'Чебоксары - 5 градусов'; | |
} | |
function save_to_database($city) { | |
file_put_contents('weather.txt', $city); | |
} | |
function print_city($city) { | |
echo '<h1>Погода в городе ' . $city . '</h1>'; | |
} | |
function load_from_database($city_name) { | |
return file_get_contents('weather.txt'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment