The database stores data on the operating time of the company in the format: 09: 00.18: 30; 12: 00.02: 00, etc. It is necessary to compare the current time with two intervals (the beginning / end of the working day) and if the current time is between working hours, return true, otherwise false.

I convert and try to compare this:

$start = strtotime($start_work); $end = strtotime($end_work); if ($current_time >= $start && $current_time <= $end) { ... } else { ... } 

How does it work

The company is open from 08:30 to 18:00, and the current time is 12:00. In this case, everything is fine. Everything is displayed correctly

How does not work

the company is open from 08:30 to 02:00, and the current time is 12:00.

In general, the problem is if any time after midnight. because it is essentially less (from 0) I was looking for options, I found a lot of questions about this, but I could not find an answer.

How can this issue be resolved? Thank.

  • And in what format is the time stored in $ start_work and $ end_work? without date? - koks_rs
  • Yes, the fact of the matter is that without a date. Only 09: 00,18: 30 - kate
  • You can take the date today, and time from a variable. Thus, to construct two variables with the date of the beginning and end of the day and then the matter is in the hat. - koks_rs
  • So, if the end of the working day ends after 00:00 (for example at 03:00), it still will not work - kate
  • Try to see how this library works [florianv / business] ( github.com/florianv/business ) - Orange_shadow

2 answers 2

  <?php //Ваши входные данные $start_work = "08:00"; $end_work = "02:00"; $currentTime = "01:00"; //текущее время с датой, даже если оно у вас 12:00 все равно имеет дату $currentDateTime = strtotime(date('Ym-d') ." ". $currentTime); //Дата и время во сколько мы закрылись вчера $previousDayEnd; //Дата и время во сколько мы открылись сегодня $startDateTime; //Дата и время во сколько мы закрылись сегодня $endDateTime; $startDateTime = strtotime(date('Ym-d') ." ". $start_work); if (strtotime($start_work) <= strtotime($end_work)){ $endDateTime = strtotime(date('Ym-d') ." ". $end_work); $previousDayEnd = strtotime(date('Ym-d') ." ". $end_work . "-1 days"); } else{ $endDateTime = strtotime(date('Ym-d') ." ". $end_work . "+1 days"); $previousDayEnd = strtotime(date('Ym-d') ." ". $end_work ); } //проверить полученные результаты echo "Мы закрылись в : " . date("Ymd H:i:s", $previousDayEnd). "\n"; echo "Открытие : " . date("Ymd H:i:s", $startDateTime) . "\n"; echo "Закрытие : " . date("Ymd H:i:s", $endDateTime). "\n"; if ($currentDateTime >= $startDateTime && $currentDateTime <= $endDateTime) { echo "Сейчас рабочее время : " . date("Ymd H:i:s", $currentDateTime) ."\n"; } else if($currentDateTime < $startDateTime && $currentDateTime < $previousDayEnd ){ echo "Сейчас рабочее время : " . date("Ymd H:i:s", $currentDateTime) ."\n"; } else { echo "Мы закрыты в это время : " . date("Ymd H:i:s", $currentDateTime) ."\n"; } 

play around here

  • Thank you very much! Everything worked out, the code works great. Have a day :-) - kate

If the end time is less than the start time, then two ranges must be checked through OR - from the start to midnight (24:00) and from midnight (00:00) to the end.