Page 1 of 1

php convert text date

Posted: Wed Sep 26, 2012 1:52 pm
by mister_v
Hi,

i would like to convert a text date like "2012 July 11 16:00"
To a SQL conform date. (JJJJ-MM-DD)

I tried it with

Code: Select all

date('Y-m-d', strtotime('2012 July 11 16:00');
But always gives me 1970-01-01.

Re: php convert text date

Posted: Wed Sep 26, 2012 7:34 pm
by chris
strtotime() does its job well, but it need to be in the correct order.
day month year

Use a function to switch the dates:

Code: Select all

function switch_date($str)
{
  //split string
  $arr=explode(' ',$str);
  //put in correct order
  $new_str=$arr[2].$arr[1].$arr[0].$arr[3];
  return  date('Y-m-d', strtotime($new_str));
}