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
  • 161
  • 1
  • 2
  • 12
  • Duplicate Options Didn't Help As Well. (Code & Sample Provided.) – Arijit Aich Jun 10 '15 at 13:15
  • Your given format is wrong `Friday 20th of March 2015` will be `Friday 20 March 2015`, and then it will be converted correctly. so it's not problem of `date()` or `strtotime()` .it's the problem of the data that you given. – Alive to die - Anant Jun 10 '15 at 13:17
  • Are you sure it didn't helped? Have you read all answers? – Rizier123 Jun 10 '15 at 13:19
  • The data which needs to be converted is `Friday 20th of March 2015`. This format cannot be changed since this is a third party format. – Arijit Aich Jun 10 '15 at 13:19
  • @Rizier123 - I am updating the question with all the answers that didn't work from the duplicate page link provided. – Arijit Aich Jun 10 '15 at 13:20
  • 1
    @ArijitAich How about specifying the correct date format: http://3v4l.org/uo6vN ? – Rizier123 Jun 10 '15 at 13:24
  • @Rizier123 - It Worked. I have updated the question with the correct answer. Would be great if you could remove the duplicate tag, since its not a duplicate entry to the link provided. Thank You. – Arijit Aich Jun 10 '15 at 13:28
  • http://stackoverflow.com/a/11435513/3933332 <- Seems like exactly what you use now. <- And this answer is in the dupe – Rizier123 Jun 10 '15 at 13:31
  • Yes. As you can see, i have already mentioned in the question, the links and answers that didn't work out since the beginning. – Arijit Aich Jun 10 '15 at 13:33
  • @fvu We can't have every possible date format as an answer.. – Rizier123 Jun 10 '15 at 13:34
  • @fvu If you feel like it, then go for it OR OP can answer his own question now. – Rizier123 Jun 10 '15 at 13:40

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
  • 161
  • 1
  • 2
  • 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