I am having a table which has multiple columns and one of them is of DateTime
type. There is also a search box where the user can insert anything he wants and the table is going to be filtered by the value he has inserted. Since I don't know which column he wants to filter by, I am adding the value he has inserted as a filter to all columns when getting data from the database. So, for example:
+------------+--------+
| Date | Amount |
+------------+--------+
| 01/01/2019 | 1 |
+------------+--------+
if he searches by date, I am handling it well in the code and everything works fine:
$date = date_create_from_format('m/d/Y', $data_model['filter'])->format('Y-m-d');
if($date) {
$query->orWhere('date', 'like', $date);
}
But if he wants to search by amount and types in '1' in the search box, once that value comes to date_create_from_format
function, I am getting an error: Call to a member function format() on boolean
.
In the PHP manual, it is said that return value for this function is: Returns a new DateTime instance or FALSE on failure.
when actually it isn't. On failure, it returns an exception instead of FALSE
.
How can I check before date_create_from_format
is called if the value that is going to be passed as a parameter is not going to cause it to fail?