1

I have date in the following format

31st of August 2014 Sun using $cursor->format("jS \of F Y D"); but how can I convert the existing time to Ymd so that the output will be 20140831

I tried like echo date("Y-m-d", strtotime(31st of August 2014 Sun));

and I don't know why the output is showing like 1970 Jan 1 which is weird.

I also tried

echo contime('1st of August 2013 Sun');

function contime($gettime) {
$fetch_date = strtotime($gettime);
$date = new DateTime('');
return $date->format('Ymd');
}

But this is returning today's date.

Also tried Convert one date format into another in PHP

$old_date = date('1st of August 2013 Sun');  //date('l, F d y h:i:s'); // returns Saturday, January 30 10 02:06:34
$old_date_timestamp = strtotime($old_date);
$new_date = date('Y-m-d H:i:s', $old_date_timestamp);   
echo $new_date;

But its printing 1970-01-01 00:00:00

Community
  • 1
  • 1
Karthik Malla
  • 5,570
  • 12
  • 46
  • 89

2 Answers2

3
<?php

$date = DateTime::createFromFormat("jS \of F Y D", '31st of August 2014 Sun');
echo $date->format('Ymd');

But be aware that string must be correct, in your example you have:

'1st of August 2013 Sun'

where 1st of August 2013 is not sunday, it's shursday, if days names in your data set are not correct you should remove them.

LPodolski
  • 2,888
  • 4
  • 21
  • 24
1

You should split the time into day month year minute and second (you will need to turn the month into a number) and then you should be able to use mktime:

 echo date("Y-m-d", mktime($hour, $minute, $second, $month, $day, $year));