InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp()

Created on 16 August 2023, about 1 year ago
Updated 15 September 2023, about 1 year ago

Problem/Motivation

The CalendarViewBase::ensureTimestampValue() method cannot handle dates in the distant past.

Original bug report from the farmOS Calendar module (which uses Calendar View):

White screen of death when visiting /calendar, with the following error in logs:

InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() (line 201 of /opt/drupal/web/core/lib/Drupal/Component/Datetime/DateTimePlus.php).

Steps to reproduce

  1. Enable the farmOS Calendar module.
  2. Create a log with a date of Jan 1, 1902 12:00:00.
  3. Visit /calendar

Proposed resolution

TBD

Remaining tasks

TBD

🐛 Bug report
Status

Fixed

Version

2.1

Component

Code

Created by

🇺🇸United States m.stenta

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @m.stenta
  • 🇺🇸United States m.stenta

    This seems to happen when the log timestamp is in the distant past. I was able to replicate it with a date of Jan 1, 1902 12:00:00.

  • 🇺🇸United States m.stenta

    I've traced this to the CalendarViewBase::ensureTimestampValue() method in the Calendar View module:

      /**
       * Helper method to make sure a timestamp is a timestamp.
       *
       * @param mixed $value
       *   A given value.
       *
       * @return int
       *   The timestamp or the original value.
       */
      public function ensureTimestampValue($value) {
        return !empty($value) && !ctype_digit(strval($value)) ? strtotime($value) : (int) $value;
      }
    

    Passing a $value of -2145898800 (Jan 01 1902 00:00:00) into that method returns FALSE because strtotime(-2145898800) returns FALSE.

    Moving this to the Calendar View module issue queue...

  • 🇺🇸United States m.stenta

    It looks like the ctype_digit() function does not work with negative numbers.

    Here are some tests with drush php:cli to show how it works:

    > ctype_digit(strval(1))
    = true
    
    > ctype_digit(strval(-1))
    = false
    

    This means that any date before the Unix epoch (1 January 1st, 1970 00:00:00 UTC) will not work with this module.

  • 🇺🇸United States m.stenta

    I wonder if we can use is_numeric() instead of ctype_digit? I tested that out locally and it fixes the issue for me.

    Change from:

    return !empty($value) && !ctype_digit(strval($value)) ? strtotime($value) : (int) $value;
    

    To:

    return !empty($value) && !is_numeric($value) ? strtotime($value) : (int) $value;
    

    I will open a MR for consideration.

  • @mstenta opened merge request.
  • Status changed to Needs review about 1 year ago
  • 🇮🇳India NivethaSubramaniyan

    I will be reviewing..

  • 🇮🇳India NivethaSubramaniyan

    The given patch is working fine. We can go with is_numeric. I have detailed the steps and attached the screenshot for the reference.

    Steps to reproduce:

    1) Created a content with authored on date 15 May 1923.
    2) Created a calendar view with content
    3) Got the issue - InvalidArgumentException: The timestamp must be numeric. in Drupal\Component\Datetime\DateTimePlus::createFromTimestamp() [ Attached the image ]
    4) Applied the patch.
    5) Now I am able to view the content which I have created on date 15 May 1923. [ Attached an image ]

    I have done this steps with drupal 9.5 and calendar_view-2.1.1.

  • First commit to issue fork.
  • Status changed to Fixed about 1 year ago
  • Thanks a lot for fixing this issue.

    Marking as fixed as it is committed on 2.1.x

    Will be shipped in a new release soon.

  • Automatically closed - issue fixed for 2 weeks with no activity.

Production build 0.71.5 2024