Convert timestamp coming from SQL database to String PHP?


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

Updated on: 12-Oct-2020

342 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements