I am using a small script to turn some lights on or off, but I can't seem to solve the following issue:
A PHP scripts runs as a daemon, gets a time setting from a database in the form HH:mm:ss
When I create a new dateTime instance like:
$TON = DateTime::createFromFormat('H:i:s', $time_on); // i.e 16:00:00
$TOFF = DateTime::createFromFormat('H:i:s', $time_off); // i.e 02:00:00
It creates an datetime object like this:
Time ON: 07-10-2019 16:00
Time OFF: 07-10-2019 02:00
Obviously the Off time is now wrong, so I check if TIME_OFF < TIME_ON and if so I add 24 hours the new object will then be:
Time ON: 07-10-2019 16:00
Time OFF: 08-10-2019 02:00
Which is now correct, but obviously as the script runs as a daemon, when passing over midnight, the datetime objects will change to the following:
Time ON: 08-10-2019 16:00
Time OFF: 09-10-2019 02:00
Causing the logic to break..
Are there any solutions to create a simple time check that works over midnight..?
Many Thanks!