2011-04-08 33 views
0

的颜色我现在有这样的代码:使用UNIX_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等

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

感谢所有帮助

+0

这是可能的,当然。检查[strtotime()](http://php.net/manual/en/function.strtotime.php)和[date()](http://php.net/manual/en/function.date.php)功能。但是,如果你已经有日期名称,我没有看到任何问题? – Wh1T3h4Ck5 2011-04-08 14:58:25

回答

0

PHP的本地日期时间格式是Unix时间戳,所以你可以在你的查询检索的价值,并与date('w', $timestamp)得到一天的周值。返回0(星期日)至6(星期六)的值,您可以使用它来确定颜色。

+0

嗨,感谢您的回复。我发现很难理解答复和如何做到这一点。你介意编辑我的代码,以便我可以学习如何做到这一点? – 2011-04-08 15:07:58

+0

我现在修复了它:) – 2011-04-08 15:57:42

0

你也可以使用MySQL的DAYOFWEEK功能查询:
SELECT *, DAYOFWEEK(your_column_name) as dw .....