Assuming you always have the same time format:
$time = "2008-02-26 14:40:01"
user substr to get hour and minute.
$hour = substr($time, 11, 2);
$min = substr($time, 14, 2);
How to loop will depend on how you retrieve and store the data from the database. Personally, I like working with non-associative arrays for this:
first make sure you have something in $result... and then
$last_hour = substr($result[0], 11, 2);
$last_min = substr($result[0], 14, 2);
// write entry to graph...
for ($i = 1; $i < count($result); $i++) {
$hour = substr($result[$i], 11, 2);
$min = substr($result[$i], 14, 2);
if ($hour == $last_hour && $min == $last_min + 20 ||
$hour = $last_hour + 1 && $min == 0 && $last_min == 40) {
// entry found, proceed normally
}
else {
// entry missinig, proceed as needed
}
$last_hour = $hour;
$last_min = $min;
}