respect-validation/docs/validators/DateTime.md
Henrique Moody 7db3bea8a6
Enhance LintSpdxCommand with contributor tracking and header normalization
Improves SPDX header linting to ensure consistent license metadata across
the codebase.

Key changes:

- Enforce deterministic tag ordering (License-Identifier, FileCopyrightText,
  FileContributor) to ensure consistency, prevent merge conflicts, and
  simplify code reviews

- Add contributor alias mapping to consolidate contributors with multiple
  emails or name variations (e.g., "nickl-" → "Nick Lombard")

- Add --contributions-strategy option with "blame" (current code authors)
  and "log" (all historical contributors) to support different attribution
  philosophies

- Add optional path argument to lint specific files or directories

- Add --fix option to automatically correct header issues

Assisted-by: Claude Code (claude-opus-4-5-20251101)
2026-02-03 15:23:20 +01:00

3.7 KiB

DateTime

  • DateTime()
  • DateTime(string $format)

Validates whether an input is a date/time or not.

The $format argument should be in accordance to DateTime::format(). See more in the Formats section.

When a $format is not given its default value is Y-m-d H:i:s.

v::dateTime()->assert('2009-01-01');
// Validation passes successfully

Also accepts strtotime() values:

v::dateTime()->assert('now');
// Validation passes successfully

And DateTimeInterface instances:

v::dateTime()->assert(new DateTime());
// Validation passes successfully

v::dateTime()->assert(new DateTimeImmutable());
// Validation passes successfully

You can pass a format when validating strings:

v::dateTime('Y-m-d')->assert('01-01-2009');
// → "01-01-2009" must be a valid date/time in the format "2005-12-30"

Format has no effect when validating DateTime instances.

Message template for this validator includes {{sample}}.

Formats

Note that this validator validates whether the input matches a given DateTime::format() format and NOT if the input can be parsed with a given DateTimeImmutable::createFromFormat() format. That makes the validation stricter but offers some limitations.

The way DateTimeImmutable::createFromFormat() parses an input allows for many different conversions. Overall DateTimeImmutable::createFromFormat() tend to be more lenient than DateTime::format(). This might be what you desire, and you may want to use Satisfies to create a custom validation.

$input = '2014-04-12T23:20:50.052Z';

v::satisfies(fn($input) => is_string($input) && DateTime::createFromFormat(DateTime::RFC3339_EXTENDED, $input))
    ->assert($input);
// Validation passes successfully

v::dateTime(DateTime::RFC3339_EXTENDED)->assert($input);
// → "2014-04-12T23:20:50.052Z" must be a valid date/time in the format "2005-12-30T01:02:03.000+00:00"

Templates

DateTime::TEMPLATE_STANDARD

Mode Template
default {{subject}} must be a valid date/time
inverted {{subject}} must not be a valid date/time

DateTime::TEMPLATE_FORMAT

Mode Template
default {{subject}} must be a valid date/time in the format {{sample}}
inverted {{subject}} must not be a valid date/time in the format {{sample}}

Template placeholders

Placeholder Description
subject The validated input or the custom validator name (if specified).
sample

Categorization

  • Date and Time

Changelog

Version Description
2.3.0 Validation became a lot stricter
2.2.4 v::dateTime('z') is no longer supported.
2.0.0 Created

See Also