On 20 May 2011 16:47, Geoff Lane <ge...@gjctech.co.uk> wrote:

*snip*

>>> Also, AFAICT createFromFormat fails if the date is not formatted
>>> according to the first parameter. So, for example:
>>>  $date = DateTime::createFromFormat('d M Y', '5/2/10')
>>> fails ... (at least, it does on my system :( )
>>>
>
>> I'm sorry for asking but what did you expect?? You're specifically
>> calling a method that parses a string according to a given format. If
>> it parsed the string according to any other format, that would be a
>> huge WTF.
>
> Don't feel sorry to have asked, because it's exactly what I expected.
> João suggested using createFromFormat. Since I need to validate dates
> input in any valid form, I felt it wouldn't work and my comment was to
> João to that effect. That said, I've seen some weird and unexpected
> results from my development server recently (e.g. my post of 16 May re
> weird cookie behaviour) which was why I added the proviso "(at least,
> it does on my system)" just in case that method wasn't meant to behave
> as I inferred.
>
> With all that said, I still have no 'out of the box' method to
> validate a user-input date string and I haven't been able to find the
> code I used with PHP 3.something before my sojourn into the depths of
> ASP to know how I used to do this!
>

My bad, I jumped into the middle of a thread - sorry.

Try:

$date = new DateTime($date_string_to_validate);
echo $date->format('Y-m-d');

Regards
Peter

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to