deviantintegral/null-date-time
最新稳定版本:v1.1.1
Composer 安装命令:
composer require deviantintegral/null-date-time
包简介
Interfaces and classes supporting empty or null DateTime objects
README 文档
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.
统计信息
- 总下载量: 16.91k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 1
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-08-22