0

I have an excel sheet with dates in Y/m/d H:i:s format. But in case if a user accidentally typed 2019/29/10 16:20:10 (ie, Y/d/m H:i:s) format it should convert to Y-m-d H:i:s format as 2019-10-29 16:20:10.

I have used the following strotime but it converted the Y/d/m format to 1970-01-01

$date = date('Y-m-d h:i:s', strtotime($rows[$i][2]));

If by mistake month and date got swapped, can we save and display the same in Y-m-d format??

Oops
  • 1,373
  • 3
  • 16
  • 46

1 Answers1

0

If you don't have any date then default date will be 1970-01-01 so please use condition before if

if($rows[$i][2]){
 date('Y-m-d h:i:s', strtotime($rows[$i][2]));
}

Another way is using Carbon in Laravel Blade Template

{{ \Carbon\Carbon::parse($rows[$i][2])->format('Y-m-d h:i:s')}}
Khem Raj Regmi
  • 2,130
  • 19
  • 21