0

I've been working on a script that will take one of the two provided divs and display the proper one depending what our hours are on that day. I'm unsure of a better way to accomplish what I need, my knowledge is very limited in this language and is coming from a design necessity for a project.

$date = date("w"); //0-6 Day of Week
$currentHour = date("H");
$currentHour2 = date("Hi");

//Tuesday, Thursday, Friday
if ($date == 2 || $date == 4 || $date == 5) {
    $openTime = 8; $closeTime = 18;
} elseif ($date == 3) {
    //Wednesday
    $openTime = 10; 
    $closeTime = 18;
} elseif ($date == 6) {
    //Saturday
    $openTime = 10; 
    $closeTime = 17;
} elseif ($date == 1 || $date == 0) {
    //Sunday, Monday
    $openTime = 12; 
    $closeTime = 13; 
    $openTime2 = 1830; 
    $closeTime2 = 1930;
} 

if ($currentHour < $openTime || $currentHour > $closeTime) {
    $OffOn = 0;
} elseif ($currentHour2 < $openTime2 || $currentHour2 > $closeTime2) {
    $OffOn = 0;
} else {
    $OffOn = 1;
}

The block of code below is a simplified example of the end result of this code - written so you understand what exactly I'm doing with the variables above.

if ($OffOn == 1) { 
    <a>We Are Current Open</a> 
}else{ 
    <a>We Are Current Closed</a> 
} 

Help would be appreciated.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Kingsland
  • 9
  • 1
  • Better formatting, using arrays instead of many vars, but hey if it works, ship it ;p – Lawrence Cherone Jul 15 '18 at 00:58
  • So what EXACTLY is the problem? What is happening or what is not happening. – RiggsFolly Jul 15 '18 at 01:09
  • Add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Jul 15 '18 at 01:09

4 Answers4

0

I'd suggest to do a configuration like in the isOpen function. Doing it like this wont make you change your code if some opening-times change. You'll just have to change the config-array.

<?php
function isOpen( $timestamp ) {
    $times = [
        0 => [[1200,1300],[1830,1930]], // Sunday
        1 => [[1200,1300],[1830,1930]], // Monday
        2 => [[800,1800]], // Tuesday
        3 => [[1000,1800]], // Wednesday
        4 => [[800,1800]], // Thursday
        5 => [[800,1800]], // Friday
        6 => [[1000,1700]], // Saturday
    ];

    $open = false;
    $weekday = date('w', $timestamp);
    $hour_and_minute = date('Hi', $timestamp);

    foreach($times[$weekday] as $opening_times) {
        if(
            $hour_and_minute >= $opening_times[0]
            && $hour_and_minute <= $opening_times[1]
        ) {
            $open = true;
            break;
        }
    }

    // For testing only
    echo date( 'Y-m-d H:i', $timestamp ) . " " . date('l', $timestamp) . " is " . ($open?'open':'closed');
    echo '<br>';

    return $open;
}

// Testing
isOpen( time() ); // now
isOpen( strToTime( '1980-01-01 15:30:00' ) );
isOpen( strToTime( '1980-01-02 19:30:00' ) );
isOpen( strToTime( '1980-01-03 07:30:00' ) );
isOpen( strToTime( '1980-01-03 08:00:00' ) );

// in your template you'd write
/*

    echo "We are currently ";
    if(isOpen(time())) {
        echo "open";
    } else {
        echo "closed";
    }


*/

Output

2018-07-14 21:29 Saturday is closed
1980-01-01 15:30 Tuesday is open
1980-01-02 19:30 Wednesday is closed
1980-01-03 07:30 Thursday is closed
1980-01-03 08:00 Thursday is open
SirPilan
  • 4,649
  • 2
  • 13
  • 26
0

For the comparisons, you can use PHP's strtotime function.

Here is how you may use it in this case, also using arrays to store the hours for readability:

$openTimes = [
    'Sunday' => [
        ['1200', '1300'],
        ['1830', '1930']
    ],
    'Monday' =>  [
        ['1000', '1800'],
        ['1830', '1930']
    ],
    'Tuesday' =>  [
        ['0800', '1800']
    ],
    'Wednesday' =>  [
        ['1000', '1800']
    ],
    'Thursday' =>  [
        ['0800', '1800']
    ],
    'Friday' =>  [
        ['0800', '1800']
    ],
    'Saturday' =>  [
        ['1200', '1300'],
        ['1830', '1930']
    ],
];

$dayOfWeek = date("l");
$currentHour = strtotime(date("hi"));

$isOpen = false;
foreach ($openTimes[$dayOfWeek] as $times) {
    $open = strtotime($times[0]);
    $close = strtotime($times[1]);
    if ($currentHour > $open && $currentHour < $close) {
        $isOpen = true;
    }
}

if($isOpen) {
    echo "<div>We are open!</div>";
} else {
    echo "<div>We are closed!</div>";
}
naturaljoin
  • 475
  • 2
  • 7
0

here's the way I would handle this with more verbose array setup so you can see what is going on...

<?php

function is_open()
{

    $days = array(
        0 => array( // Sunday
            'Day Shift' => array(
                'Start' => '12:00',
                'End' => '13:00'
            ),
            'Night Shift' => array(
                'Start' => '18:30',
                'End' => '19:30'
            )
        ),
        1 => array( // Monday
            'Day Shift' => array(
                'Start' => '12:00',
                'End' => '13:00'
            ),
            'Night Shift' => array(
                'Start' => '18:30',
                'End' => '19:30'
            )
        ),
        2 => array( // Tuesday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        3 => array( // Wednesday
            'Day Shift' => array(
                'Start' => '10:00',
                'End' => '18:00'
            )
        ),
        4 => array( // Thursday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        5 => array( // Friday
            'Day Shift' => array(
                'Start' => '08:00',
                'End' => '18:00'
            )
        ),
        6 => array( // Saturday
            'Day Shift' => array(
                'Start' => '10:00',
                'End' => '17:00'
            )
        )
    );

    $today_index = date('w');
    $time_now = date('H:i');

    foreach($days[date('w')] as $shift)
    {

        if($today_index == $days[date('w')] && $time_now >= $shift['Start'] && $time_now < $shift['End'])
        {

            return true;

        }

    }

    return false;

}

if(is_open())
{

    echo '<a>We Are Current Open</a>';

} else {

    echo '<a>We Are Current Closed</a>';

}
Simon K
  • 1,503
  • 1
  • 8
  • 10
0

This is how it would be done in php

$date = date("w"); //0-6 Day of Week
$currentHour = (int)date("H");
$daysHours = array(array(1200,1300),array(1830,1900),array(1000,1800),array(800,1800),array(800,1800), array(800,1800), array(1000,1700));//days of week with open and close hours

if($currentHour < $daysHours[$date][0] || $currentHour > $daysHours[$date][1] ){
$OffOn = 0;
    echo "div is off ";
    return;
}
$OffOn = 1;
echo "div is on";
mytimenow
  • 16
  • 3