0

How can I display hour and minute only Php from mysql database like(00:00)? Here is my code:

$query = "SELECT * FROM table";
$resualt = mysqli_query($conn, $query);

if (mysqli_num_rows($resualt) > 0)
{
    echo "<table class='tb'>";
    while($row = mysqli_fetch_assoc($resualt)){
        echo "<tr><td>" . $row['id']. "</td><td> " .$row['name']. " </td><td> " . $row['place']. " </td><td> " . $row['ddate']. " </td><td> " . $row['dtime']. " </td><td> ".$row['message']."</td></tr>";
    }
    echo `enter code here`"<table>";
}
else
{
    echo "there is no record.";
}

thanks

Duane Lortie
  • 1,285
  • 1
  • 12
  • 16
buuhoodle
  • 1
  • 1
  • 4
  • 1
    Possible duplicate of [Get the Year/Month/Day from a datetime in php?](http://stackoverflow.com/questions/7247259/get-the-year-month-day-from-a-datetime-in-php) –  Dec 28 '16 at 20:46
  • Is this referring to the `dtime` column? What is an example value from that column? – Matt Altepeter Dec 28 '16 at 20:48
  • dtime column shows me this: 02:01:00.0 but i want to show me 02:00 hh:mm only in the browser – buuhoodle Dec 28 '16 at 21:00

2 Answers2

2

You query the time through MySQL with

SELECT DATE_FORMAT(NOW(), '%H:%i')

For example

$query = "SELECT *, DATE_FORMAT(NOW(), '%H:%i') as time FROM table";

Then you can access it through $row['time'];

user3606329
  • 2,405
  • 1
  • 16
  • 28
  • the time is not current time it is a time which is already in the database , when i try your code i got this error "mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given" – buuhoodle Dec 28 '16 at 21:15
  • Replace NOW() with ddate. Your error usually occurs when mysqli_query failed. Check if your table is correct. The sample query I showed you works for me fine and is syntaxically correct. Try to replace time to time123, eventually it's a reserved variable in your environment. – user3606329 Dec 28 '16 at 22:16
0

Assuming your table has a datetime column, you can pluck the hour and minutes from it like this...

 SELECT HOUR( `datecol` ) AS HOUR, MINUTE( `datecol` ) AS MINUTE FROM table WHERE BLAH=blah

You can also select other things like ....

WEEK to get the week number of the year

WEEKDAY to get the day name of the week...

Duane Lortie
  • 1,285
  • 1
  • 12
  • 16