https://github.com/wwtg99/date_period
Generate a series of date period, usually for statistics. 基于日、周、月、年等的时间段切分。
https://github.com/wwtg99/date_period
datetime period
Last synced: 5 months ago
JSON representation
Generate a series of date period, usually for statistics. 基于日、周、月、年等的时间段切分。
- Host: GitHub
- URL: https://github.com/wwtg99/date_period
- Owner: wwtg99
- License: mit
- Created: 2017-09-19T02:07:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2017-09-22T05:31:11.000Z (over 8 years ago)
- Last Synced: 2025-04-06T18:50:56.774Z (10 months ago)
- Topics: datetime, period
- Language: PHP
- Homepage:
- Size: 9.77 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Date Period
===========
Generate a series of date period, usually for statistics.
基于日、周、月、年等的时间段切分。
# Installation
```
composer require wwtg99/date_period
```
or add
```
"wwtg99/date_period": "*"
```
to composer.json
# Test
Unit test file in tests. Tested in PHP 7.1.
# Usage
## Methods
- DatePeriod::getPeriodArray($datePeriod, $start, $end, $dateFormat)
- DatePeriod::getPeriodGenerator($datePeriod, $start, $end, $dateFormat)
### Params
- $datePeriod: period to separate, day, week, month, year ...
- $start: start time string
- $end: end time string
- $dateFormat: date format(default) or datetime format
## Examples
### Generate day array between start day(included) and end day(excluded)
```php
$start = '2017-09-01';
$end = '2017-09-10';
$arr = DatePeriod::getPeriodArray(DatePeriod::TYPE_PERIOD_DAY, $start, $end);
foreach ($arr as $item) {
echo "Title: ", $item->getTitle(), " From: ", $item->getStartString(), " To: ", $item->getEndString(), "\n";
}
```
Output:
```
Title: 2017-09-01 From: 2017-09-01 To: 2017-09-02
Title: 2017-09-02 From: 2017-09-02 To: 2017-09-03
Title: 2017-09-03 From: 2017-09-03 To: 2017-09-04
Title: 2017-09-04 From: 2017-09-04 To: 2017-09-05
Title: 2017-09-05 From: 2017-09-05 To: 2017-09-06
Title: 2017-09-06 From: 2017-09-06 To: 2017-09-07
Title: 2017-09-07 From: 2017-09-07 To: 2017-09-08
Title: 2017-09-08 From: 2017-09-08 To: 2017-09-09
Title: 2017-09-09 From: 2017-09-09 To: 2017-09-10
Title: 2017-09-10 From: 2017-09-10 To: 2017-09-11
```
### Generate month array between start day(included) and end day(excluded)
```php
$start = '2017-05-11';
$end = '2017-09-10';
$arr = DatePeriod::getPeriodArray(DatePeriod::TYPE_PERIOD_MONTH, $start, $end);
foreach ($arr as $item) {
echo "Title: ", $item->getTitle(), " From: ", $item->getStartString(), " To: ", $item->getEndString(), "\n";
}
```
Output:
```
Title: 2017-05 From: 2017-05-01 To: 2017-06-01
Title: 2017-06 From: 2017-06-01 To: 2017-07-01
Title: 2017-07 From: 2017-07-01 To: 2017-08-01
Title: 2017-08 From: 2017-08-01 To: 2017-09-01
Title: 2017-09 From: 2017-09-01 To: 2017-10-01
```
### Use generator instead of array (usually for very long period)
Generator support PHP 7.0+.
```php
$start = '2017-07-11';
$end = '2017-09-10';
foreach (DatePeriod::getPeriodGenerator(DatePeriod::TYPE_PERIOD_WEEK, $start, $end) as $item) {
echo "Title: ", $item->getTitle(), " From: ", $item->getStartString(), " To: ", $item->getEndString(), "\n";
}
```
Output:
```
Title: 2017-07 W2 From: 2017-07-10 To: 2017-07-17
Title: 2017-07 W3 From: 2017-07-17 To: 2017-07-24
Title: 2017-07 W4 From: 2017-07-24 To: 2017-07-31
Title: 2017-07 W5 From: 2017-07-31 To: 2017-08-07
Title: 2017-08 W1 From: 2017-08-07 To: 2017-08-14
Title: 2017-08 W2 From: 2017-08-14 To: 2017-08-21
Title: 2017-08 W3 From: 2017-08-21 To: 2017-08-28
Title: 2017-08 W4 From: 2017-08-28 To: 2017-09-04
Title: 2017-09 W1 From: 2017-09-04 To: 2017-09-11
```
## Supported period
- Second: every second from start to end
- Minute: every minute from start to end
- Hour: every hour from start to end
- Day: every day from start to end
- Week: every week from start to end
- Month: every month from start to end
- Season: every season from start to end
- Year: every year from start to end
- None: same as DateInterval