-
Notifications
You must be signed in to change notification settings - Fork 0
/
lastxx.php
62 lines (59 loc) · 1.23 KB
/
lastxx.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
<?php
$peakvalues = array();
$avgvalues = array();
$lastday = '';
$firstday = '';
$datecount = 0;
$s->execute();
while (($r = $s->fetch()) !== false) {
if ($firstday == '') {
$firstday = $r->p;
}
$peakvalues[$r->tday] = $r->p;
$avgvalues[$r->tday] = $r->sm / 288; // (check every 5m)
$datecount++;
$lastday = $r->tday;
}
/*
if ($lastday != '') {
$_t = time();
// this may not work well when db/php use different language to format
// dates...
$safeguard = 30;
$misseddates = array();
do {
$_t -= 3600 * 24;
$__t = date($dformat, $_t);
if ($__t == $lastday) {
break;
}
$misseddates[] = $__t;
} while($safeguard-- > 0);
$datecount += count($misseddates);
while (count($misseddates) > 0) {
$__t = array_pop($misseddates);
$peakvalues[$__t] = 0;
$avgvalues[$__t] = 0;
}
}
*/
$datecount = 0;
$_start = $firstdatapoint;
$_today = time();
$_peakvalues = array();
$_avgvalues = array();
lastxx_l:
$datecount++;
$_t = date($dformat, $_start);
if (array_key_exists($_t, $peakvalues)) {
$_peakvalues[$_t] = $peakvalues[$_t];
$_avgvalues[$_t] = $avgvalues[$_t];
} else {
$_peakvalues[$_t] = 0;
$_avgvalues[$_t] = 0;
}
$_start += 3600*24;
if ($_start < $_today) {
goto lastxx_l;
}
$values = array($_peakvalues, $_avgvalues);