Within a for loop, I’m using the following code to convert from one date format to another:
339: $newdate = date_create_from_format('j-M g:i A', $moneyline[$i][date]);
340: echo date_format($newdate, 'Y-m-d');
According to the var_dump of $moneyline[$i][date], my variable appears to be a string:
string(15) "18-Jun 7:05 PM"
But when I use the variable as an argument for the $newdate constructor, $newdate creates a boolean according to vardump:
bool(false)
That means when I try to execute line 340, I get the following error:
Warning: date_format() expects parameter 1 to be DateTime, boolean given in /home/andrewmin/andrewmin.com/share/scraping/betting_scraping.php on line 340
However, if I change line 339 to:
339: $newdate = date_create_from_format('j-M g:i A', '18-Jun 7:05 PM');
it prints perfectly. Anyone have any ideas on why?
edit: sorry, it’s not a foreach, it’s a for:
for ($i = 2; $i < $array_length; $i++) {
Surely when I test using this code:
I get this result:
The notice can be gotten rid of by properly quoting the
'date'array index.My guess: your
$moneylinearray contains a string not conforming to the date format you provided (e.g. an empty string). In this case,date_create_from_formatwill fail and will returnfalse.If in doubt, please post a more complete copy of the code involved and an array the code fails on.