views:

54

answers:

3

Examples of the translations I need to do:

$stringDate = "November 2009";  $output = "11/09";
$stringDate = "October 1 2010"; $output = "10/01/2010";

$stringDate = "January 2010"; $output = "01/10";
$stringDate = "January 9 2010"; $output = "01/09/2010";

Note that I do not know which format the $stringDate will be in and the lack of commas in the month day year set.

Thanks for any help anyone might offer.

+9  A: 

PHP.net strtotime

First convert it to UNIX timestamp using strtotime, then to your own format using date() function.

Kemo
This is the method I use, and it works well. The second parameter of the date() function is optional, and can be any timestamp (default is, of course, the current time).
Josh
+2  A: 

Use strtotime() - it can capture the date and/or time in most formats and convert it to a timestamp which you then can format as you wish with date().

EDIT:
You can for example do it like this:

$stringDate = "October 1 2010";
$mysql_datetime = date("Y-m-d H:i:s",strtotime($stringDate));
alexteg
+1  A: 

Your problem is that "November 2009" is not a date but "October 1 2010" is.

If your input type is unknown (date, date range, etc.), it´s going to be extremely difficult to get a reliable conversion.

For a date you can of course use date("format", strtotime($input)) but that only solves part of the problem and will lead to problems if $input is not a date.

jeroen
strtotime() returns FALSE if inputted date isn't recognized, not much of a problem though .)
Kemo
@Kemo, you're kind of missing the point, the input is not a date by definition, it can be a date-range and that smells like a potential *garbage in => garbage out* problem to me.
jeroen