1

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);
    }       
Jc John
  • 1,799
  • 2
  • 34
  • 69
  • So if i understand you correctly; If someone clocked in at 4am and out at 8am. Then you want your function to return 2? Because that's the amount of hours they worked between 10pm and 6am? – Dirk Scholten Sep 16 '19 at 14:08
  • @DirkScholten yes.. thats it – Jc John Sep 16 '19 at 14:22
  • @DirkScholten something like if i have 10pm and 6am out then the function also should return 8. – Jc John Sep 16 '19 at 14:27

1 Answers1

1

If ($schedule->time_in == "4:00am") then DateTime::createFromFormat('!H:i', $schedule->time_in); will fail and return FALSE.

Which explains the error Call to a member function modify() on boolean probably triggered by the call $i->modify('+1 day') in your function.

In the other hand, if ($schedule->time_in == "4:00") the error should disappear.

I hope this helps.

Melwin Kieffer
  • 380
  • 4
  • 11
  • i tried to print_r($schedule->time_in) and here is the output: 04:00:00 – Jc John Sep 16 '19 at 14:25
  • can you explain me why this returns "1" integer value ? – Jc John Sep 16 '19 at 14:38
  • how do i convert 04:00:00 to something "4:00" .. btw, is the "4:00" a string value? – Jc John Sep 16 '19 at 14:39
  • 1
    You can use function `var_dump()` to ensure the data type, but yes it's a string. If you are sure about the value of `$schedule->time_in` you could use `DateTime::createFromFormat('!H:i:s', "04:00:00")` or you could truncate the string using function `substr()`. – Melwin Kieffer Sep 16 '19 at 15:09
  • @Melvin i tried and i dont understand the return type of the function. can you explain to me the return of the said function? – Jc John Sep 16 '19 at 15:23
  • Are you talking about the `isBetween()` function? – Melwin Kieffer Sep 17 '19 at 11:23