pfrug/simpleperiod

PHP 时间周期管理类

v1.0 2023-03-20 21:34 UTC

This package is auto-updated.

Last update: 2024-10-01 00:15:00 UTC


README

时间周期管理类。

该库使用 DateTime

您也可以使用 Period,它使用 Carbon

安装

composer require pfrug/simpleperiod

两个月日期周期

$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 

3周前和2周后的周期

$period = Period::weeks(3,2);
echo $period; // From: 2021-11-04 00:19:43, To: 2021-12-09 00:19:43 

通过 "Scopes" 使用模型示例

// 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
)