Add sampledate column to weather DB table

This commit is contained in:
Junior 2024-10-23 11:46:44 -04:00
parent 695902ea1f
commit aa4f4fb010
3 changed files with 16 additions and 4 deletions

View File

@ -17,10 +17,19 @@ class Weather {
. "&appid=" . WEATHERAPIKEY
. "&units=" . WEATHERUNITS;
public static function fetchWeather() {
public static function deleteOldWeather($days = 10) {
if ( !is_int($days) ) return false;
if ( $days < 0 ) return false;
$query = "DELETE FROM " . AppDB::TABLE_WEATHER . " WHERE DATEDIFF(NOW(), sampledate) > :days";
$sth = AppDB::getSTH($query);
if ( $sth === false ) return false;
$sth->bindValue(":days", $days, PDO::PARAM_INT);
$sth->execute();
}
public static function fetchWeather() {
$weather = file_get_contents(Weather::WEATHERURL);
$query = "UPDATE " . AppDB::TABLE_WEATHER . " SET weather=:weather";
$query = "INSERT INTO " . AppDB::TABLE_WEATHER . " (sampledate, weather) VALUES(NOW(), :weather)";
$sth = AppDB::getSTH($query);
if ( $sth === false ) return false;
$sth->bindValue(":weather", $weather, PDO::PARAM_STR);
@ -28,7 +37,7 @@ class Weather {
}
public static function getWeather() {
$query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " LIMIT 1";
$query = "SELECT weather FROM " . AppDB::TABLE_WEATHER . " ORDER BY sampledate DESC LIMIT 1";
$sth = AppDB::getSTH($query);
if ( $sth === false ) return false;
$sth->execute();

View File

@ -42,7 +42,9 @@ DROP TABLE IF EXISTS `weather`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `weather` (
`weather` text NOT NULL
`sampledate` datetime NOT NULL,
`weather` text NOT NULL,
PRIMARY KEY(`sampledate`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC;
/*!40101 SET character_set_client = @saved_cs_client */;
/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;

View File

@ -10,5 +10,6 @@ require_once("authfunctions.php");
if ( php_sapi_name() != "cli" ) exit();
$fetched = Weather::fetchWeather();
$deleted = Weather::deleteOldWeather();
exit();