how to get time from MySQL database in PHP

by Monika Dadool 15-Mar-19

0 789

We Generally face the situation while creating any table  in MySQL. When we are creating a column with type =TIME,  we will get time in the following format

i.e. 2019-03-21 20:26:12

If we want to get only time from the above-mentioned format, there are many methods that can be implemented. Two of them are illustrated with an example:

We have used strtotime() for converting the time string into a timestamp and a date() function for getting time format as per our requirement.

strtotime() function


$str = '2019-03-21 20:26:12';
echo date('g:i A', strtotime($str));

Output: 8:26 PM

Date() function

$str = '2019-03-21 20:26:12';
echo date('g:i A ', $ts);

Output: 12:00 AM


Leave a Reply


    Waiting for your comments