使用UNIX_TIMESTAMP确定行的颜色

时间:2011-04-08 14:51:09

标签: php html timestamp

我目前有这段代码:

        $rawsql2 = "SELECT 
    *
    FROM 
        _erc_foffices n 
    INNER JOIN 
        _erc_openings o ON n.id = o.branch_id 
    INNER JOIN 
        _erc_openings_times t ON o.id = t.opening_id
    WHERE 
    (
        n.id = %d
    );";

    $sql2 = sprintf($rawsql2, mysql_real_escape_string($id));

    $result2 = mysql_query($sql2);

    /*These if & while statements decide whether or not the information should be displayed. If no results are returned from the query above then an alternative message is shown.*/
    if(mysql_num_rows($result2) > 0) {
    $count=0;
    $day = 1;
    echo "<div class='timetable'><p>";
        while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            if ($day=='1') { echo "<b>Sun - </b>";} else if ($day=='3') { echo "<b>Mon - </b>"; } else if ($day=='5') { echo "<b>Tue - </b>"; } else if ($day=='7') { echo "<b>Wed - </b>"; } else if ($day=='9') { echo "<b>Thu - </b>"; } else if ($day=='11') { echo "<b>Fri - </b>"; } else if ($day=='13') { echo "<b>Sat - </b>"; } else { echo "";}
            if ($row["open"] == 0 && $row["close"] == 0) {
     if ($day % 2 == 1) {
         echo "closed";
     }

}
            else{
            echo "" . substr($row["open"], 0, -3) . "-" . substr($row["close"], 0, -3) . " &nbsp;&nbsp; ";
            if ($count % 2 !=0 ){ echo  "<br/>"; } }
$count++;
$day++;

        }
    } else {
            echo "Error";
    }

从数据库中提取时间表,然后以下列格式显示:

太阳 - 08:00-12:30 13:30-16:30
周一至09:00-17:00 18:00-20:00等。

我现在要做的是,例如,当前说的是星期日,那么星期日行字体将是红色。这可能吗?

感谢您的帮助

2 个答案:

答案 0 :(得分:0)

PHP的本机日期时间格式是一个unix时间戳,因此您可以获取查询中检索到的值,并使用date('w', $timestamp)获取星期值。返回0(星期日)到6(星期六)范围内的值,您可以使用它来确定颜色。

答案 1 :(得分:0)

您还可以在查询中使用DAYOFWEEK mysql函数:
SELECT *, DAYOFWEEK(your_column_name) as dw .....