0

How to convert from dmy to d-m-y, using this code, but this method is not what I want.

      $source = '05032020';
      $date = new DateTime($source);
      echo $date->format('d.m.Y'); // 05-03-2020
ariana
  • 33
  • 7
  • What format do you want? `05-03-2020` looks like it matches your `d-m-y` requirements. – halfer Apr 23 '20 at 14:15
  • i want, dd-mm-yy – ariana Apr 23 '20 at 14:17
  • Your source is not in a common format, so you're going to have to pass it through [DateTime::createFromFormat()](https://www.php.net/manual/en/datetime.createfromformat.php) – aynber Apr 23 '20 at 14:17

1 Answers1

1

Try this.

$source = '05032020';
$date = date_create_from_format('dmY', $source);
echo $date->format('d-m-Y'); //05-03-2020
Star_Man
  • 1,091
  • 1
  • 13
  • 30
  • this option is correct, I have one more question whether it is possible to make the time hi (1934) in h: i (19:34) using this method? – ariana Apr 23 '20 at 14:35
  • Try this. $source = '1934'; $date = date_create_from_format('Hi', $source); echo $date->format('H:i'); – Star_Man Apr 23 '20 at 14:38