-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronejobscript.php
146 lines (92 loc) · 3 KB
/
cronejobscript.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<?php
date_default_timezone_set("Asia/Colombo");
cronejob();
function cronejob(){
$servername = "localhost";
$username = "corona";
$password = "root";
$dbname = "corona";
$conn = new mysqli($servername, $username, $password, $dbname);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "http://hpb.health.gov.lk/api/get-current-statistical",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$response = json_decode($response, true); //because of true, it's in an array
//because of true, it's in an array
$apicount = $response['data']['local_total_cases'];
$today = Date('Y-m-d');
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT MAX(`date`) as `date` FROM `daycount`";
//$sql = "SELECT `date`,count FROM `daycount` group by count order by `date` desc limit 1;";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
$dbdate = $row["date"];
// $count = $row["count"];
}
} else {
echo "0 results";
}
$sql2 = "SELECT SUM(count) as fullcount from daycount";
$result2 = $conn->query($sql2);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
// $dbdate = $row["date"];
$count = $row2["fullcount"];
}
} else {
echo "0 results";
}
if($dbdate!=$today&&intval($apicount)>0){
$datetoinsert = $today;
$newcount = intval($apicount)-$count;
//echo $apicount;
$sql = "INSERT INTO daycount (`date`,count)
VALUES ('".$datetoinsert."', ".$newcount.")";
if ($conn->query($sql) === TRUE) {
//echo "New record created successfully";
}
}else if($dbdate==$today&&$count<intval($apicount)){
$sql4 = "SELECT SUM(count) as fullcount from daycount where `date`<'".$today."'";
$result4 = $conn->query($sql4);
if ($result4->num_rows > 0) {
// output data of each row
while($row4 = $result4->fetch_assoc()) {
//echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
// $dbdate = $row["date"];
$yesterdaysum = $row4["fullcount"];
}
} else {
echo "0 results";
}
//echo $yesterdaysum;
$datetoinsert2 = $today;
$newcount2 = intval($apicount)-$yesterdaysum;
//echo('AAAA');
$sql3 = "UPDATE daycount SET count=".$newcount2." WHERE `date`='".$datetoinsert2."'";
if ($conn->query($sql3) === TRUE) {
//echo "New record created successfully";
}
}
$conn->close();
}
?>