AlwaysLearning AlwaysLearning - 2 months ago 9
PHP Question

PHP if specific business day of week and time echo

The following code works to echo "Open" or "Closed" if time is between 8:15am and 5:30pm. I am trying to make it day specific. How can I incorporate format character 'D' as example, Mon hours 8:15am - 5:30pm .. echo "Open", Sat hours 8:15am - 1:00pm "Open". I want to be able to control echo of Open/Closed by each day and time.

current working code for hours only

<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('Hi');
$open = "yah hoo, we are open";
$closed = "by golly, im closed";

if ($hour >= 0815 && $hour <=1735) {
// between 8:15am and 5:35pm
echo "$open";
} else {
echo "$closed";
}
?>


example of what I am trying to do:

$hour = (int) date('D Hi');
if ($hours >= 0815 && $hour <=1735 && $hour === 'Mon')
{ echo "$open"; }
else { echo "$closed"; }
if ($hours >= 0815 && $hour <=1300 && $hour === 'Sat')
{ echo "$open"; }
else { echo "$closed"; }


another example per The One and Only's answer which looks close to what I am looking for, but this also does not work

<?php
$openDaysArray = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat','Sun');

$thisDate = date('D Hi');

$explode = explode(" ", $thisDate);
$day = $explode[0];
$time = $explode[1];

if (in_array($day, $openDaysArray))
if ($time < 815 || $time > 1730 && $day === 'Mon');
if ($time < 815 || $time > 1730 && $day === 'Tue');
if ($time < 815 || $time > 1730 && $day === 'Wed');
if ($time < 815 || $time > 1730 && $day === 'Thu');
if ($time < 815 || $time > 1730 && $day === 'Fri');
if ($time < 815 || $time > 1730 && $day === 'Sat');
if ($time < 815 || $time > 1730 && $day === 'Sun');
{echo 'Open';}
else {echo 'Closed';}
?>

Answer

I'd handle it this way. Set up an array of all your open times. If you know you're closed on Saturday and Sunday, there's really no need to proceed with with checking times at that point, so kill the process there first. Then simply find out what day of the week it is, look up the corresponding opening and closing times in your $hours array, create actual DateTime objects to compare (rather than integers). Then just return the appropriate message.

function getStatus() {

    $hours = array(
        'Mon' => ['open'=>'08:15', 'close'=>'17:35'],
        'Tue' => ['open'=>'08:15', 'close'=>'17:35'],
        'Wed' => ['open'=>'08:15', 'close'=>'17:35'],
        'Thu' => ['open'=>'08:15', 'close'=>'22:35'],
        'Fri' => ['open'=>'08:15', 'close'=>'17:35']
    );

    $now = new DateTime();
    $day = date("D");

    if ($day == "Sat" || $day == "Sun") {
        return "Sorry we're closed on weekends'.";
    }

    $openingTime = new DateTime();
    $closingTime = new DateTime();

    $oArray = explode(":",$hours[$day]['open']);
    $cArray = explode(":",$hours[$day]['close']);

    $openingTime->setTime($oArray[0],$oArray[1]);
    $closingTime->setTime($cArray[0],$cArray[1]);

    if ($now >= $openingTime && $now < $closingTime) {
        return "Hey We're Open!";
    }

    return "Sorry folks, park's closed. The moose out front should have told ya.";
}

echo getStatus();