deviantintegral / null-date-time
Interfaces and classes supporting empty or null DateTime objects
Installs: 8 552
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^7.3||^8.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.15
- phpunit/phpunit: ^9.5
This package is auto-updated.
Last update: 2024-11-29 05:51:13 UTC
README
composer require deviantintegral/null-date-time
This package provides classes that decorate \DateTime so that it is always formattable as a string, even if the time is empty or null.
PHP's \DateTime object does not have any way to represent an "empty" date. While there is a \DateTimeInterface class, it's documentation explicitly says it is not for implementation but type hinting only.
We don't want calling code to have to check for null returns on every get call. DateTimeFormatInterface requires that format() return an empty string if the underlying date is not set.
When constructing a DateTime object, a common pattern would be:
<?php function createTime(string $time = null): \Deviantintegral\NullDateTime\DateTimeFormatInterface { if (null === $time || '' === $time) { return new \Deviantintegral\NullDateTime\NullDateTime(); } return \Deviantintegral\NullDateTime\ConcreteDateTime::fromString($time); } $dateTime = createTime('now'); $dateTime->format('U'); // Will return the current Unix timestamp. if ($dateTime instanceof \Deviantintegral\NullDateTime\ConcreteDateTimeInterface) { $dateTime->getDateTime()->getOffset(); // Call any of the usual \DateTime methods. } $dateTime = createTime(''); $dateTime->format('U'); // Will return an empty string.