Giter VIP home page Giter VIP logo

date_period's Introduction

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)

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

$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+.

$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

date_period's People

Contributors

wwtg99 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.