glucnac / datetimeinterval
DateTimeInterval是DateInterval的包装器,提供了计算延迟的有用方法。
v3.0.0
2024-05-17 20:40 UTC
Requires
- php: ^8.1
Requires (Dev)
- mockery/mockery: ^1.5
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.8
- phpstan/phpstan-mockery: ^1.1
- phpunit/phpunit: ^9.5
- symplify/easy-coding-standard: ^12.1
This package is auto-updated.
Last update: 2024-09-17 22:04:21 UTC
README
当您想要计算两个DateTime之间的延迟时,快速得到期望的结果可能很棘手,有时您需要进行一些计算,这可能导致结果不正确。
DateTimeInterval是DateInterval的包装器,提供了计算延迟的有用方法。多亏了这个库,您不再需要计算DateInterval返回的结果。此外,它所有的方法都已全面测试,并且名称明确、带有注释。
这样,您不必复制/粘贴在另一个项目上实现的功能,只需安装此库即可享受。
安装
首选的安装方法是使用Composer。运行以下命令安装包并将其添加到项目的composer.json
中。
composer require glucnac/datetimeinterval
用法
$firstDate = new DateTimeImmutable('2022-07-15'); $secondDate = new DateTimeImmutable('2022-08-15'); $dateTimeInterval = new DateTimeInterval($firstDate, $secondDate); // Absolute count of days echo $dateTimeInterval->getDays(); // 31 // Relative count of days echo $dateTimeInterval->getDays(false); // 0 (15th day of the month (august) - 15th day of the month (july))
$firstDate = new DateTimeImmutable('2022-07-15'); $secondDate = new DateTimeImmutable('2023-08-15'); $dateTimeInterval = new DateTimeInterval($firstDate, $secondDate); // Absolute count of months echo $dateTimeInterval->getMonths(); // 13 // Relative count of months echo $dateTimeInterval->getMonths(false); // 1 (8th month of the year (august) - 7th month of the year (july))
等等
$dateTimeInterval->getYears(); $dateTimeInterval->getHours(); $dateTimeInterval->getMinutes(); $dateTimeInterval->getSeconds(); $dateTimeInterval->getMicroseconds();
结果可以是正数或负数,具体取决于构造函数的第三个参数($returnAbsoluteValue
,默认为true)。
$firstDate = new DateTimeImmutable('2022-07-15'); $secondDate = new DateTimeImmutable('2022-08-15'); $dateTimeInterval = new DateTimeInterval($firstDate, $secondDate, false); echo $dateTimeInterval->getDays(); // -31
结果可以格式化为字符串
$supDate = new DateTime('2021-08-27 00:00:00'); $infDate = new DateTime('2022-09-29 23:35:59'); $dateTimeInterval = new DateTimeInterval($firstDate, $secondDate); // Internally uses DateInterval::format() method echo $dateTimeInterval->format('%y years, %m months, %d days, %h hours, %i minutes, %s seconds'); // 1 years, 1 months, 2 days, 23 hours, 35 minutes, 59 seconds
怀念DateInterval?没问题,您可以用它回来
$dateInterval = $dateTimeInterval->getDateInterval();
目的是什么?
为了理解这个库的实用性,让我们尝试计算一些延迟。
使用date_diff()获取延迟
// Let's say you want to compute some delays between 2022-07-15 and 2022-08-15. $firstDate = new DateTimeImmutable('2022-07-15'); $secondDate = new DateTimeImmutable('2022-08-15'); $dateInterval = date_diff($secondDate, $firstDate); // OR $dateInterval = $firstDate->diff($secondDate); // You want to count the days between these two dates. Using DateInterval, // you notice that two properties are available : // PhpDoc states: "Totals number of days in the interval span" $dateInterval->days; // PhpDoc states: "Number of days" $dateInterval->d; // Well, the doc of ->d is not really clear, let's see the difference between these two returns echo $dateInterval->days; // 30 echo $dateInterval->d; // 0 // Nice, it seems ->d is a relative count, whereas ->days give an absolute count // What about getting the number of months then ? // PhpDoc states: "Number of months" echo $dateInterval->m // 1 // We don't yet if it's a relative or an absolute count. So let's change the year to next one : $firstDate = new DateTimeImmutable('2022-07-15'); $secondDate = new DateTimeImmutable('2023-08-15'); $dateInterval = date_diff($secondDate, $firstDate); echo $dateInterval->m // 1 // Now it's clear: ->m also returns a relative count
实际上:->y
、->m
、->d
、->h
、->i
、->s
、->f
:都返回一个相对计数,这意味着对于任何绝对结果,您必须使用->days
进行一些计算,这是唯一绝对的结果。尽管这些计算很简单,但每次在项目中重写它们都是乏味的,并且可能导致疏忽错误。
这就是为什么制作这个库的原因:抽象计算并实现更面向对象的编程。
版权和许可证
GlucNAc/DateTimeInterval库版权©GlucNAc,许可在MIT许可证(MIT)下使用。有关更多信息,请参阅LICENSE。