pfrug / period
PHP 时间周期管理类
v1.0
2023-03-20 18:41 UTC
Requires
- nesbot/carbon: ^2.66
Requires (Dev)
- phpunit/phpunit: ^10.0
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2024-10-01 00:15:00 UTC
README
时间周期管理类。
此库使用 Carbon
如果您不想在项目中包含 Carbon,可以使用 SimplePeriod,其中使用 DateTime 代替
安装
composer require pfrug/period
两月日期周期
$period = Period::months(2);
echo $period; // From: 2021-09-25 00:19:43, To: 2021-11-25 00:19:43
两年日期周期
$period = Period::years(2);
echo $period; // From: 2021-09-25 00:19:43, To: 2021-11-25 00:19:43
前三周和后两周的周期
$period = Period::weeks(3,2);
echo $period; // From: 2021-11-04 00:19:43, To: 2021-12-09 00:19:43
使用“范围”通过模型使用示例
// List Posts from the last 2 days
$period = Post::days(2);
$posts = Post::active()
->byPeriod($period)
->latest()
->get();
// Model Post
public function scopeByPeriod($q, $period ){
$q->whereBetween('created_at', $period->toArray());
}
用于创建图表的有用函数
如果我们想以 2 天的间隔绘制一个月的值
$period = Period::months(1);
$range = $period->getDatePeriodByTime( 2 , 'day');
foreach( $range as $step ){
print_r($step->format('Y-m-d'));
}
结果
2021-10-25
2021-10-27
2021-10-29
2021-10-31
2021-11-02
2021-11-04
2021-11-06
2021-11-08
2021-11-10
2021-11-12
2021-11-14
2021-11-16
2021-11-18
2021-11-20
2021-11-22
2021-11-24
如果我们想获得一定步数的日期范围,例如 7
$range = $period->getDatePeriod(7);
foreach( $range as $step ){
print_r($step->format('Y-m-d H:i:s'));
}
结果
2021-10-25 00:19:43
2021-10-29 10:45:26
2021-11-02 21:11:09
2021-11-07 07:36:52
2021-11-11 18:02:35
2021-11-16 04:28:18
2021-11-20 14:54:01
乌拉圭时区的 120 分钟周期
$period = Period::minutes(120)->toTimezone( TimeZone::TZ_UY);
print_r($period);
结果
Libraries\Period Object
(
[startDate] => DateTime Object
(
[date] => 2021-11-24 18:19:43.000000
[timezone_type] => 3
[timezone] => America/Montevideo
)
[endDate] => DateTime Object
(
[date] => 2021-11-24 20:19:43.000000
[timezone_type] => 3
[timezone] => America/Montevideo
)
[timezone] => UTC
[outputFormat] => Y-m-d H:i:s
)
更改日期显示的格式
$period = Period::months(2);
echo $period; // From: 2021-09-25 00:19:43, To: 2021-11-25 00:19:43
$period->outputFormat = 'Y-m-d';
echo $period; // From: 2021-09-25, To: 2021-11-25
设置输出时区
默认时区
$period = Period::months(2);
print_r($period);
结果
Libraries\Period Object
(
[startDate] => DateTime Object
(
[date] => 2021-09-25 00:19:43.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[endDate] => DateTime Object
(
[date] => 2021-11-25 00:19:43.000000
[timezone_type] => 3
[timezone] => Europe/Berlin
)
[timezone] => UTC
[outputFormat] => Y-m-d H:i:s
)
乌拉圭时区
$period->toTimezone(TimeZone::TZ_UY);
print_r($period);
结果
Libraries\Period Object
(
[startDate] => DateTime Object
(
[date] => 2021-09-24 19:19:43.000000
[timezone_type] => 3
[timezone] => America/Montevideo
)
[endDate] => DateTime Object
(
[date] => 2021-11-24 20:19:43.000000
[timezone_type] => 3
[timezone] => America/Montevideo
)
[timezone] => UTC
[outputFormat] => Y-m-d H:i:s
)
指明日期输入的时区,我们可以将这些日期转换为适当的时区(默认为 UTC),例如在数据库中执行查询
假设用户为搜索输入日期范围,用户将在他们的时区输入日期,但在数据库中数据以 UTC 存储在这种情况下,我们可以创建 Period 对象并将日期转换为 UTC,指明输入的时区
在乌拉圭时区输入日期
$period = Period::create( '2021-11-05 13:56', '2021-11-09 13:56:39');
将日期转换为 UTC
$period->convertToTimezone(TimeZone::TZ_UY);
print_r($period);
结果
Libraries\Period Object
(
[startDate] => DateTime Object
(
[date] => 2021-11-05 16:56:00.000000
[timezone_type] => 3
[timezone] => UTC
)
[endDate] => DateTime Object
(
[date] => 2021-11-09 16:56:39.000000
[timezone_type] => 3
[timezone] => UTC
)
[timezone] => UTC
[outputFormat] => Y-m-d H:i:s
)