DateTimeInterface can’t be implemented by user classes

Description

DateTimeInterface is an interface. It should be available for usage with the implements keyword, but the PHP engines forbids it. Instead, it is recommended to extends the DateTime or DateTimeImmutable classes.

On the other hand, DateTimeInterface is available to type arguments, properties, etc. or to use with instanceof and catch.

Example

<?php

class X implements DateTimeInterface {}

?>

Solutions

  • Extends DateTime class.

  • Extends DateTimeImmutable class.