基于其他两个变量回显两个变量

时间:2014-04-17 11:50:55

标签: php variables

我很喜欢我的项目......你能帮助我吗?

我要做的是在两个不同的地方回应变量$open_text$open_image$close_text$close_image基于当天和小时 第一个声明。

我已经尝试了switch -> case,但它没有用。另一个想法是使用变量变量,但我不知道如何使其适应我的项目。 我拥有的是:

<?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;'>webmaster@webmaster.com</span>";
}
?>

非常感谢。

2 个答案:

答案 0 :(得分:1)

无论您想要回显什么,您都可以在显示之前检查变量是否已设置:

if(!empty($open_image)){
    echo $open_image
}

或者,因为大概$ close_image或$ open_image会显示在同一个地方,你可以使用相同的变量:

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;'>webmaster@webmaster.com</span>";
}

然后你可以:

echo $open_text;
echo $open_image;

答案 1 :(得分:0)

你去吧

    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;'>webmaster@webmaster.com</span>";
    }