php convert text date

All your question about php
Post Reply
mister_v
Posts: 188
Joined: Thu Mar 04, 2010 9:19 pm

php convert text date

Post 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.
chris
Site Admin
Posts: 194
Joined: Mon Jul 21, 2008 9:52 am

Re: php convert text date

Post 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));
}
Post Reply