datetime - PHP Function To Detect Expired Webhooks

I've been using the function below to test for expired webhooks (older than 10 minutes) and it's been working until today.

    public function isWebhookExpired($eventTime){

        $status = TRUE;
        $serverTimezone = new DateTimeZone('America/Chicago');
        $webhookTimezone = new DateTimeZone('GMT');
 
        $adjustedEventTime = new DateTime($eventTime, $webhookTimezone);

        $timeOffset = $serverTimezone->getOffset($adjustedEventTime) - 600;

        $currentTime = strtotime("$timeOffset seconds");

        if ($currentTime > strtotime($eventTime)) {
            error_log('An expired webhook detected.');
        }else{
            $status = FALSE;
        }

        return $status;
    }

Starting today I've been getting the error

PHP Fatal error:  Uncaught Exception: DateTime::__construct(): Failed to parse time string (2021-02-01T20:42:09.567506804Z) at position 0 (2): The timezone could not be found in the database

As far as I can tell the timestamp format has not changed -- 2021-02-01T20:42:09.567506804Z

I did recently update from php 7.2 to 7.4 but the function worked after the update until today.

I'd appreciate any input on how to resolve the error, reformat the timestamp, or a new function that accomplishes the same task.

Answer

Solution:

I would think that this line is the problem

  $webhookTimezone = new DateTimeZone('GMT')

The error tells you that the timezone does not exist in the constructor and you use that var in your class instantiation.

Php manual says that GMT is deprecated so I would just try to change GMT to

Europe/London Or another valid timezone from here:

https://www.php.net/manual/en/timezones.europe.php

Try to replace the old line 5 by:

$webhookTimezone = new DateTimeZone('Europe/London');

Source