Pretty simple with Composer, run:
composer require anime-db/smart-sleep
First build schedule
use AnimeDb\SmartSleep\Rule\EverydayRule;
use AnimeDb\SmartSleep\Schedule;
$schedule = new Schedule([
new EverydayRule(0, 3, 260), // [00:00, 03:00)
new EverydayRule(3, 9, 900), // [03:00, 09:00)
new EverydayRule(9, 19, 160), // [09:00, 19:00)
new EverydayRule(19, 23, 70), // [19:00, 23:00)
new EverydayRule(23, 24, 60), // [23:00, 24:00)
]);
Configure SmartSleep
use AnimeDb\SmartSleep\SmartSleep;
$smart = new SmartSleep($schedule);
And now we can sleep
$seconds = $smart->sleepForSeconds(new \DateTimeImmutable());
sleep($seconds);
The rule corresponds to specific day in the specified time interval. Can be used for public holidays.
$rule = new SpecificDayRule(new \DateTimeImmutable('2017-01-01'), $start_hour, $end_hour, $max_sleep_seconds)
The rule corresponds to any day in the specified time interval.
$rule = new EverydayRule($start_hour, $end_hour, $max_sleep_seconds)
The rule corresponds to the holiday at the specified time interval.
$rule = new HolidayRule($start_hour, $end_hour, $max_sleep_seconds)
The rule corresponds to the weekday at the specified time interval.
$rule = new WeekdayRule($start_hour, $end_hour, $max_sleep_seconds)
The rule always corresponds to the specified time. Returns the seconds in the next day.
$rule = new OnceDayRule()
The rule always corresponds to the specified time. Returns the seconds in the next week.
$rule = new OnceWeekRule()
The rule always corresponds to the specified time. Returns the seconds in the next month.
$rule = new OnceMonthRule()
This bundle is under the MIT license. See the complete license in the file: LICENSE