I want to get the number of hours from the time-in of an employee 10pm to 6am. I have search a code to check if the time-in is between two time : PHP Check if time is between two times regardless of date
I want to check my employee's time in if it is between 10pm to 6am and return the number of hours that is between 10pm to 6pm. From the link, i've tried and i've returned an error with: Call to a member function modify() on boolean
Here is the code that i've tried, i want to use date_diff
function to get the number of hours from 10pm to 6am. The $schedule->time_in
is the time-in of my employee which in example of my code, i have a value of 4:00am from $schedule->time_in
Here is the function from the link which i've used and returns error:
private function isBetween($from, $till, $input) {
$f = DateTime::createFromFormat('!H:i', $from);
$t = DateTime::createFromFormat('!H:i', $till);
$i = DateTime::createFromFormat('!H:i', $input);
if ($f > $t) $t->modify('+1 day');
return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
}
I'm trying to use it like this to check what value it would return:
$cola_in = "22:00";
$cola_out = "6:00";
print_r($this->isBetween($cola_in,$cola_out,$schedule->time_in));
I wan't to came up with something like this :
$cola_in = "22:00";
$cola_out = "6:00";
$date1 = date("H:i",strtotime($schedule->time_in));
$date2 = date("H:i",strtotime($cola_in));
$date3 = date("H:i",strtotime($cola_out));
if ($date1 > $date2 && $date1 < $date3) {
$number_of_hours = $date1->diff($date3);
print_r($number_of_hours);
}