【问题标题】:Compare today day and shop opening days and show if opened or closed?比较今天和商店的营业日,并显示是否营业或关闭?
【发布时间】:2022-01-25 11:00:40
【问题描述】:

您好,我正在尝试在 php 中创建一个函数来检查当前日期并返回商店是否打开或关闭。这是我到目前为止所拥有的,它有效,但它很长,所以如果有人知道最短路径,请帮忙!

$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
  }
}

如果日期介于星期四和星期一之间,则返回 Opened else Closed。如果您知道捷径,请帮助我

【问题讨论】:

    标签: php


    【解决方案1】:

    如果您知道关闭的日子(您必须这样做),为什么不这样做呢?

        $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') );
    

    今天输出:

    We are "open" on Saturday
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-21
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-10
      相关资源
      最近更新 更多