Working with Time in PHP/ MySQL?


To work with time in PHP/ MySQL, you can use strtotime() function. The PHP code is as follows for the same −

$timeValue='8:55 PM';
$changeTimeFormat = date('H:i:s', strtotime($timeValue));
echo("The change Format in 24 Hours=");
echo($changeTimeFormat);

The snapshot of PHP code is as follows −

Here is the output.

Here is the MySQL query to get the original time −

mysql> SELECT CONCAT('The change Format in 12 Hours in
MySQL=',DATE_FORMAT('2019-03-12 20:55:00', '%l:%i %p')) AS OriginalTimeFormat;

The following is The output −

+------------------------------------------------+
| OriginalTimeFormat                             |
+------------------------------------------------+
| The change Format in 12 Hours in MySQL=8:55 PM |
+------------------------------------------------+
1 row in set (0.00 sec)

Updated on: 30-Jul-2019

194 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements