views:

30

answers:

2

strtotime("25/03/1957") returns false. what will satisfy all of these date formats? i can't imagine how long it would take to actually make my own, so i hope there's already one out there you know of.

thanks!

+2  A: 

Considering some dates are valid but can point to two different actual dates, no function will ever be able to "guess" the right format at all times...

To help with that, with PHP >= 5.3, a new function has been added : date_create_from_format -- but it doesn't exist with PHP < 5.3, unfortunately...
(See also DateTime::createFromFormat)


Still, in the example you took, the year 1957 is a possible source of problems : PHP generally works with UNIX Timestamps, when it comes to dates...

And, at least on 32-bits systems, those can only represent dates between 1970 and 2038 -- as they count the number of seconds since 1970-01-01.

To avoid this problem, it's often a good idea to use the DateTime class, with which (quoting) :

The date and time information is internally stored as an 64-bit number so all imaginable dates (including negative years) are supported. The range is from about 292 billion years in the past to the same in the future.

(It will not solve the parsing problems with PHP < 5.3 ; but it'll solve the date-range problem...)

Pascal MARTIN
+1  A: 

I've found that dateTime objects support a wider range of formats than the strtotime() function, and the timezone settings of your server also make a difference; but I ended up building a function that would replace '/' with '-' before using the string to date methods. I also test for valid, then try swapping the apparent dd and mm (25-03-2001 => 03-25-2001) if invalid before testing again.

Mark Baker