karthika pommudu

karthika pommudu

  • NA
  • 321
  • 34.6k

y-d-m date format convert in php

Apr 14 2020 11:21 PM
Here the code,
 
$date = '03/17/2020 - 04/15/2020';
 
$pieces = explode("-", $date);
$date11= $pieces[0]; // piece1
$date12= $pieces[1]; // piece2
 
$date1 = str_replace('/', '-', $date11); 
$DATE1 = date('Y-d-m', strtotime($date1));
 
$date2 = str_replace('/', '-', $date12);
$DATE2= date("Y-m-d", strtotime(preg_replace('/[^A-Za-z0-9\-]/', '',$date2)));
echo $DATE1; //2020-03-17
echo $DATE2;//1970-01-01
 
Output:    $DATE1 : 2020-03-17
                $DATE2: 1970-01-01
 
 Please advice.
 

Answers (7)