I have a cronjob, that kicks in every 5 minutes. It should do some tasks only at specific times of the day (e.g. morning and evening).
What’s php’s most effective / elegant way to determine if the DateTime of now is in between the 5 minute time frame in that the cronjob may kick in?
At the moment I’m doing:
$date = new DateTime();
$hour = (int)$date->format('H');
$min = (int)$date->format('i');
if($hour == 7 && ($min >= 40 || $min < 45)) {
// Do something in the morning
}
if($hour == 21 && ($min >= 00 && $min < 05)) {
// Do something in the evening
}
But this seems like a lot of code. Ain’t there something like
$date->isInTimeRane($begin, $end);
as native php code?
You can extend DateTime to add your own methods to it. I would do it this way:-
Then you can simply do:-
That is the cleanest way I can think of of doing it and pretty much what you asked for.