Reputation: 7
Im kind'of stuck on my project...can you help me?
What i'm trying to do is to echo in two different places either the variable $open_text
and $open_image
OR $close_text
and $close_image
based on current day and hour from the
first statement.
I've already tried with switch -> case
but it didn't work. Another idea was to use variables variable but i dont quite know how to adapt it to my project.
What i have is:
<?php
date_default_timezone_set('Europe/Rome'); // timezone
$weekday = date(l); // today
// Set open and closing time for each day of the week
if ($weekday == "Tuesday" || $weekday == "Wednesday" || $weekday == "Thursday" || $weekday == "Friday" || $weekday == "Saturday") {
$morning_open_from = "09:15";
$morning_open_to = "12:30";
$afternoon_open_from = "15:00";
$afternoon_open_to = "19:30";
}
else {
$morning_open_from = "00:00";
$morning_open_to = "00:00";
$afternoon_open_from = "00:00";
$afternoon_open_to = "00:00";
}
// check if the current time is before or after opening hours
if (date("H:i") > $morning_open_from || date("H:i") < $morning_open_to && date("H:i") > $afternoon_open_from || date("H:i") < $afternoon_open_to ) {
$open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>";
$open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>";
}
// show the checkout button
else {
$close_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>";
$close_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>";
}
?>
Thank you very much.
Upvotes: 0
Views: 87
Reputation: 1
Here you go
date_default_timezone_set('Europe/Rome'); // timezone
$weekday = date(l); // today
$openDays = array("Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
$morning_open_from = "00:00";
$morning_open_to = "00:00";
$afternoon_open_from = "00:00";
$afternoon_open_to = "00:00";
// Set open and closing time for each day of the week
if (in_array($weekday, $openDays) ) {
$morning_open_from = "09:15";
$morning_open_to = "12:30";
$afternoon_open_from = "15:00";
$afternoon_open_to = "19:30";
}
// check if the current time is before or after opening hours
if (( date("H:i") > $morning_open_from && date("H:i") < $morning_open_to )
||
( date("H:i") > $afternoon_open_from && date("H:i") < $afternoon_open_to )) {
$open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>";
$open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>";
}
// show the checkout button
else {
$close_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>";
$close_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>";
}
Upvotes: 0
Reputation: 22656
Wherever you want to echo you can either check to see if the variables are set before displaying them:
if(!empty($open_image)){
echo $open_image
}
Or, since presumably $close_image or $open_image will be displayed in the same place you can just use the same variable:
if (date("H:i") > $morning_open_from || date("H:i") < $morning_open_to && date("H:i") > $afternoon_open_from || date("H:i") < $afternoon_open_to ) {
$open_text = "<span style='font-size:14px; color: green;'>We're open, call us!</span>";
$open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>0123.456789</span>";
}else {
$open_text = "<span style='font-size:14px; color: red;'>We're closed, contact us!</span>";
$open_image = "<span style='font-size:20px; color: #000; font-family: Arial,Verdana,sans-serif;'>[email protected]</span>";
}
Then later you can just:
echo $open_text;
echo $open_image;
Upvotes: 1