1

I can change the date and time but I issue is I save my date and time in database in this format Thursday 09th of July 2020 12:24:23 PM so I want to convert this in d-m-Y OR d-m-Y H:i:s Please help me how to I make this I search this related thread on stackoverflow but I couldn't achieve that ..

current I have tried this below code..

<?php
    $orgDate = "Thursday 09th of July 2020 12:24:23 PM";
    $newDate = date("d-m-Y", strtotime($orgDate));
    echo "New date format is: ". $newDate. " (MM-DD-YYYY)";
?>

RESPONSE: New date format is: 01-01-1970 (MM-DD-YYYY)

Shubham
  • 65
  • 8

1 Answers1

1

Personally I would make sure the date is being saved in simplier format to database (DATETIME), then it's more accessible and even easy to use in database query when comparing dates etc.

But if you have no choice, this should do:

$orgDate = "Thursday 09th of July 2020 12:24:23 PM";
$dateTime = DateTime::createFromFormat('l dS * F Y h:i:s A', $orgDate);
echo $dateTime->format('Y-m-d');

DateTime parameters are explained in documentation: https://www.php.net/manual/en/datetime.createfromformat.php

kaarto
  • 747
  • 1
  • 8
  • 18