converting date to new format not working using strtotime()

60 views Asked by At

I have date format in my database like this '13/09/2022' but my client suggest to change this format into this format '09/13/2022' so I use strtotime() but instead of having correct result it give this kind of result 01/01/1970 and you can see it give me wrong result. can someone explain what happen?

This is my code:

<?php 
$oldDate = '13/09/2022';
$newDate = date("m/d/Y", strtotime($oldDate)); 
echo $newDate; 
?> 
1

There are 1 answers

0
veapon leung On

Your can try this:

echo DateTimeImmutable::createFromFormat('d/m/Y', '13/09/2022')->format("m/d/Y");

According to the documentation, 'DD/MM/YY' is not a standard format: https://www.php.net/manual/en/datetime.formats.php.