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':

  echo "<p>Date is $str</p>\n";
  $date = DateTime::createFromFormat('d M Y', $str);
  echo "<pre>";
  echo "</pre>\n";
  echo date('d M Y') . "<br />" . date('d M Y', $date);

  <p>Date is 7 feb 2010</p>
  <pre>DateTime Object
      [date] => 2010-02-07 15:11:34
      [timezone_type] => 3
      [timezone] => Europe/London
  20 May 2011<br />

This is pretty much as expected except that the second call to date()
- i.e. date('d M Y', $date) - outputs nothing.

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 :( )


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

Reply via email to