Convert PHP Variable to MySQL Time Format



Use DateTime to convert PHP variable “11:00 AM: to MySQL time format.

The PHP code is as follows −

$phpTime = '11:00 AM';
echo('The PHP Time Format is =');
echo ($phpTime);
$timeFormat = DateTime::createFromFormat( 'H:i A', $phpTime);
$MySQLTimeFormat = $timeFormat->format( 'H:i:s');
echo ('
');
echo('The MySQL Time Format is =');
echo ($MySQLTimeFormat);

The snapshot of PHP code is as follows −

Here is the output −

The PHP Time Format is =11:00 AM
The MySQL Time Format is =11:00:00
Updated on: 2019-07-30T22:30:25+05:30

290 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements