3v4l.org

run code in 500+ PHP versions simultaneously
<?php date_default_timezone_set('Europe/Stockholm'); function displayAllowPostsMessage($dateTime, $isDebug = false) { $weekdayStartTime = new DateTime('09:00'); $weekdayEndTime = new DateTime('19:00'); $saturdayStartTime = new DateTime('10:00'); $saturdayEndTime = new DateTime('15:00'); $templateOpen = "It's %s and we are open!"; $templateClosed = "It's %s and the posting are only allowed between %s - %s. Sorry for the Inconvenience."; $dayOfTheWeek = intval($dateTime->format('N')); $message = "It's closed on sunday."; if ($isDebug) { $weekdayStartTime->setDate((int)$dateTime->format('Y'), (int)$dateTime->format('m'), (int)$dateTime->format('d')); $weekdayEndTime->setDate((int)$dateTime->format('Y'), (int)$dateTime->format('m'), (int)$dateTime->format('d')); $saturdayStartTime->setDate((int)$dateTime->format('Y'), (int)$dateTime->format('m'), (int)$dateTime->format('d')); $saturdayEndTime->setDate((int)$dateTime->format('Y'), (int)$dateTime->format('m'), (int)$dateTime->format('d')); } // On a weekday if ($dayOfTheWeek < 6) { $isOpen = $dateTime > $weekdayStartTime && $dateTime < $weekdayEndTime; $message = sprintf( $templateOpen, $dateTime->format('l H:i') ); if (!$isOpen) { $message = sprintf( $templateClosed, $dateTime->format('l H:i'), $weekdayStartTime->format("H:i"), $weekdayEndTime->format("H:i") ); } } // On a saturday if ($dayOfTheWeek === 6) { $isOpen = $dateTime > $saturdayStartTime && $dateTime < $saturdayEndTime; $message = sprintf( $templateOpen, $dateTime->format('l H:i') ); if (!$isOpen) { $message = sprintf( $templateClosed, $dateTime->format('l H:i'), $saturdayStartTime->format("H:i"), $saturdayEndTime->format("H:i") ); } } echo "$message\n"; } $testDates = array( DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 08:59'), // friday DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 09:00'), DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 09:01'), DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 18:59'), DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 19:00'), DateTime::createFromFormat('d/m/Y H:i', '18/12/2015 19:01'), DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 09:59'), // saturday DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 10:00'), DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 10:01'), DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 14:59'), DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 15:00'), DateTime::createFromFormat('d/m/Y H:i', '19/12/2015 15:01'), DateTime::createFromFormat('d/m/Y H:i', '20/12/2015 11:30') // sunday ); foreach ($testDates as $testDate) { displayAllowPostsMessage($testDate, true); }
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.34, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.30, 8.3.0 - 8.3.30, 8.4.1 - 8.4.18, 8.5.0 - 8.5.3
It's Friday 08:59 and the posting are only allowed between 09:00 - 19:00. Sorry for the Inconvenience. It's Friday 09:00 and the posting are only allowed between 09:00 - 19:00. Sorry for the Inconvenience. It's Friday 09:01 and we are open! It's Friday 18:59 and we are open! It's Friday 19:00 and the posting are only allowed between 09:00 - 19:00. Sorry for the Inconvenience. It's Friday 19:01 and the posting are only allowed between 09:00 - 19:00. Sorry for the Inconvenience. It's Saturday 09:59 and the posting are only allowed between 10:00 - 15:00. Sorry for the Inconvenience. It's Saturday 10:00 and the posting are only allowed between 10:00 - 15:00. Sorry for the Inconvenience. It's Saturday 10:01 and we are open! It's Saturday 14:59 and we are open! It's Saturday 15:00 and the posting are only allowed between 10:00 - 15:00. Sorry for the Inconvenience. It's Saturday 15:01 and the posting are only allowed between 10:00 - 15:00. Sorry for the Inconvenience. It's closed on sunday.

preferences:
83.91 ms | 2516 KiB | 4 Q