格式化PHP行的时间

时间:2011-04-04 08:20:16

标签: php mysql time if-statement

我目前正在使用此代码:

     if(mysql_num_rows($result2) > 0) {
    $count=0;
    $day = 1;
        while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {
            echo "<b>";
            if ($day=='1') { echo "Sunday - ";} else if ($day=='3') { echo "Monday - "; } else if ($day=='5') { echo "Tuesday - "; } else if ($day=='7') { echo "Wednesday - "; } else if ($day=='9') { echo "Thursday - "; } else if ($day=='11') { echo "Friday - "; } else if ($day=='13') { echo "Saturday - "; } else { echo "";}
            if ($row["open"] == 0 && $row["close"] == 0) {
     if ($day % 2 == 1) {
         echo "closed";
     }

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

        }
    } else {
            echo "Error";
    }

显示输出,如:

 Sunday 09:00:00 - 15:00:00  16:00:00 - 20:00:00
 Monday 09:00:00 - 15:00:00  16:00:00 - 20:00:00

我想格式化这些时间,以便只显示小时和分钟(基本上,删除秒)。有人可以告诉我,我该怎么做?

感谢您的帮助

4 个答案:

答案 0 :(得分:3)

您可以使用substr替换$ row [“open”]和$ row [“close”]变量并删除最后三个字符:

substr($row["open"], 0, -3);
substr($row["close"], 0, -3);

看起来像这样:

echo "</b>" . substr($row["open"], 0, -3) . "-" . substr($row["close"], 0, -3) . "&nbsp;&nbsp;&nbsp;&nbsp;";

答案 1 :(得分:2)

这应该有效:

date( "H:i", strtotime( $row["open"] ) )

答案 2 :(得分:0)

 echo "</b>" . substr($row["open"], 5) . "-" . substr($row["close"], 5);

答案 3 :(得分:0)

您可以使用substr

substr($row["open"], 0, -3)

substr($row["close"], 0, -3)