gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:02:04 26 4
gpt4 key购买 nike

我已经查看了此处的问答,但没有找到我正在尝试做的事情的答案。我想在数组中放置一个 $variable,有点像 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';
}

?>

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

<?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(只有一个实例)。

关于php echo Open or Closed business hours depending on date or 日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40011167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com