Daniel H
Daniel H

Reputation: 2925

Formatting time in PHP rows

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

Upvotes: 1

Views: 127

Answers (4)

ayk
ayk

Reputation: 1340

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

Upvotes: 3

Haim Evgi
Haim Evgi

Reputation: 125496

you can use substr

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

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

Upvotes: 0

Soul Reaver
Soul Reaver

Reputation: 2092

This should work:

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

Upvotes: 2

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

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

Upvotes: 0

Related Questions