开发者

Formatting time in PHP rows

开发者 https://www.devze.com 2023-02-22 09:04 出处:网络
I\'m currently using this code: if(mysql_num_rows($result2) > 0) { $count=0; $day = 1; while ($row = mysql_fetch_array($result2, MYSQL_ASSOC)) {

I'm currently using this code:

     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";
    }

which displays an output such as:

 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

I would like to format these times so that only the hour and minute is shown (basically, remove the seconds). Could someone tell me how I'd do this please?

Thanks for any help


You can replace your $row["open"] and $row["close"] variable with substr and cut last three characters:

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

Will look like this:

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


This should work:

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


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


you can use substr

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

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

精彩评论

暂无评论...
验证码 换一张
取 消