skip to Main Content

I have a form where you can fill out Firstname, Lastname etc. And the information will get sent to a database along with the date (yyyy-mm-dd hh-mm-ss). In my table it will display the full date but i only want it to display the hours and seconds. But year, month etc. in the database.

So how do i only fetch the hours and minutes from the database and display it in a table?

HTML:

<tr>
    <th>dato</th>
    <th>Båtnr</th>
    <th>Fornavn</th>
    <th>Etternavn</th>
    <th>Tid</th>
    <th>Kr</th>
</tr>

PHP:

<?php
    $sok = @$_POST['searchh'];
    $sql = "SELECT dato, baatnr, fornavn, etternavn, tid, kr FROM utleie WHERE baatnr     LIKE '%$sok%' or fornavn LIKE '%$sok%' or etternavn LIKE '%$sok%' or tid LIKE     '%$sok%'";
    $result = $conn-> query($sql);

if ($result-> num_rows > 0) {
    while ($row = $result-> fetch_assoc()) {
        echo "<tr><td>". 
        $row["dato"] ."</td><td>".
        $row["baatnr"] ."</td><td>". 
        $row["fornavn"] ."</td><td>". 
        $row["etternavn"] ."</td><td>". 
        $row["tid"] ."</td><td>". 
        $row["kr"] ."</td></tr>";
    }
echo "</table>";
}
else {
    echo "0 results";

database date enter image description here

5

Answers


  1. use date_format() of mysql

    date_format(column,'%h:%i') as column

    Login or Signup to reply.
  2. You can use time() function for getting the current hour and minutes:

    echo date('H:i');
    
    Login or Signup to reply.
  3. You will have to split your date value which you will get from the query result.
    For splitting the date in hours and minutes you can use the explode function.

    Ex :

    <?php
    
    $date = '2016-08-01 17:56:18';
    
    $hours_minutes = explode(" ",$date);
    
    print_r($new);
    
    echo "hours =".$hours_minutes[1];
    
    //Output hours =17:56:18
    

    Here I have splitted the date field using ” ” (space) and fetched the second value of array.

    Note : this will be handled on the php end and not on the database part because in your database you have already inserted the value in timestamp format.

    Login or Signup to reply.
  4. $date = '2016-08-01 17:56:18';
    $var = strtotime($date);
    
    echo date('H:i',$var);
    

    or

    echo date('H:i',strtotime('2016-08-01 17:56:18'));
    

    Hope This Help

    Login or Signup to reply.
  5. you can do it without any change from database side.
    you can use php date function to manipulate it like:

    while ($row = $result-> fetch_assoc()) {
            echo "<tr><td>".  
            date('H:i',strtotime($row["dato"])) ."</td><td>".
    

    this will convert your database date to HH:mm format.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search