PHPackages                             pfrug/period - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. pfrug/period

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

pfrug/period
============

PHP Class for managing time periods

v1.0(3y ago)1341MITPHP

Since Mar 20Pushed 2y ago1 watchersCompare

[ Source](https://github.com/pfrug/period)[ Packagist](https://packagist.org/packages/pfrug/period)[ RSS](/packages/pfrug-period/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (1)Dependencies (3)Versions (2)Used By (0)

Period
======

[](#period)

Class for managing time periods.

This library uses [Carbon](https://carbon.nesbot.com/)

If you prefer not to include Carbon in your project, you can use [SimplePeriod](https://github.com/pfrug/simple-period) where [DateTime](https://www.php.net/manual/es/class.datetime.php) is used instead

Installation
------------

[](#installation)

```
composer require pfrug/period
```

### Two-month date period

[](#two-month-date-period)

```
$period = Period::months(2);
echo $period; // From: 2021-09-25 00:19:43, To: 2021-11-25 00:19:43

```

### Two-years date period

[](#two-years-date-period)

```
$period = Period::years(2);
echo $period; // From: 2021-09-25 00:19:43, To: 2021-11-25 00:19:43

```

### Period of 3 weeks before and 2 weeks after

[](#period-of-3-weeks-before-and-2-weeks-after)

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

```

### Example of use with Models through "Scopes"

[](#example-of-use-with-models-through-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());
}

```

### Useful functions for creating graphs

[](#useful-functions-for-creating-graphs)

If we want to graph the values of a month in intervals of 2 days

```
$period = Period::months(1);
$range = $period->getDatePeriodByTime( 2 , 'day');

foreach( $range as $step ){
  print_r($step->format('Y-m-d'));
}

```

Result:

```
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

```

If we want to obtain a range of dates in a certain amount of steps, for example 7

```
$range = $period->getDatePeriod(7);
foreach( $range as $step ){
	print_r($step->format('Y-m-d H:i:s'));
}

```

Resutl:

```
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 minute period in Uruguay time zone

[](#120-minute-period-in-uruguay-time-zone)

```
$period = Period::minutes(120)->toTimezone( TimeZone::TZ_UY);
print_r($period);

```

Result:

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

```

### Change the format in which dates are displayed

[](#change-the-format-in-which-dates-are-displayed)

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

```

### Set output timezone

[](#set-output-timezone)

Default Timezone

```
$period = Period::months(2);
print_r($period);

```

Result:

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

```

Timezone Uruguay

```
$period->toTimezone(TimeZone::TZ_UY);
print_r($period);

```

Result:

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

```

### Indicating in which timezone the dates were entered we can convert these dates to the appropriate timezone (by default UTC) for example to perform queries in the db

[](#indicating-in-which-timezone-the-dates-were-entered-we-can-convert-these-dates-to-the-appropriate-timezone-by-default-utc-for-example-to-perform-queries-in-the-db)

Suppose that users enter a range of dates for a search, the user will enter the dates in their time zone but in the DB the data is stored in UTC, In this case we can create the Period object and convert the dates to UTC indicating in which timezone were entered

Enter dates in Uruguay time zone

```
$period = Period::create( '2021-11-05 13:56', '2021-11-09 13:56:39');

```

Convert dates to UTC

```
$period->convertToTimezone(TimeZone::TZ_UY);
print_r($period);

```

Result:

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

```

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity45

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

1191d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/bdea59e3f90698df3c5fa81e8b9f40e0444a32a1bc99270314ef976a90735e70?d=identicon)[frugone](/maintainers/frugone)

---

Top Contributors

[![pfrug](https://avatars.githubusercontent.com/u/5391920?v=4)](https://github.com/pfrug "pfrug (5 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/pfrug-period/health.svg)

```
[![Health](https://phpackages.com/badges/pfrug-period/health.svg)](https://phpackages.com/packages/pfrug-period)
```

###  Alternatives

[illuminate/support

The Illuminate Support package.

582110.9M39.8k](/packages/illuminate-support)[craftcms/feed-me

Import content from XML, RSS, CSV or JSON feeds into entries, categories, Craft Commerce products, and more.

293943.4k27](/packages/craftcms-feed-me)[solspace/craft-freeform

The most flexible and user-friendly form building plugin!

53675.5k15](/packages/solspace-craft-freeform)[erlandmuchasaj/laravel-gzip

Gzip your responses.

40140.4k2](/packages/erlandmuchasaj-laravel-gzip)[japanese-date/japanese-date

日本の暦、祝日を取り扱うライブラリ

169.9k](/packages/japanese-date-japanese-date)[solspace/craft-calendar

The most powerful event management and calendaring plugin!

1831.6k1](/packages/solspace-craft-calendar)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
