需要帮助为在线商店创建日期/时间功能

时间:2022-05-10 01:57:20

I am writing a function that will output to the user if a store is open or not.

我正在编写一个函数,如果商店是否打开将输出给用户。

I wrote a fucntion named is_store_open() with a variable called $day which will have the current day of the week assigned to it.

我写了一个名为is_store_open()的函数,其中包含一个名为$ day的变量,该变量将分配给它的当前星期几。

Lets say it is Saturday. it finds the case for "Saturday" and checks the opening time and closing time for Saturday.

让我们说是星期六。它找到“星期六”的情况并检查星期六的开放时间和结束时间。

The problem that I am facing, is when the store is open past midnight on Saturday. It then won't check the case for Saturday.

我面临的问题是,商店周六午夜开放。然后它不会检查星期六的情况。

Instead will check the case for Sunday. Which is not what I want, because it will show the store as closed when its open still open on "Saturday"

相反,将检查周日的情况。这不是我想要的,因为它会在“星期六”打开时仍显示商店已关闭

I wrote a function below that checks to see if the previous day closing hours are greater then the current time. If its true then return true and I guess that should fix the problem.

我在下面编写了一个函数,用于检查前一天的结束时间是否大于当前时间。如果它的真实然后返回true,我想这应该解决问题。

But when I wrote that it would give me an error saying that $saturday_close is undefined. I'm guessing its because its in a switch statement, and it does not equal to that day.

但是当我写到它会给我一个错误,说$ saturday_close是未定义的。我猜它是因为它在switch语句中,并且它不等于那一天。

I'm a little bit confused on how to set this up. If anyone has a suggestion I would really appreciate your help. Thanks!

我对如何设置它有点困惑。如果有人有任何建议我会非常感谢你的帮助。谢谢!

 function is_store_open(){
    $set_time = strtotime("tomorrow"); // Lets just sat its Sunday for testing purposes

     $day =  date("l", $set_time); 

     $time_now =  mktime("00", "00", "00"); // Now lets say the time now is 12:00am

    switch ($day) {

     case "Saturday":
     $open = "11:00";
     $saturday_close = "1:00";
      break; 


     case "Sunday"; 
   if($saturday_close > $time_now ){//If the $monday_close hours are greater then the    current     time return false the store is still open
    return true;
      } else {
   $open = "2:00";
   $close = "22:00";
   }
   break;

     }

1 个解决方案

#1


0  

class StoreOperationHours {


 public $time_now;  

 public function __construct() {
//$this->time_now =  mktime("2", "00", "00" ); // Now lets say the time now is 12:00am;
  $this->time_now = mktime(date('H'), date('i'), date('s'));
 }


 public $SaturdayOpenHours = "11:00";
 public $SaturdayClosingHours = "1:08:00";

 public $SundayOpenHours = "11:00";
 public $SundayCloseHours = "23:00";




 public function IsStoreOpen() {


    //$set_time = strtotime("tomorrow"); // Lets just sat its Sunday for testing purposes
    $day =  date("l", time()); 


    switch ($day) {


    case "Saturday":
    $this->SaturdayOpenHours;
    $this->SaturdayClosingHours;
     break; 


    case "Sunday"; 
    if(strtotime($this->SaturdayClosingHours) >= $this->time_now ){//If the $monday_close hours are greater   then the current time return false the store is still open

    return true;
    } elseif($e) {
     return false;
    }
    break;

#1


0  

class StoreOperationHours {


 public $time_now;  

 public function __construct() {
//$this->time_now =  mktime("2", "00", "00" ); // Now lets say the time now is 12:00am;
  $this->time_now = mktime(date('H'), date('i'), date('s'));
 }


 public $SaturdayOpenHours = "11:00";
 public $SaturdayClosingHours = "1:08:00";

 public $SundayOpenHours = "11:00";
 public $SundayCloseHours = "23:00";




 public function IsStoreOpen() {


    //$set_time = strtotime("tomorrow"); // Lets just sat its Sunday for testing purposes
    $day =  date("l", time()); 


    switch ($day) {


    case "Saturday":
    $this->SaturdayOpenHours;
    $this->SaturdayClosingHours;
     break; 


    case "Sunday"; 
    if(strtotime($this->SaturdayClosingHours) >= $this->time_now ){//If the $monday_close hours are greater   then the current time return false the store is still open

    return true;
    } elseif($e) {
     return false;
    }
    break;