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