php echo根据日期或日期开放或关闭营业时间

时间:2016-10-13 02:09:45

标签: php arrays

我已经在这里查看了q / a,但没有找到我想要做的答案。我想在数组中加一个$变量,有点像回声。

以下是一个例子:

$days = '"12/25","12/26"';

return array($days);

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

$days = '"12/25","12/26"';

return array("12/25","12/26")

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

<?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';
   }

  ?>

为了帮助澄清,这是实际的工作代码。根据星期几,时间和假日显示“我们开放”,“我们关闭”,“休假”。 “假期休息”仅在假期中列出的那些日期之一时显示。它工作正常,但我试图改变它,以便如果我想在假期计划中添加更多天,我可以轻松地在页面代码的顶部执行它,而不是向下滚动。我知道懒惰,但它是出于生产目的。

<?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';
   }

  ?>

2 个答案:

答案 0 :(得分:1)

我假设你想把字符串转换成数组。

你可以先用逗号作为分隔符来爆炸它们,然后从值中删除双引号并输入days数组变量。

<?php

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

答案 1 :(得分:1)

$holidays是&#34;方便&#34;您将其视为常量的变量。第一次分配后,代码中永远不会更改。

在之前的实现中,$holidays作为字符串非常有用。根据您的新多天假期要求,将其初始化为&#34; m / d&#34;的数组会更有用。字符串。

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

完成上述更改后,您的HolidayList()功能变得多余,因此请将其删除。此外$holidaysList变得多余,因此请用$holidays替换它的每个实例(只有一个实例)。