Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.
Code example #
<?php declare(strict_types = 1);
new DateInterval('1M');
Why is it reported? #
The duration string passed to the DateInterval constructor is invalid and produces an error at runtime. PHP’s DateInterval constructor expects an ISO 8601 duration string starting with the letter P.
In the example above, '1M' is missing the required P prefix. The correct form is 'P1M' for one month or 'PT1M' for one minute.
How to fix it #
Use a valid ISO 8601 duration format:
-new DateInterval('1M');
+new DateInterval('P1M');
Valid duration strings always start with P and follow this structure:
'P1Y'— 1 year'P1M'— 1 month'P1D'— 1 day'PT1H'— 1 hour'PT1M'— 1 minute'PT1S'— 1 second'P1Y2M3DT4H5M6S'— combined duration
The T separator is required before time components (hours, minutes, seconds).
How to ignore this error #
You can use the identifier new.dateInterval to ignore this error using a comment:
// @phpstan-ignore new.dateInterval
codeThatProducesTheError();
You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:
parameters:
ignoreErrors:
-
identifier: new.dateInterval
Rules that report this error #
- PHPStan\Rules\DateIntervalInstantiationRule [1]