Understanding PHP 8 Deprecations in CodeIgniter 4 DateTime Handling
You are encountering a specific deprecation warning introduced in PHP 8: Passing null to parameter #1 ($datetime) of type string is deprecated. This error appears when you attempt to pass a value that evaluates to null into a function expecting a string, such as the first argument of DateTime::__construct() or functions that internally call it. In your specific case, $date1 is becoming null, which causes date_diff() to fail before it can perform its calculation.
The root cause lies in how CodeIgniter 4 (and standard PHP) handles database dates when they are not explicitly cast to a string. The line $v['sipp_ed'] likely contains a raw value from the database, or perhaps an empty string, but more critically, it might be null. If your database column is defined as nullable and you haven't populated it for this specific record, $v['sipp_ed'] holds a null value. When date_create() receives null, it returns false in older PHP versions or throws the deprecation warning in PHP 8.0+ depending on the exact build and configuration, but ultimately, you end up trying to pass that falsy/null result into date_diff().
Here is the breakdown of why your current approach fails:
$date1 = date_create($v['sipp_ed']); // $v['sipp_ed'] might be null or empty string
$date2 = new DateTime(); // This works fine
$intval = date_diff($date1, $date2); // Fails if $date1 is not a valid DateTime object
The issue isn't just that you are passing null to date_create(). Even if $v['sipp_ed'] contains a string like "2023-10-01", date_create() expects a valid date format. If the database returns an empty string or NULL, date_create() returns false. Passing false to date_diff() is where the strict typing of PHP 8 kicks in, warning you about passing invalid types.
To resolve this in CodeIgniter 4, you must ensure that $v['sipp_ed'] is converted into a valid ISO-8601 string before creating the DateTime object. The most robust way to do this is to use DateTime::createFromFormat(). This method explicitly tells PHP what format to expect from your database string.
Converting Database Dates Safely
Assuming your database stores dates in the standard 'Y-m-d' or 'Y-m-d H:i:s' format, you should validate the input first. If the value is null or an empty string, you must handle that case before attempting to create the object.
Here is a corrected implementation pattern:
// Get the raw value from your query result
$rawDate = $v['sipp_ed'];
// Handle null or empty cases explicitly
if ($rawDate === null || trim($rawDate) === '') {
// Decide on your behavior here.
// Option A: Skip the calculation
echo 'No date difference to calculate.';
return;
// Option B: Default to today if missing (use with caution)
$rawDate = new DateTime();
}
// Use createFromFormat for robust parsing
$date1 = DateTime::createFromFormat('Y-m-d', $rawDate);
// Check if parsing was successful. createFromFormat returns false on failure.
if ($date1 === false) {
throw new InvalidArgumentException("Invalid date format found in database: {$rawDate}");
}
$date2 = new DateTime(); // Represents 'now'
$interval = $date1->diff($date2); // Note: diff() is a method, not a standalone function
echo "Difference is {$interval->days} days";
Why strtotime() Might Still Be Risky
You mentioned trying strtotime(). While often convenient, it relies on the system's locale and can sometimes be ambiguous with certain date formats. More importantly, if your database contains malformed dates or unexpected characters (common in legacy systems), strtotime() might return false as well, leading to the same deprecation warning if you don't check for that boolean result before using it.
The best practice is to stick to strict format strings like 'Y-m-d'. This ensures that your application code is decoupled from system locale settings and behaves consistently across different environments.
Best Practices for CodeIgniter 4
In CodeIgniter 4, you should always validate incoming data before processing it with date functions. If you are using Query Builder or Model associations, ensure you handle null returns gracefully. You can also leverage the framework's validation library if this data is coming from a request, but for database retrieval, explicit checks inside your controller logic are usually sufficient.
If you are working on a larger project where date consistency is critical across multiple modules, consider adopting a dedicated date handling package like carbonphp/carbon-doctrine-types. Carbon provides a much more expressive API and handles edge cases (like invalid dates) with far more clarity than the native PHP DateTime class. However, if you prefer sticking to the core framework without external dependencies, the createFromFormat approach combined with strict null checks is the standard solution for PHP 8 compatibility.