Passing null to parameter #1 ($datetime) of type string is deprecated in CodeIgniter 4 (PHP 8)
Asked Answered
H

1

8

I'm trying to make date difference (in days) between today and a date from a field in database (array), this is my code :

$date1 = date_create ($v['sipp_ed']);//this is from field in database
$date2 = new DateTime();
$intval = date_diff ($date1, $date2);
echo $intval->format("Diiference is %a days"); 

but always get error "Passing null to parameter #1 ($datetime) of type string is deprecated". I have tried many times with other functions like strtotime, date, and getdate, but always fail.....could anybody help me, please ?

Helles answered 17/9, 2022 at 5:9 Comment(0)
M
12

On this line

$date1 = date_create ($v['sipp_ed']);

It's telling you that $v['sipp_ed'] is null.

Either check its value or use something like this which will pass an empty string when the value is null.

$date1 = date_create ($v['sipp_ed'] ?? '');
Marco answered 17/9, 2022 at 12:28 Comment(5)
No, it is not null....this is the value when I print the value (dd($date1);) where $date1 = date_create ($v['sipp_ed'] ?? ''); , like your suggestion.. : $date1 DateTime (3) 2018-07-31 00:00:00-05:00 CDT.Helles
Does it work with that changeMarco
Yes, it works, but when I try to take the difference : if ($intval>=90) { $hari="Kadaluarsa"; } it doesn't work > "Object of class DateInterval could not be converted to int "Helles
Since it works you should tick the answer. I have upvoted the question. It doesn't work because $intval is an object, not an integer. See PHP Documentation. You either need to convert $intval to integer or 90 to datetime. You need to start googling for php examples. Start with "php datetime object"Marco
What do you mean tick the answer...I am a new member, don't know the rules.....but thanks for the answer....I will googling for "php datetime object".Helles

© 2022 - 2024 — McMap. All rights reserved.