-5

I am extracting date from a string. Below is my code. I am getting only January 1, 1970 at the end. Please help me to fix this. your help would be appreciated.

    $current = 'DATE OF : 23/03/1951 BENCH:';

    $DATEOF = preg_replace('/(.*)DATE OF (.*?)BENCH:(.*)/s', '$2', $current);

    if (!is_null($DATEOF)) {
        $oldDate = $DATEOF;
        $oldDateReplace = str_replace(array('!\s+!', '/^\s+/', '/\s+$/',':'), array('','','',''), trim($oldDate));

        $date = ''.$oldDateReplace.'';
        $timestamp = strtotime($date);
    if ($timestamp === FALSE) {
         $timestamp = strtotime(str_replace('/', '-', $date));
     }
     echo $newDate = date("F j, Y",$newDateM);

    }else{$newDate = '';}

    // print this date only January 1, 1970 i want this date 23/03/1951

1 Answers1

0

Use below code

$current = 'DATE OF : 23/03/1951 BENCH:';

$DATEOF = preg_replace('/(.*)DATE OF (.*?)BENCH:(.*)/s', '$2', $current);

if (!is_null($DATEOF)) {
$oldDate = $DATEOF;
$oldDateReplace = str_replace(array('!\s+!', '/^\s+/', '/\s+$/',':'), array('','','',''), trim($oldDate));

$date = ''.$oldDateReplace.'';
$timestamp = strtotime($date);
if ($timestamp === FALSE) {
     $timestamp = strtotime(str_replace('/', '-', $date));
}
   echo $newDateM = date("m/d/Y",$timestamp);
   echo $newDate = date("F j, Y",$timestamp);

}else{$newDate = '';}
Vinie
  • 2,983
  • 1
  • 18
  • 29