Re: Date validation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



 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.

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

-- 
Geoff


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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux