Reputation: 1
Hi I'm trying to create a function in php that checks the current day and return if the store is open or closed. This is what I have so far, it works but it's very long so if anyone knows shortest path then please help !
$today = date('w'); // today
$starting = "THUR"; //opening day
$closing = "MON"; //closing day
$openday = date("w", strtotime($starting)); //coverting to number
$closeday = date("w", strtotime($closing)); // coverting to number
//checking if closing day is greater than opening day
if($openday < $closeday ){
//checking if today lies between starting and closing day
if($today > $openday && $today <= $closeday ){
$stat = 'Opened'; //if today lies between starting day and ending day then Opened
}
else{
$stat = 'Closed'; //else Closed
}
}
// if closing day is less than opening day
else{
//checking if today lies between opening day and friday
if($today > $openday && $today <= 6){
$stat = 'Opened';
}
//checking if today lies between sunday and closeday
else if( $today > 0 && $today <= $closeday ){
$stat = 'Opened';
}
else{
$stat = 'Closed'; // else Closed
}
}
This returns if day lies between Thursday and Monday Opened else Closed. If you know short way to do then please help me
Upvotes: 0
Views: 36
Reputation: 33813
If you know the days when you are closed ( which you must do ) why not something like this?
$closed=array(
date('w',strtotime('Tuesday')),
date('w',strtotime('Wednesday'))
);
$status=in_array( date('w'),$closed ) ? 'Closed' : 'Open';
printf('We are "%s" on %s', $status, date('l') );
which today outputs:
We are "open" on Saturday
Upvotes: 1