-2

I am looking to create a bespoke bit of PHP to work out based on the time of day whether a shop is open or closed.

The shop opening times are: Mon-Sat: 0700-1300, 1400-1800 (e.g. closed between 13:00 and 14:00), Sun: 0700-1230

So, I need the code to work out:

If its Mon,Tues,Wed,Thur,Fri or Sat and between 07:00 and 13:00, or between 14:00 and 18:00, or if its Sun between 07:00 and 12:30 echo

OPEN

else echo CLOSED.

Cant seem to get it to work... What I have is a bunch of IF statements in isolation from each other which works - but I need it to work in one IF statement..

What I have is below:

<!-- Is it a sunday? -->
<?php if (date('l') >= Monday || Tuesday || Wednesday || Thursday || Friday || Saturday) : ?>
<p>It's not Sunday</p>
<?php $dayType = 1; ?>
<?php else : ?>
<p>It is Sunday</p>
<?php $dayType = 2; ?>
<?php endif; ?>

<!-- is it between 8 and 18? -->
<?php if ($dayType = 1 && current_time('H') >= 8 && current_time('H') < 18) : ?>
<p>Not Sunday and Open</p>
<?php $dayTypeStatus = 11; ?>
<?php else : ?>
<p>Not Sunday and Closed</p>
<?php $dayTypeStatus = 10; ?>
<?php endif; ?>

<!-- Is it between 13 and 14? -->
<?php if ($dayType = 1 && $dayTypeStatus = 11 && current_time('H') >= 13 && current_time('H') < 14) : ?>
<p>Not Sunday and Open - Lunchtime</p>
<?php $dayTypeStatusLunch = 111; ?>
<?php else : ?>
<p>Not Sunday and Open - Not Lunchtime</p>
<?php $dayTypeStatusLunch = 110; ?>
<?php endif; ?>

<!-- Is it between 8 and 12:30 on a sunday? -->
<?php if (date('l') >= Sunday && current_time('H') >= 8 && current_time('H:i') < "12:30") : ?>
<p>Sunday Open</p>
<?php else : ?>
<p>Sunday Closed</p>
<?php endif; ?>

Any ideas to make it work?

Better attempt at code is as below:

<?php if (date('l') != Sunday && current_time('H') >= 7 && current_time('H') < 18) : ?>
<p>OPEN</p>
<?php else : ?>
<p>CLOSED</p>
<?php endif; ?>

But still missing the lunchtime close element and also including the sunday opening hours check - e.g. this works to check if its not a sunday and is between 0700 and 1800.

UPDATE - think this works???

<?php if (date('l') != "Sunday" && current_time('H') >= 7 && current_time('H') < 13 || current_time('H') >= 14 && current_time('H') < 18 || date('l') == "Sunday" && current_time('H') >= 8 && current_time('H:i') < "12:30") : ?>
<p>OPEN</p>
<?php else : ?>
<p>CLOSED</p>
<?php endif; ?>
dubbs
  • 1,027
  • 1
  • 11
  • 30
  • `date('l') >= Monday || Tuesday ...` is absolute nonsense. This is actually supposed to work?! – deceze Jul 14 '16 at 15:10
  • See update for the more recent version I have which is getting closer – dubbs Jul 14 '16 at 15:12
  • 1
    `date('l') != Sunday` unless you've defined `Sunday` as a constant the PHP interpreter should be throwing a wobbly... – CD001 Jul 14 '16 at 15:13
  • OK... as you can tell I am a PHP novice.. so welcome your assistance and skills here chaps as I clearly need some heavy guidance! ;) – dubbs Jul 14 '16 at 15:16
  • @dubbs at the place of `Sunday` it should be `"Sunday"` , as the `Sunday` is not defined as constant , so you have to make it a string by putting it inside quotes. – Arsh Singh Jul 14 '16 at 15:19
  • I'd recommend taking a look at the `DateTime` class (http://php.net/manual/en/class.datetime.php) in PHP that should get you a decent idea of what's possible... and remember strings need to be quoted one way or another (http://php.net/manual/en/language.types.string.php) ... and you probably want to switch on error trapping (http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display), PHP does give generally reasonably useful pointers about what's going wrong and where. – CD001 Jul 14 '16 at 15:20
  • Thanks - think I have just got it working - see latest edit to the question above... Let me know? – dubbs Jul 14 '16 at 15:40

2 Answers2

0

What about this?

<?php
///
if(date('w') !== 0) { //not sunday
  if((date('H')<18 && date('H')>=14) || (date('H')<13 && date('H')>=7)) {
    echo 'OPEN';
  }
  else {
    echo 'CLOSED';
  }
}
else if(intval(date('Hi'))<1230 && date('H')>=7) {
  echo 'OPEN';
}
else {
  echo 'CLOSED';
}
0

You could form a binary flag, based on the conditions and watch the current state by output code.

<?php
        // not sunday
$lol = (date('N')<7) << 0 | 
(current_time('H') >= 8 && current_time('H') < 18) << 1 | 
(current_time('H') >= 13 && current_time('H') < 14) << 2 |
(current_time('H') >= 8 && current_time('H:i') < "12:30") << 3;

That will give you an unique code for each situation

fiddle

xottabych007
  • 142
  • 2
  • 10