1

How to convert "Friday 20th of March 2015" into "2015-03-20" in Php ?

Options Tried (Failed):

$time = strtotime(stripslashes("Friday 20th of March 2015"));
echo date("Y-m-d",$time);


$time = strtotime("Friday 20th of March 2015");
echo date("Y-m-d",$time);

Option 2 (Failed):

$old_date = date('Friday 20th of March 2015');           
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d', $old_date_timestamp);   
echo $new_date;

Option 3 (Failed):

$date = DateTime::createFromFormat('l d of F Y', 'Friday 20th of March 2015');
$new_date_format = $date->format('Y-m-d');
echo $new_date_format;

Duplicate Entries That Didn't Work Out: How to convert "Day, dd Month yyyy" to "yyyy-mm-dd" in php

Convert one date format into another in PHP

Community
  • 1
  • 1
Arijit Aich
  • 151
  • 1
  • 1
  • 12

1 Answers1

1

This Code Works:

$date = DateTime::createFromFormat('l dS \o\f F Y', 'Friday 20th of March 2015');
        $new_date_format = $date->format('Y-m-d');
        echo $new_date_format;
Arijit Aich
  • 151
  • 1
  • 1
  • 12
  • You should use `'l dS \o\f F Y'` as the format specifier as it will cope not only with `20th`, but also with eg `2nd` and `1st` – fvu Jun 10 '15 at 16:41