Menu

← Back to new.*

Error Identifier: new.dateTime

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 DateTime('2020.11.17');

Why is it reported? #

The date string passed to the DateTime constructor is invalid and produces an error at runtime. PHP’s DateTime (and DateTimeImmutable) constructors throw an exception or produce warnings when they cannot parse the given date string.

In the example above, '2020.11.17' uses dots as separators which PHP interprets differently than intended, leading to a parsing error.

How to fix it #

Use a valid date format:

-new DateTime('2020.11.17');
+new DateTime('2020-11-17');

Common valid date formats include:

  • '2020-11-17' (ISO 8601)
  • '2020-11-17 14:30:00' (date and time)
  • 'now' (current date/time)
  • 'yesterday', 'tomorrow' (relative dates)

For custom formats, use DateTime::createFromFormat():

-new DateTime('2020.11.17');
+DateTime::createFromFormat('Y.m.d', '2020.11.17');

How to ignore this error #

You can use the identifier new.dateTime to ignore this error using a comment:

// @phpstan-ignore new.dateTime
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.dateTime

Rules that report this error #

  • PHPStan\Rules\DateTimeInstantiationRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.