- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
<!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
Advertisements