Convert timestamp coming from SQL database to String PHP?

PHPServer Side ProgrammingProgrammingMySQL

To convert timestamp to string, use setTimestamp(). Let’s say the following is our input i.e. timestamp −

$SQLTimestamp = 1600320600000;

We want the following output i.e. Date string −

2020-09-17 05:30:00

At first, get seconds from Timestamp −

$seconds = round($SQLTimestamp/1000, 0);

Example

 Live Demo

<!DOCTYPE html>
<html>
<body>
<?php
   $SQLTimestamp = 1600320600000;
   $seconds = round($SQLTimestamp/1000, 0);
   $PHPDateObject = new DateTime();  
   $PHPDateObject->setTimestamp($seconds);
   echo $PHPDateObject->format('Y-m-d H:i:s');
?>
</body>
</html>

Output

2020-09-17 05:30:00
raja
Published on 12-Oct-2020 16:55:20
Advertisements