How to compare time (a predetermined range) and the date (on the day)?

In General, the tasks you want to display the message on weekdays from 18 PM to 9 am, and at any time of the day.

There are two functions, one compares time, the other checks for the day

WeekendCheck function($date) {
 return (date('N', strtotime($date)) >= 6);
}

 function TimeCheck($date) 
{
 $date_min = new DateTime("9:00");
 $date_max = new DateTime("18:00");
 $date_now = new DateTime($date);
 if ($date_now >= $date_min && $date_now <= $date_max) 
{
 return true;
}
 return false;
}

 if (WeekendCheck("2017-10-07"))
{
 print("the Message in the day");
 } else {
 if(!TimeCheck("8:00")) {
 print("the Message on a weekday");
}
 };


This works as it should, but is there any simpler options?
June 14th 19 at 19:05
2 answers
June 14th 19 at 19:07
Solution
As an option. Was advised on another site.
function TimeCheck(){
 $now = (int) (new DateTime())->format("H");
 return $now < 9 || $now >= 18;
}
WeekendCheck function(){
 $now = (int) (new DateTime())->format("w");
 return $now === 0 || $now === 6;
}
if (WeekendCheck()){
 print("the Message in the day");
} else if(TimeCheck()) {
 print("the Message on a weekday");
};
June 14th 19 at 19:09
Fundamentally so.
But small things to improve:
  1. else if to combine elseif
  2. if condition return true; else return false; replace with : return condition;
  3. TimeCheck($date) replace with TimeCheck($time)
  4. if the clock always send two digits, it is possible to compare time strings, not objects

Find more questions by tags PHPWork with date / time