Reputation: 197
I have reviewed the q/a's on here and have not found an answer for what I am trying to do. I want to put a $variable inside of an array, kind of like an echo.
Here is an example:
$days = '"12/25","12/26"';
return array($days);
I am wanting the above to look like this when the PHP page loads, so that the variable loads/echos inside the array
$days = '"12/25","12/26"';
return array("12/25","12/26")
Here is my entire code, it echos Business hours Open or Closed. As you can see, I want to be able to change the holidays dates from the top of the code to prevent from going to bottom of the page inside the code to change it. I have tried, ($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';
}
?>
To help Clarify, This is the actual working code. It displays "We are Open","We are Closed","Closed for Holidays" depending on the day of the week, time, and holidays. "Closed for Holidays" is only displayed if it is one of those days listed in Holidays. It works fine, but I was trying to change it so that if I wanted to add more days to the Holidays schedule, I could easily do it at the top of the page code, rather than scrolling down. I know lazy, but it was for production purposes.
<?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';
}
?>
Upvotes: 3
Views: 377
Reputation: 468
$holidays
is a "convenience" variable that you are treating as a constant. After the first assignment, it is never changed within your code.
In your previous implementation, $holidays
was useful as a string. With your new multi-day holidays requirement, it will be more useful to initialise it as an array of "m/d" strings.
<?php
$holidays = array("12/25", "12/26");
//...
?>
After making the above change, your HolidayList()
function becomes redundant, so remove it. Also $holidaysList
becomes redundant, so replace every instance of it with $holidays
(there is only one instance).
Upvotes: 1
Reputation: 559
I assume you wanto convert the string into array.
you can first explode them using comma as delimiter, then remove the double quote from the value and put in the days array variable.
<?php
$string = '"12/25","12/26"';
$tmps = explode(',', $string);
foreach($tmps as $tmp)
{
$days[] = str_replace("\"","", $tmp);
}
print_r($days);
Upvotes: 1