<?php
include("../../db.php");
header('content-type: application/json');

$from_date = $_GET['from'];
$to_date = $_GET['to'];


$arr = array();
$brandQry = $db->getRows("select * from teacher_timetable  ORDER BY id DESC");
// $queryRes = mysql_query($brandQry);
$status = 0;
if (count($brandQry) > 0) {
// while($row_val = mysql_fetch_assoc($queryRes))
    foreach ($brandQry as $row_val) {

        if ($from_date != null && $to_date != null) {
            $range = check_in_range($from_date, $to_date, $row_val['create_time']);
            if (!$range){
                continue;
            }
        }

        $teacher = $db->getRow("select * from staff where idno='$row_val[teacher_id]' ");

        array_push($arr, ["id" => "$row_val[id]", "teacher_id" => "$row_val[teacher_id]",
            "file" => "uploads/notice/$row_val[file]", "session" => "$row_val[session]",
            "create_time" => "$row_val[create_time]", "name" => "$teacher[name]",
            "subject" => "$teacher[experience]"]);
    }
    $status = 1;
}

$json_data = array("status" => $status, "data" => $arr);


echo json_encode($json_data);


function check_in_range($start_date, $end_date, $date_from_user)
{
    $dx = new DateTime($date_from_user);
    $new_date = $dx->format('Y-m-d');


    // Convert to timestamp
    $start_ts = strtotime($start_date);
    $end_ts = strtotime($end_date);
    $user_ts = strtotime($new_date);

    // Check that user date is between start & end
    return (($user_ts >= $start_ts) && ($user_ts <= $end_ts));
}
