[SOLVED] PHP Convert in date and update a database (PhpMyAdmin) with a date (like 22:43:50)

Issue

i don’t understand why i can’t update my DataBase with times like "21:20:46".

$tempsPassage = date(' H:i:s', 1647724846); //timestamp to date
echo $tempsPassage; // it shows corretly the time i want like "21:20:46".
   $stmt = $conn->prepare("UPDATE chrono SET time = $tempsPassage WHERE  ......");
  $stmt->execute();  

My table in PhpMyAdmin is in time unit. i already tried to update time differently like SET time = 212046 and it updated my table to 21:20:46

Solution

FROM_UNIXTIME is amysql function that will work just fine

SELECT FROM_UNIXTIME(1647724846, '%h:%i:%s');
| FROM_UNIXTIME(1647724846, '%h:%i:%s') |
| :------------------------------------ |
| 09:20:46                              |

db<>fiddle here

But your code is vulnerable to sql injection, use always place holders and parameters.

$tempsPassage = 1647724846; //timestamp to date
echo $tempsPassage; // it shows corretly the time i want like "21:20:46".
   $stmt = $conn->prepare("UPDATE chrono SET time = FROM_UNIXTIME(?, '%h:%i:%s') WHERE  ......");
   $stmt->bind_param("s", tempsPassage );
  $stmt->execute();  

And for your information phpmyadnun is a gui written in php to manage mysql databases

Answered By – nbk

Answer Checked By – Robin (BugsFixing Admin)

Leave a Reply

Your email address will not be published.