On 20 May 2011 16:22, Geoff Lane <ge...@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>

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

Reply via email to