-1

Since i read another few topic with similar problem i dont understand what is wrong in my situation date is sorted with - in both case so date 03-14-2020 should become 2020-14-03 but become 1970-01-01

$dates = explode(" / ", $this->input->post('date'));
print_r($dates);

Array ( [0] => 03-14-2020 [1] => 03-20-2020 )


    $newDate = array(
       'start'      => date("Y-m-d", strtotime($dates[0])),
       'end'        => date("Y-m-d", strtotime($dates[1]))
    );
print_r($newDate)

Array ( [start] => 1970-01-01 [end] => 1970-01-01 ) 

even this way is not showing correct

$date = DateTime::createFromFormat('d-m-Y', $dates[0]);
print_r($date->format('Y-m-d'));
2021-02-03
Ivan
  • 433
  • 5
  • 16

2 Answers2

1

I done it with using DateTime::createFromFormat

    $start = DateTime::createFromFormat('m-d-Y', $dates[0]);
    $end = DateTime::createFromFormat('m-d-Y', $dates[1]);
    $newDate = array(
        'start'     => $start->format('Y-m-d'),
        'end'       => $end->format('Y-m-d')
    );

    print_r($newDate) ;
    Array ( [start] => 2020-03-14 [end] => 2020-03-20 ) 
Ivan
  • 433
  • 5
  • 16
-2
$dates = array ('03-14-2020', '03-20-2020' );
  print_r($dates);
      $newDate = array(
         'start'      => date("Y-m-d", time($dates[0])),
         'end'        => date("Y-m-d", time($dates[1]))
      );
  print_r($newDate)

Try this

Samuel
  • 182
  • 9
  • result is wrong: Array ( [0] => 03-14-2020 [1] => 03-20-2020 ) Array ( [start] => 2020-03-11 [end] => 2020-03-11 ) – Ivan Mar 11 '20 at 10:52
  • Found: You need to use those format for the old dates - DD-MM-YYYY - YYYY-MM-DD – Samuel Mar 11 '20 at 11:08