สามารถเปลี่ยนรูปแบบวันที่ใน PHP ได้โดยใช้ date() ร่วมกับ strtotime() สมมติว่ามีวันที่ดังนี้
$dateValue = "2020-09-19";
เปลี่ยนรูปแบบวันที่ด้วย strtotime()
$modifiedDate = date("d-m-Y", strtotime($dateValue));ตัวอย่างโค้ด
<!DOCTYPE html> <html> <body> <?php $dateValue = "2020-09-19"; echo "The original date format is=", $dateValue, "<br>"; $modifiedDate = date("d-m-Y", strtotime($dateValue)); echo "The changed date format is= " . $modifiedDate; ?> </body> </html>ผลลัพธ์
จะได้ผลลัพธ์ดังนี้
The original date format is=2020-09-19 The changed date format is= 19-09-2020