Q:

Return breaks in between start and end date of classes

i have classes with their times start and end date 
class example data
"id": "1",
"name": "class 1",
"start": "2017-08-09 02:00:00",
"end": "2017-08-09 03:00:00",

"id": "2",
"name": "class 2",
"start": "2017-08-09 04:00:00",
"end": "2017-08-09 05:00:00",

"id": "3",
"name": "class 3",
"start": "2017-08-09 06:30:00",
"end": "2017-08-09 08:00:00",

i want to return all the break times >= current time, in between classes, for example a break is considered the time between end date from class 1 and start date from class 2 
expected output
"break": "1",
"start": "2017-08-09 03:00:00",
"end": "2017-08-09 04:00:00",

"break": "2",
"start": "2017-08-09 05:00:00",
"end": "2017-08-09 06:30:00",

this is what i tried
$now = date('Y-m-d H:i:s');
$sql = "SELECT * FROM classes WHERE classes.start >= '$now'";

try {
    $db = new db();
    $db = $db->connect();
    $stmt = $db->prepare($sql);

    $stmt->execute();

    $breaks = $stmt->fetchAll(PDO::FETCH_OBJ);

    $db = null;

    if(empty($breaks)) {
        $response->getBody()->write
        ('
        {
            "error":
            {
                "message":"Invalid"
            }
        }');
    } else {
        $response->getBody()->write(json_encode($breaks));
    }
} catch(PDOException $e) {}

A:

Please try this :
function getBreaks($classes = array()) {
    $breaks = array();
    for ($i = 0; $i <= count($classes); $i++) {
        if ($i < count($classes)-1) {
            if (strtotime($classes[$i+1]->start) > strtotime($classes[$i]->end)) {
                $break = array();
                $break["break"] = count($breaks)+1;
                $break["start"] = $classes[$i]->end;
                $break["end"] = $classes[$i+1]->start;  
                array_push($breaks, $break);
            }
        }
    }
    return array("breaks"=>$breaks);
}

$now = date('Y-m-d H:i:s');
$sql = "SELECT * FROM classes WHERE classes.start >= '$now' ORDER BY classes.start";

try {
    $db = new db();
    $db = $db->connect();
    $stmt = $db->prepare($sql);

    $stmt->execute();

    $breaks = $stmt->fetchAll(PDO::FETCH_OBJ);

    $db = null;

    if(empty($breaks)) {
        $response->getBody()->write
        ('
        {
            "error":
            {
                "message":"Invalid"
            }
        }');
    } else {
        $arr = array();
        foreach($breaks as $break) {
            array_push($arr, $break);
        }
        var_dump($arr);
        $response->getBody()->write(json_encode(getBreaks($arr)));
    }
} catch(PDOException $e) {}

