Re: Re: Date validation
| From: | Jo�o C�ndido de Souza Neto | Date: | Fri, 20 May 2011 14:42:49 +0000 |
| Subject: | Re: Re: Date validation | ||
| References: | 1 2 3 4 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-312964@lists.php.net to get a copy of this message | ||
If you look carefully, you´ll notice that I´m using the DateTime object
(default from PHP 5.2.0 or higher) not the function date.
--
João Cândido de Souza Neto
"Peter Lind" <peter.e.lind@gmail.com> escreveu na mensagem
news:BANLkTinjONYvfNQjqTfQtDmU_R2-CFPaoA@mail.gmail.com...
On 20 May 2011 16:22, Geoff Lane <geoff@gjctech.co.uk> wrote:
> On Friday, May 20, 2011, João Cândido de Souza Neto wrote:
>
>> What about using this:
>
>> $date = DateTime::createFromFormat("Y-m-d", "2011-05-20");
>
> Hi João, and thanks for your help.
>
> FWIW, I thought about that but it didn't work for me. On further
> investigation, I'm now completely confused and suspect I've got a duff
> PHP installation. Thankfully, it's a virtual machine so it should be
> reasonable easy to 'vapourise' and start over (perhaps with CentOS
> rather than Ubuntu as the OS).
>
> Anyway, the following code produces the following result when the
> variable $str = '7 feb 2010':
>
> [code]
> echo "<p>Date is $str</p>\n";
> $date = DateTime::createFromFormat('d M Y', $str);
> echo "<pre>";
> print_r($date);
> echo "</pre>\n";
> echo date('d M Y') . "<br />" . date('d M Y', $date);
> [/code]
>
> [result]
> <p>Date is 7 feb 2010</p>
> <pre>DateTime Object
> (
> [date] => 2010-02-07 15:11:34
> [timezone_type] => 3
> [timezone] => Europe/London
> )
> </pre>
> 20 May 2011<br />
> [/result]
>
> This is pretty much as expected except that the second call to date()
> - i.e. date('d M Y', $date) - outputs nothing.
date() takes an int as second parameter - a timestamp. Not an object.
And from a quick test it doesn't look like DateTime has a __toString
method.
> 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.
Regards
Peter
--
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>