Home > Mobile >  How to separate date and time in php when displaying data in loop
How to separate date and time in php when displaying data in loop

Time:09-08

I can't figure out how to separate the date and time when displaying the data from the database table.

if($num>0){
    while($row = mysqli_fetch_assoc($result)){

    echo "<tr>";
    echo "<td>" . $row['Id'] . "</td>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Number'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td>" . $row['Password'] . "</td>";
    echo "<td>" . $row['CNIC'] . "</td>";
    echo "<td>" . $row['dt'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
}

CodePudding user response:

If your dt field contains a properly formatted datetime string (i.e. the MySQL column data type is set to datetime) then you can create a new DateTime object in PHP and format it however you want.

$dt = new DateTime($row['dt']);
$time = $dt->format('h:i:s');
$date = $dt->format('Y-m-d');

CodePudding user response:

After the while..

$date = date("Y-m-d", $row['dt']);
$time = date("h:i:sa", $row['dt']);

then use those variables.

It works when dt is a unix timestamp or parsable date format.

  • Related