PHPackages                             pfrug/simpleperiod - 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/simpleperiod

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

pfrug/simpleperiod
==================

PHP class for managing time period

v1.0(3y ago)015MITPHP

Since Mar 20Pushed 1y ago1 watchersCompare

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

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

Period
======

[](#period)

Class for managing time periods.

This library uses [DateTime](https://www.php.net/manual/es/class.datetime.php)

Instead you can use [Period](https://github.com/pfrug/period) which uses [Carbon](https://carbon.nesbot.com/)

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

[](#installation)

```
composer require pfrug/simpleperiod
```

### 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-yeanrs date period

[](#two-yeanrs-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

23

—

LowBetter than 27% of packages

Maintenance26

Infrequent updates — may be unmaintained

Popularity6

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

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

1151d 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 (14 commits)")

---

Tags

datetimephp

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[omaralalwi/laravel-trash-cleaner

clean logs and debug files (clockwork , laravel telescope and more)

221.8k](/packages/omaralalwi-laravel-trash-cleaner)

PHPackages © 2026

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