duanlao6573 2014-04-17 11:50
浏览 31
已采纳

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

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

Thank you very much.

  • 写回答

2条回答 默认 最新

  • doupingzhi9674 2014-04-17 12:02
    关注

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

    Then later you can just:

    echo $open_text;
    echo $open_image;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思