3v4l.org

run code in 300+ PHP versions simultaneously
<?php //function checkDeliveryPeriod(bool $isGas = false) //{ // $startDate = new DateTime('2020-08-01 00:00:00'); // $endDate = new DateTime('2024-02-16'); // // initial // $originReadAt = new DateTime('2024-02-17 00:00:00'); // $readAt = clone $originReadAt; // if ($isGas && $endDate !== null && $readAt > $endDate) { // //Gas day case: gas day ends at 6:00 next day // $readAt->subHours(6); // } // $readAt->setTime(0, 0, 0); // // // assure contract start date // if ($startDate == null) { // return false; // } // //special case when the start and end dates of the contract are the same // $startEndOnSameDay = ( // ($originReadAt->format('H:i') == '00:00') // && ($endDate === $startDate) // ); // // final check // return ( // $readAt >= $startDate // && ( // $endDate === null // || ($endDate !== null && $readAt <= $endDate) // ) // ) || $startEndOnSameDay; //} // //var_dump(checkDeliveryPeriod()); function determineNewRunAt(\DateTimeInterface $createdAt): DateTime { $nextRunAt = new DateTime(); $oneMonth = (clone $createdAt)->modify('1month'); $diff = $nextRunAt->diff($createdAt); var_dump($diff->days, $diff->h); // time out after one month after creation if ($nextRunAt >= $oneMonth) { throw new \Exception('Waiting time expired'); } // run the action once a day, if it was not finished within 1 day after creation if ($diff->days >= 1) { return $nextRunAt->modify('1day'); } return $diff->h === 0 ? $nextRunAt->modify('5minute') // run the action every 5 minutes within the first hour after creation : $nextRunAt->modify('1hour'); // run the action every hour for one day after creation } $nextRunAt = determineNewRunAt(new DateTime('2024-04-10 07:15')); var_dump($nextRunAt->format('Y-m-d h:i:s')); $nextRunAt = determineNewRunAt(new DateTime('2024-04-10 06:15')); var_dump($nextRunAt->format('Y-m-d h:i:s')); $nextRunAt = determineNewRunAt(new DateTime('2024-04-09 06:15')); var_dump($nextRunAt->format('Y-m-d h:i:s')); try { $nextRunAt = determineNewRunAt(new DateTime('2024-03-09 06:15')); } catch (\Throwable $e) { var_dump($e->getMessage()); }

preferences:
30.11 ms | 404 KiB | 5 Q