การแปลงรูปแบบเวลาจาก PHP ไป MySQL
สามารถใช้คลาส DateTime ของ PHP เพื่อแปลงเวลาจากรูปแบบ "11:00 AM" เป็นรูปแบบมาตรฐานของ MySQL (H:i:s) ได้อย่างง่ายดาย
โค้ด PHP
$phpTime = '11:00 AM';
echo 'The PHP Time Format is = ' . $phpTime;
$timeFormat = DateTime::createFromFormat('h:i A', $phpTime);
$MySQLTimeFormat = $timeFormat->format('H:i:s');
echo 'The MySQL Time Format is = ' . $MySQLTimeFormat;
ผลลัพธ์
The PHP Time Format is = 11:00 AM The MySQL Time Format is = 11:00:00
คำอธิบาย
createFromFormat('h:i A', ...)- อ่านเวลาในรูปแบบ 12 ชั่วโมงพร้อม AM/PMformat('H:i:s')- แปลงเป็นรูปแบบ 24 ชั่วโมงมาตรฐานของ MySQL- รูปแบบ 'h' ใช้สำหรับ 12 ชั่วโมง, 'H' ใช้สำหรับ 24 ชั่วโมง