PHPackages                             andydune/datetime - 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. andydune/datetime

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

andydune/datetime
=================

Extends DateTime class for add some comfort.

v2.3.1(7y ago)03882MITPHPPHP &gt;=7.1CI failing

Since Apr 3Pushed 5y ago1 watchersCompare

[ Source](https://github.com/AndyDune/DateTime)[ Packagist](https://packagist.org/packages/andydune/datetime)[ Docs](https://github.com/AndyDune/DateTime)[ RSS](/packages/andydune-datetime/feed)WikiDiscussions master Synced 5d ago

READMEChangelog (10)Dependencies (2)Versions (18)Used By (2)

DateTime
========

[](#datetime)

[![Build Status](https://camo.githubusercontent.com/ba795603f86bef18ad5cd43cc066b0d859df70d461e8516c9bf3611512c0d2c6/68747470733a2f2f7472617669732d63692e6f72672f416e647944756e652f4461746554696d652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/AndyDune/DateTime)[![Software License](https://camo.githubusercontent.com/55c0218c8f8009f06ad4ddae837ddd05301481fcf0dff8e0ed9dadda8780713e/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d627269676874677265656e2e7376673f7374796c653d666c61742d737175617265)](LICENSE)[![Packagist Version](https://camo.githubusercontent.com/0898f8fd80454ef4dd5b4d7f7040255f959903a162a3dc2155c28a9af200df1d/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f616e647964756e652f6461746574696d652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/datetime)[![Total Downloads](https://camo.githubusercontent.com/85e0706defa89767458f1b9cd709d797f123de90f6a15148460940cf3b98003a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f616e647964756e652f6461746574696d652e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andydune/datetime)

It extends DateTime class to add some comfort.

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

[](#installation)

Installation using composer:

```
composer require andydune/datetime

```

Or if composer was not installed globally:

```
php composer.phar require andydune/datetime

```

Or edit your `composer.json`:

```
"require" : {
     "andydune/datetime": "^2"
}

```

And execute command:

```
php composer.phar update

```

How to create instance
----------------------

[](#how-to-create-instance)

Constructor without parameters set current time value.

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime();
$dt->getTimestamp; // == time()
```

Constructor with integer parameter is unit seconds.

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime(time() + 3600);
$dt->getTimestamp; // == time() + 3600
```

Constructor with string parameter is the same for function `strtotime()`

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime('2018-04-11'); // default format is mysql datetime
$dt = new DateTime('11.04.2017', 'd.m.Y'); // own format - use string as for date() function
```

Constructor with parameter `\Datetime` type

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime(new \DateTime());
```

Format datetime
---------------

[](#format-datetime)

It has method `format()` to get formated datetiem data as string. Method waits string like date() function.

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime();
echo $dt->format('Y-m-d'); // 2107-04-12
echo $dt->format('H:i'); // 10:04
```

Dates arithmetic.
-----------------

[](#dates-arithmetic)

Each duration period is represented by an integer value followed by a period designator. If the duration contains time elements, that portion of the specification is preceded by the letter T.

Period Designators:

- Y - years,
- M - months,
- D - days,
- W - weeks,
- H - hours,
- M - minutes,
- S - seconds.

Examples:

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime();
$dt->add('2D'); // two days
$dt->add('T2S'); // two seconds
$dt->add('6YT5M'); // six years and five minutes
```

The unit types must be entered from the largest scale unit on the left to the smallest scale unit on the right. Use first "-" char for negative periods. OR Relative period.

Examples:

```
use AndyDuneTest\DateTime\DateTime;
$dt = new DateTime();
$dt->add('+5 weeks'); // 5 weeks to future
$dt->add('12 day'); // 12 days to future
$dt->add('-7 weekdays'); // 7 working daye to past
$dt->add('3 months - 5 days'); // 3 months to future and 5 days to past
```

Tools
-----

[](#tools)

### Bring per day statistics to weeks.

[](#bring-per-day-statistics-to-weeks)

If there is no full weekdays number is source data missing days will be added as average.

Source json:

```
$json = '
   {
    "2018-03-01" : 913,
    "2018-03-03" : 913,
    "2018-03-04" : 913,

    "2018-03-05" : 910,
    "2018-03-07" : 914,
    "2018-03-08" : 915,
    "2018-03-09" : 915,
    "2018-03-11" : 912,

    "2018-03-12" : 869,
    "2018-03-14" : 869,
    "2018-03-16" : 869,
    "2018-03-17" : 864,
}';
```

```
use AndyDune\DateTime\Tool\Statistics\BringNumberInDayToNumberInWeek;
$data = json_decode($json, true);

$stat = new BringNumberInDayToNumberInWeek($data);
$weeks = $stat->getWeeksWithCalendarDivision();
```

Weeks are:

```
 {
    "2018-03-04" : 6391,
    "2018-03-11" : 6393,
    "2018-03-18" : 6075
}
```

Strategy pattent
----------------

[](#strategy-pattent)

There is great instrument to manipulatin with `DateTime` object without editting existing code in this library.

Methods
-------

[](#methods)

`DateTime::setAction(AbstractAction $action)` - add action for further execution

`DateTime::executeAction(...$params)` - execute actions with any patrams

### To know is working day

[](#to-know-is-working-day)

```
use AndyDune\DateTime\Action\IsWorkingDay;
use AndyDune\DateTime\DateTime;

$dt = new DateTime('18-04-2018', 'd-m-Y');
$dt->setAction(new IsWorkingDay())->executeAction(); // true

$dt = new DateTime('22-04-2018', 'd-m-Y');
$dt->setAction(new IsWorkingDay())->executeAction(); // false
```

### To know closest working date after pointed days number

[](#to-know-closest-working-date-after-pointed-days-number)

There is an action `AndyDune\DateTime\Action\PlusWorkingDays` for this task.

```
use AndyDune\DateTime\Action\PlusWorkingDays;
use AndyDune\DateTime\DateTime;

$dt = new DateTime('28-04-2018', 'd-m-Y');
$action = new PlusWorkingDays();
$action->setNoWorkingDays(['1-05', '30-04']);  // set list of official holidays
$action->setWorkingDays(['28-04']); // set list of working sundays or saturdays
$dt->setAction($action)->executeAction(1); // to know working date after 1 day

$dt->format('d-m-Y'); // '02-05-2018'
$action->getDaysPlus(); // 4 days
```

###  Health Score

30

—

LowBetter than 64% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity67

Established project with proven stability

 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

Every ~10 days

Recently: every ~26 days

Total

17

Last Release

2804d ago

Major Versions

v1.3.0 → v2.0.02018-04-20

### Community

Maintainers

![](https://www.gravatar.com/avatar/79da3b2173a2cefb36abc9b4707cf2c633df8f2c748633ccf64186f5c0e7be6c?d=identicon)[AndyDune](/maintainers/AndyDune)

---

Top Contributors

[![AndyDune](https://avatars.githubusercontent.com/u/3772910?v=4)](https://github.com/AndyDune "AndyDune (29 commits)")

---

Tags

datetimephp7phpdatetime

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/andydune-datetime/health.svg)

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

###  Alternatives

[kartik-v/php-date-formatter

A Javascript datetime formatting and manipulation library using PHP date-time formats.

461.5M3](/packages/kartik-v-php-date-formatter)[dater/dater

Compact PHP library for working with date/time in different formats &amp; timezones.

14282.3k](/packages/dater-dater)[zjkal/time-helper

一个简单快捷的PHP日期时间助手类库。 a smart PHP datetime helper library.

21128.6k1](/packages/zjkal-time-helper)

PHPackages © 2026

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