0

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?

niksrb
  • 459
  • 7
  • 25
  • It seems $data_model['filter'] is boolean, but it needs string. Be sure it is a string with var_dump($data_model['filter']) – tanaydin Feb 05 '19 at 10:44
  • If it throws an exception, just use `try/catch`? – M. Eriksson Feb 05 '19 at 10:44
  • Possible duplicate of [Correctly determine if date string is a valid date in that format](https://stackoverflow.com/questions/19271381/correctly-determine-if-date-string-is-a-valid-date-in-that-format) – Maraboc Feb 05 '19 at 10:46
  • @MagnusEriksson it's not going to solve my problem. If that is throwing an exception, than how can I search once user type in `1` or `0` or anything else that is going to cause the function to fail? Only if I catch all those situations, but there must be a better way. – niksrb Feb 05 '19 at 10:52

1 Answers1

2

date_create_from_format actually returned false. So if we step through the code this will happen:

We are assuming $data_model['filter'] = 1

$date = date_create_from_format('m/d/Y', $data_model['filter'])->format('Y-m-d');
$date = date_create_from_format('m/d/Y', '1')->format('Y-m-d');   // $data_model['filter'] = 1
$date = FALSE->format('Y-m-d');                                   // date_create_from_format returns false
// An exception is thrown because you can't call a method on a boolean

So the correct solution is to store the result from date_create_from_format first and check if it's FALSE. If not, you can call ->format('Y-m-d')

One possible solution would be:

$date = date_create_from_format('m/d/Y', $data_model['filter']);
if ($date !== false) {
    $date = $date->format('Y-m-d');
    $query->orWhere('date', 'like', $date);
}
JensV
  • 3,997
  • 2
  • 19
  • 43