Search code examples
phpdate

Wrong Date returning


<!DOCTYPE html>
<html>
<body>

<?php
$date=date_create_from_format("l d F Y, H:i:s A","Wednesday 15 January 2025, 14:11:05 
PM");
echo date_format($date,"Y-m-d");
?>

This is returning 2025-01-22 i'm not sure what am missing.


Solution

  • The A flag is for AM/PM, which is completely unnecessary for 24- hour formats, which upper- case H stands for.

    Either use 24- hour format, like this:

    $date = date_create_from_format(
      "l d F Y, H:i:s", // Upper- case H, but no A at the end.
      "Wednesday 15 January 2025, 14:11:05" // Skip the "PM"
    );
    

    Or 12- hour format, like this:

    $date = date_create_from_format(
      "l d F Y, h:i:s A", // Lower- case h, for 12 hour format.
      "Wednesday 15 January 2025, 2:11:05 PM"
    );