note 102686 added to datetime.getlasterrors
[email protected] Mon, 28 Feb 2011 12:08:28 -0800
| Newsgroups | php.notes |
|---|---|
| Message-ID | <[email protected]> |
DateTime::createFromFormat is smart to handle the cases where you input an invalid date, like April 31st, and convert it to May 1st. In some cases, you do not want this automatic smart handling of the dates for example in a user input form where you want to be sure that your user did input the date he wanted. To do that, you need to get access to the warnings, this method is the only way to do it:
<?php
$date = DateTime::createFromFormat('Y-m-d', '1999-04-31');
print $date->format('Y-m-d') . PHP_EOL;
print_r(DateTime::getLastErrors());
?>
The output is:
1999-05-01
Array
(
[warning_count] => 1
[warnings] => Array
(
[10] => The parsed date was invalid
)
[error_count] => 0
[errors] => Array
(
)
)
So, here you can see, you have a warning because the date was invalid, but not an error because PHP was smart enough to convert it into a valid date. It is then up to you to do something with this information.
----
Server IP: 85.214.154.209
Probable Submitter: 80.139.32.14
----
Manual Page -- http://www.php.net/manual/en/datetime.getlasterrors.php
Edit -- https://master.php.net/note/edit/102686
Del: integrated -- https://master.php.net/note/delete/102686/integrated
Del: useless -- https://master.php.net/note/delete/102686/useless
Del: bad code -- https://master.php.net/note/delete/102686/bad+code
Del: spam -- https://master.php.net/note/delete/102686/spam
Del: non-english -- https://master.php.net/note/delete/102686/non-english
Del: in docs -- https://master.php.net/note/delete/102686/in+docs
Del: other reasons-- https://master.php.net/note/delete/102686
Reject -- https://master.php.net/note/reject/102686
Search -- https://master.php.net/manage/user-notes.php