T
T
truesadboy2018-09-28 14:00:08
PHP
truesadboy, 2018-09-28 14:00:08

PHP(7.2) error: Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given?

Hi all! The task is to parse the site. One of the elements contains a date and process it. Original date format: jmy . I'm converting to Ymd and I get an error Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given.

foreach($matches as $value){
    $value = pq($value);
    $date = $value->find('h4')->text();
    $fdate = preg_replace("/[.]/", "-", $date);
    $create_date = date_create_from_format('j-m-y', $fdate);
    $formate_date = date_format($create_date, 'Y-m-d');
    $end_date = strtotime($formate_date);
    var_dump($date);
}

the error comes from the $date variable. var_dump showed the following
string(8) "09/06/18"
Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given in C:\OSPanel\domains\pq\new.php on line 76
string(0) "" string(8) " 09/10/18"
Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given in C:\OSPanel\domains\pq\new.php on line 76
string(0) "" string(8) "09/12/18"

Where does string(0) "" come from after the first "run" in the loop?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Decadal, 2018-09-28
@Decadal

date_create_from_format

Returns the newly created instance of the DateTime class, or FALSE on error.
firstly, this point must be taken into account;
secondly, it reads all h4 headings. Does it surprise you that there are empty h4s in someone else's layout? just consider them and ignore them.

M
Melkij, 2018-09-28
@melkij

Warning: date_format() expects parameter 1 to be DateTimeInterface, boolean given

What exactly is unclear in the error? The parameter should be something similar to DateTimeInterface, and you shove a bool.
From bool - because date_create_from_format has the right to return it. In particular, if the string format does not match the required one.
Well, it doesn’t make sense to pull the formatting into a string and re-strtotime when there is already a native getTimestamp method.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question