AlwaysLearning
AlwaysLearning

Reputation: 197

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';}
?>

Upvotes: 0

Views: 709

Answers (4)

AlwaysLearning
AlwaysLearning

Reputation: 197

This one works, added remarks to explain as much as possible.

<?php

date_default_timezone_set('America/New_York');

// Runs the function
echo time_str();

function time_str() {

    if(IsHoliday())
    {
        return ClosedHoliday();
    }           

$dow = date('D'); // Your "now" parameter is implied

    // Time in HHMM
    $hm = (int)date("Gi");

    switch(strtolower($dow)){
            case 'mon': //MONDAY adjust hours - can adjust for lunch if needed
                if ($hm >=    0 && $hm <  830) return Closed();
                if ($hm >= 830 && $hm < 1200) return Open();
                if ($hm >= 1200 && $hm < 1300) return Lunch();
                if ($hm >= 1300 && $hm < 1730) return Open();
                if ($hm >= 1730 && $hm < 2359) return Closed();
                break;
            case 'tue': //TUESDAY adjust hours
                if ($hm >= 830 && $hm < 1730) return Open();
                else return Closed();
                break;              
            case 'wed': //WEDNESDAY adjust hours
                if ($hm >= 830 && $hm < 1730) return Open();
                else return Closed();
                break;              
            case 'thu': //THURSDAY adjust hours
                if ($hm >= 830 && $hm < 1730) return Open();
                else return Closed();
                break;              
            case 'fri': //FRIDAY adjust hours
                if ($hm >= 830 && $hm < 1730) return Open();
                else return Closed();
                break;              
            case 'sat': //Saturday adjust hours
                return Closed();
                break;              
            case 'sun': //Saturday adjust hours
                return Closed();
                break;              

    }           
}

// List of holidays
function HolidayList()
{
// Format: 2009/05/11 (year/month/day comma seperated for days)
return array("2016/11/24","2016/12/25");
}

// Function to check if today is a holiday
function IsHoliday()
{
// Retrieves the list of holidays
$holidayList = HolidayList();
// Checks if the date is in the holidaylist  - remove Y/ if Holidays are same day each year  
if(in_array(date("Y/m/d"),$holidayList))
{ 
    return true;
}else
{
    return false;
}   
}

// Returns the data when open
function Open()
{
    return 'Yes we are Open';
}

// Return the data when closed
function Closed()
{
    return 'Sorry We are Closed';
}   

// Returns the data when closed due to holiday
function ClosedHoliday()
{
    return 'Closed for the Holiday';
}

// Returns if closed for lunch
// if not using hours like Monday - remove all this
// and make 'mon' case hours look like 'tue' case hours
function Lunch()
{
    return 'Closed for Lunch';
}

?>

Upvotes: 0

Robert Wade
Robert Wade

Reputation: 5003

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();

Upvotes: 2

Use a switch statement:

$thisDate = date('D Hi');

$hoursOfOpArray = array("Mon_Open" => "815", "Mon_Close" => "1730", "Tue_Open" => "815", "Tue_Close" => "1730"); //repeat for all days too fill this array

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

switch($day) {
    case "Sun":
        $status = "Closed";
    break;
    case "Mon":
        $status = ($time < $hoursOfOpArray[$day . "_Open"] || $time > $hoursOfOpArray[$day . "_Close"]) ? "Closed" : "Open";
    break;
    //same as Monday case for all other days
}

echo $status;

This should also work:

echo ($day === 'Sun' || ($time < $hoursOfOpArray[$day . "_Open"]) || ($time > $hoursOfOpArray[$day . "_Close"])) ? "Closed" : "Open";

Upvotes: 1

JOUM
JOUM

Reputation: 254

$o = ['Mon' => [815, 1735], /*and all other days*/'Sat' => [815, 1300]];
echo (date('Hi')>=$o[date('D')][0] && date('Hi')<=$o[date('D')][1]) ? "open": "closed";

Done! And dont ask.

Upvotes: 0

Related Questions