lostInTheTetons
lostInTheTetons

Reputation: 1222

Create a function checking the day and time and return open or closed php

Hi I'm trying to create a function in php that checks the current day and business hours of that day and return if the store is open or closed. This is what I have so far, but it always comes back as closed.

$day = date( 'l' );
function get_biz_hours_openclosed( $hours_openclosed ) {
$day = 'Thursday';
$t = date("H:i a");
$d = date('l');
$dowstart = '9:00 am';
$dowend = '21:00 pm';
$openclosed = ''; //default return string to empty string
echo $hours_openclosed;
    if ( ($day == $d ) && ($dowstart < $t) && ($t < $dowend) ) {
        $openclosed = 'Open!';
    }
    else{ 
        $openclosed = 'Closed'; 
    }
return $openclosed; 
}

echo get_biz_hours_openclosed( $day );
echo "<br>";
echo "current time: " . date("H:i a");

Upvotes: 0

Views: 106

Answers (3)

btc4cash
btc4cash

Reputation: 325

From a delivery system project I made, I'm sure you can find what you want with this function :

function is_between_hours($h1 = array(), $h2 = array()){
    $est_hour = date('H');
    $h1 = ($h1[1] == 'am') ? $h1[0] : $h1[0]+12;
    $h1 = ($h1 === 24) ? 12 : $h1;
    $h2 = ($h2[1] == 'am') ? $h2[0] : $h2[0]+12;
    $h2 = ($h2 === 24) ? 12 : $h2;        
    echo "Current time:";
    echo $est_hour;
    echo "Opening time:";
    echo $h1;
    echo "Close time:";
    echo $h2;
    if ( $est_hour >= $h1 && $est_hour < $h2 ) {
        echo "store open";}
        else {
            echo "store close";}}

Upvotes: 1

lostInTheTetons
lostInTheTetons

Reputation: 1222

I figured it out

$day = date( 'l' );

function get_biz_hours_openclosed( $hours_openclosed ) {

$day = 'Friday'; // testing day of the week to see if it compares correctly
$d = date('l'); // current day 
$dowstart = '9:00 am'; // testing with an example open hour
$dowend = '9:00 pm'; // testing with an example close hour
$openclosed = ''; // default return string to empty string
echo $hours_openclosed;

if ( $day == $d && time() > strtotime($dowstart) && time() < strtotime($dowend) ) {
    $openclosed = "Open!";
} else { 
    $openclosed = "Closed"; 
}

return "<span>&nbsp;</span>" . $openclosed; 

}

echo get_biz_hours_openclosed( $day );
echo "<br>";
echo "current time: " . date("g:i a");

Upvotes: 0

user2195582
user2195582

Reputation: 57

if ( ($day == $d )){ 

  if($t == '12:00pm'){
           $openclosed = 'Open!';
        }
  if($t <$dowend ) {
       $openclosed = 'Open!';
   }

  if(($t <'12:00pm') && ($t >='9:00am')){
        $openclosed = 'Open!';
           }

}
else{ 
    $openclosed = 'Closed'; 
}

not sure with the syntax (Not familiar with php)

Upvotes: 1

Related Questions