brtriver/date-range
Composer 安装命令:
composer require brtriver/date-range
包简介
Simple date ranges for PHP
README 文档
README
DateRange is a simple representation of date range for PHP.
Requirements
DateRange works with PHP 5.4.0 or later.
Install
The recommended way to install date-range is with composer.
$ composer require brtriver/date-range
Usage
Instance
Pass two date(start and end) args like below:
use Brtriver\DateRange\DateRange; $range = new DateRange('2015-12-01', '2015-12-31');
or Pass array of date :
$range = new DateRange(['2015-12-01', '2015-12-31']);
Not only string format but DateTime object is also accepted :
$start = new DateTime('2012-12-01'); $end = new DateTime('2012-12-31'); $range = new DateRange([$start, $end]);
Exception
if DateRange cannot accept construct parameters, it throw InvalidArgumentException. So in a short scope, you have to catch the exception.
try { $range = new DateRange('tomorrow', 'today'); } catch (\InvalidArgumentException $e) { echo $e->getMessage(); // end date is the day before than start date }
access two date
DateRange has 2 date as DateTime object. and you can access with getter methods.
# if you get start DateTime object. $start = $range->getStart(); # if you get end DateTime object. $end = $range->getEnd();
contains
## check contains specific date var_dump($range->contains('2015-12-10')); // bool(true) var_dump($range->contains('2017-01-10')); // bool(false)
diff
get DateInterval between start and end date to call diff:
$range = new DateRange('2015-12-01', '2015-12-03'); $range->diff()->format('%R%a days'); // +2 days
toString
get start and end date string to echo the instance:
$range = new DateRange('2015-12-01', '2015-12-03'); echo $range; // '2015-12-01 ~ 2015-12-03'
If you want to change the format, to call toString method with your format and separator:
$range = new DateRange('2015-12-01', '2015-12-03'); echo $range->toString('Y/m/d', '-'); // '2015/12/01 - 2015/12/03'
Period
You can use DateRange in foreach.
foreach ($range as $d) { echo $d->format('Y-m-d') . PHP_EOL; }
If you use DatePeriod object directly, you can also get DatePeriod object through getDatePeriod like below:
foreach ($range->getDatePeriod() as $d) { echo $d->format('Y-m-d') . PHP_EOL; } // 2015-12-01 // 2015-12-02 // 2015-12-03 // 2015-12-04 // ... // 2015-12-28 // 2015-12-29 // 2015-12-30 // 2015-12-31
Default interval is set to P1D.
If you want to change interval, call setInterval
## if you want to change interval, use setInterval() $start = new DateTime('2012-12-01'); $end = new DateTime('2020-12-31'); $range = new DateRange([$start, $end]); $range->setInterval(new DateInterval('P1Y')); // change from 'P1D' (Default) foreach ($range->getDatePeriod() as $d) { echo $d->format('Y-m-d') . PHP_EOL; } // 2012-12-01 // 2013-12-01 // 2014-12-01 // 2015-12-01 // 2016-12-01 // 2017-12-01 // 2018-12-01 // 2019-12-01 // 2020-12-01
exclude start or end date
If you want to exclude start (end) date in range, call excludeStartDate() or excludeEndDate() like below:
// exclude start date $range = new DateRange('2015-12-01', '2015-12-03'); $range->excludeStartDate(); foreach ($range => $d) { echo $d->format('Y-m-d') . PHP_EOL; } // 2015-12-02 // 2015-12-03 // exclude end date $range = new DateRange('2015-12-01', '2015-12-03'); $range->excludeEndDate(); foreach ($range => $d) { echo $d->format('Y-m-d') . PHP_EOL; } // 2015-12-01 // 2015-12-02
try demo
If you show demo, you have only to run make demo.
cd ./vendor/brtriver/date-range
make setup
make install
make demo
License
DateRange is licensed under the MIT license.
brtriver/date-range 适用场景与选型建议
brtriver/date-range 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.14k 次下载、GitHub Stars 达 9, 最近一次更新时间为 2015 年 12 月 27 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 brtriver/date-range 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 brtriver/date-range 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 51.14k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 9
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-27