开放或关闭营业时间取决于日期或日期


php echo Open or Closed business hours depending on date or date

我已经查看了这里的q/a,并没有找到我想做的事情的答案。我想在数组里面放一个$变量,有点像echo。

下面是一个例子:

$days = '"12/25","12/26"';
return array($days);

我希望上面的内容在PHP页面加载时看起来像这样,以便变量加载/返回数组

$days = '"12/25","12/26"';
return array("12/25","12/26")

这是我的整个代码,它反映营业时间开放或关闭。正如您所看到的,我希望能够从代码的顶部更改假期日期,以防止在代码的页面底部更改它。我试过了,($holidays) (holidays) ('$holidays')

<?php
$holidays = '"12/25","12/26"'; 

date_default_timezone_set('America/New_York');
// Runs the function
echo time_str();
function time_str() {
    if(IsHoliday())
    {
        return ClosedHoliday();
    }           
$dow = date('D'); // Your "now" parameter is implied
    // Time in HHMM
    $hm = (int)date("Gi");
    switch(strtolower($dow)){
            case 'mon': //MONDAY adjust hours - can adjust for lunch if needed
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;   
            case 'tue': //TUESDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'wed': //WEDNESDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'thu': //THURSDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'fri': //FRIDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'sat': //Saturday adjust hours
                return Closed();
                break;              
            case 'sun': //Saturday adjust hours
                return Closed();
                break;              
    }           
}
// List of holidays
function HolidayList()
{
// Format: 05/11 (if year/month/day comma seperated for days)
return array($holidays);
}
// Function to check if today is a holiday
function IsHoliday()
{
// Retrieves the list of holidays
$holidayList = HolidayList();
// Checks if the date is in the holidaylist  - remove Y/ if Holidays are same day each year  
if(in_array(date("m/d"),$holidayList))
{ 
    return true;
}else
{
    return false;
}   
}
// Returns the data when open
function Open()
{
    return 'We are Open';
}
// Return the data when closed
function Closed()
{
    return 'We are Closed';
}
 // Returns the data when closed due to holiday
 function ClosedHoliday()
{
       return 'Closed for Holidays';
    }
    // Returns if closed for lunch
    // if not using hours like Monday - remove all this
   // and make 'mon' case hours look like 'tue' case hours
   function Lunch()
   {
       return 'Closed for Lunch';
   }
  ?>

为了帮助澄清,这是实际的工作代码。它会根据星期几、时间和节假日显示"We are Open"、"We are Closed"、"Closed for Holidays"。"假日关闭"仅在假日中列出的其中一天才会显示。它工作得很好,但是我试图改变它,以便如果我想要在Holidays日程表中添加更多的天数,我可以很容易地在页面代码的顶部进行操作,而不是向下滚动。我知道懒惰,但这是为了生产目的。

<?php

date_default_timezone_set('America/New_York');
// Runs the function
echo time_str();
function time_str() {
    if(IsHoliday())
    {
        return ClosedHoliday();
    }           
$dow = date('D'); // Your "now" parameter is implied
    // Time in HHMM
    $hm = (int)date("Gi");
    switch(strtolower($dow)){
            case 'mon': //MONDAY adjust hours - can adjust for lunch if needed
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;   
            case 'tue': //TUESDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'wed': //WEDNESDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'thu': //THURSDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'fri': //FRIDAY adjust hours
                if ($hm >= 830 && $hm < 1700) return Open();
                else return Closed();
                break;              
            case 'sat': //Saturday adjust hours
                return Closed();
                break;              
            case 'sun': //Saturday adjust hours
                return Closed();
                break;              
    }           
}
// List of holidays
function HolidayList()
{
// Format: 05/11 (if year/month/day comma seperated for days)
return array("12/25","12/26");
}
// Function to check if today is a holiday
function IsHoliday()
{
// Retrieves the list of holidays
$holidayList = HolidayList();
// Checks if the date is in the holidaylist  - remove Y/ if Holidays are same day each year  
if(in_array(date("m/d"),$holidayList))
{ 
    return true;
}else
{
    return false;
}   
}
// Returns the data when open
function Open()
{
    return 'We are Open';
}
// Return the data when closed
function Closed()
{
    return 'We are Closed';
}
 // Returns the data when closed due to holiday
 function ClosedHoliday()
{
       return 'Closed for Holidays';
    }
    // Returns if closed for lunch
    // if not using hours like Monday - remove all this
   // and make 'mon' case hours look like 'tue' case hours
   function Lunch()
   {
       return 'Closed for Lunch';
   }
  ?>

$holidays是一个"方便"变量,您将其视为常量。在第一次赋值之后,它永远不会在代码中更改。

在前面的实现中,$holidays作为字符串是有用的。对于新的多天假期要求,将其初始化为"m/d"字符串数组将更有用。

<?php
$holidays = array("12/25", "12/26");
//...
?>

进行上述更改后,您的HolidayList()函数变得多余,因此将其删除。$holidaysList也变得多余,所以用$holidays替换它的每个实例(只有一个实例)。

我假设您想将字符串转换为数组。

您可以首先使用逗号作为分隔符将它们分开,然后从值中删除双引号并放入days数组变量。

<?php
$string = '"12/25","12/26"';
$tmps = explode(',', $string);
foreach($tmps as $tmp)
{
  $days[] = str_replace("'"","", $tmp);
}
print_r($days);