strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated

Created on 20 October 2022, about 2 years ago
Updated 28 November 2023, 12 months ago

Problem/Motivation

If a View has a date field added to it that is NULL, the following error triggers :
Deprecated function: strtotime(): Passing null to parameter #1 ($datetime) of type string is deprecated in Drupal\view_custom_table\Plugin\views\field\MysqlDate->render() (line 154 of /var/www/html/web/modules/contrib/view_custom_table/src/Plugin/views/field/MysqlDate.php)

This is due to the fact that if no date is set, $date_value ends up as NULL (which then gets passed on to strtime) :

  public function render(ResultRow $values) {
    $date_value = $this->getValue($values);
    $value = strtotime($date_value);
    ..

This is only from PHP 8.1 onwards, as 8.1 has deprecated passing null to a lot of functions (see here).

Steps to reproduce

- Create a database table with a date field, that is allowed to be set to NULL. Make sure the table has at least a single entry with a date of NULL
- Add the new database table as a Views Custom Table
- Create a View based on this new Views Custom Table, and add the date field to it
- Preview the View

Proposed resolution

Check that $date_value is not NULL before continuing the render function.

Remaining tasks

User interface changes

API changes

Data model changes

📌 Task
Status

Fixed

Version

2.0

Component

Code

Created by

🇧🇪Belgium vodde83

Live updates comments and jobs are added and updated live.
  • PHP 8.0

    The issue particularly affects sites running on PHP version 8.0.0 or later.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

Production build 0.71.5 2024