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

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

prokki/ext-datetime
===================

Extends the common php DateTime objects with a lot of additional helpful methods.

0.0.0(6y ago)3841MITPHPPHP &gt;=7.3.0

Since Jul 31Pushed 6y ago1 watchersCompare

[ Source](https://github.com/prokki/ext-datetime)[ Packagist](https://packagist.org/packages/prokki/ext-datetime)[ RSS](/packages/prokki-ext-datetime/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (1)Versions (3)Used By (1)

Ext-DateTime
============

[](#ext-datetime)

[![LICENSE](https://camo.githubusercontent.com/00177b0942bdbfce32a1ae9fe33b0769b1b95e5407a0b48178bb43a5daec2636/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656c656173652d3025324530253245302d626c75652e7376673f7374796c653d666c6174)](https://camo.githubusercontent.com/00177b0942bdbfce32a1ae9fe33b0769b1b95e5407a0b48178bb43a5daec2636/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72656c656173652d3025324530253245302d626c75652e7376673f7374796c653d666c6174)[![Packagist](https://camo.githubusercontent.com/1db76e41c7b5a995f3cfbcd1a702d7457df90c02ef73c94e87d2f44525d84151/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5061636b61676973742d3025324530253245302d626c75652e7376673f7374796c653d666c6174)](https://packagist.org/packages/prokki/ext-datetime)[![LICENSE](https://camo.githubusercontent.com/59497bc4563dd468e37bbf539439e60cacf196f537f678b721a8efabf21c64ef/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e73652d4d49542d626c75652e7376673f7374796c653d666c6174)](LICENSE)[![PHP v7.3](https://camo.githubusercontent.com/25d250283c1b799eff855ef8286cb0dac681722c14f08ae577e23656f0335fe9/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d25453225383925413537253245332d3030343461612e737667)](https://www.php.net/manual/en/migration73.new-features.php)[![codecov](https://camo.githubusercontent.com/c27309f22566725a2e732dde84736dde765b356e21c43b9da956c215aba088a5/68747470733a2f2f636f6465636f762e696f2f67682f70726f6b6b692f6578742d6461746574696d652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/prokki/ext-datetime)[![Build Status](https://camo.githubusercontent.com/eadb84da2d40a32f207f0ab6f94edb6cbd1d3961f33708a4cf53f68f27374d7e/68747470733a2f2f7472617669732d63692e6f72672f70726f6b6b692f6578742d6461746574696d652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/prokki/ext-datetime)

Extends the native datetime objects ([\\DateTime](https://www.php.net/manual/en/class.datetime.php) and [\\DateTimeImmutable](https://www.php.net/manual/en/class.datetimeimmutable.php)) with a lot of additional helpful methods.

Most of the new methods are **short cuts of already existing functionality**. But these methods will help you to save time and code if you need to handle a many date- and time-operations.

All new methods support [*method chaining*](https://stackoverflow.com/questions/3724112/php-method-chaining).

Table of Contents
-----------------

[](#table-of-contents)

- [Requirements](#requirements)
- [Integration](#integration)
- [Usage](#usage)
    - [Static Initialization](#static-initialization)
    - [Cloning](#cloning)
    - [Manipulating](#manipulating)
    - [Instant Setter Methods](#instant-setter-methods)

Requirements
------------

[](#requirements)

The usage of [**PHP v7.3**](https://www.php.net/manual/en/migration73.new-features.php) is obligatory.

Integration
-----------

[](#integration)

Please install via [composer](https://getcomposer.org/).

```
composer require prokki/ext-datetime "^0.0"
```

Usage
-----

[](#usage)

The classes and instances of the classes [ExtDateTime/DateTime](src/DateTime.php) and [ExtDateTime/DateTimeImmutable](src/DateTimeImmutable.php) can be used exactly like native datetime objects/classes.

Example

```
use ExtDateTime\DateTime;
use ExtDateTime\DateTimeImmutable;

// create a datetime object
$dateTime = new DateTime("now");

// create an immutable datetime object
$dateTimeImmutable = new DateTimeImmutable("now");
```

### Static Initialization

[](#static-initialization)

Similar to the native objects there are several others ways to create a datetime object.

All static methods can be used to implement [*method chaining*](https://stackoverflow.com/questions/3724112/php-method-chaining).

#### create()

[](#create)

Use the static constructor `create()` to use chaining immediately.

```
use ExtDateTime\DateTime;

// create a datetime object
// and use chaining immediately
$dateTime = DateTime::create("now")
            ->addHours(5)
            ->addDays(5)
            ;
```

#### current()

[](#current)

The static constructor `current()` returns a datetime object with the current date/time. But in opposite to `DateTime::create("now")` this method returns the object **with additional microtime**.

```
use ExtDateTime\DateTime;

$currentMicroseconds = DateTime::current()->format("u");         // output example 654321
$noMicroseconds      = DateTime::create("now")->format("u");     // output always 000000
```

#### createFromObject()

[](#createfromobject)

Creates a new object from any datetime object implementing the [DateTimeInterface](https://www.php.net/manual/en/class.datetimeinterface.php).

```
use ExtDateTime\DateTimeImmutable;

// create an immutable datetime object from a native non-immutable object
$datetime = DateTimeImmutable::createFromObject(new \DateTime());
```

### Cloning

[](#cloning)

Two new methods add the availability to use chaining directly after cloning an object.

#### duplicate()

[](#duplicate)

This method is just a wrapper function for `clone`:

```
use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

// clone the datetime object and proceed like usual with chaining
$clone = $datetime->duplicate()
         ->addHours(5)
         ->addDays(5)
         ;
```

#### toImmutable() / toMutable()

[](#toimmutable--tomutable)

Instead of using static constructors ([DateTime::createFromImmutable](https://www.php.net/manual/en/datetime.createfromimmutable.php) or [DateTimeImmutable::createFromMutable](https://www.php.net/manual/en/datetimeimmutable.createfrommutable.php)) you can also use these new non-static methods.

```
use ExtDateTime\DateTime;

// create a datetime object
$datetime = DateTime::current();

$clonedImmutable = $datetime->toImmutable();       // only available in class DateTime

$clonedMutable   = $clonedImmutable->toMutable();  // only available in class DateTimeImmutable
```

### Manipulating

[](#manipulating)

Most of the new methods are short cuts to avoid re-initialization of necessary parameter in your code.

#### addHours() / subHours()

[](#addhours--subhours)

Adds or subtracts hours of a datetime object.

```
use ExtDateTime\DateTime;

// create a datetime object and add 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 22:35:17"

// create a datetime object and subtracts 10 hours
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subHours(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 02:35:17"
```

#### addDays() / subHours()

[](#adddays--subhours)

Adds or subtracts days of a datetime object.

```
use ExtDateTime\DateTime;

// create a datetime object and add 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 days
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subDays(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"
```

#### addMonth() / subMonth()

[](#addmonth--submonth)

Adds or subtracts months of a datetime object.

**Attention**: This methods behaves differently than the suspected background function of adding days. If the current date is the last day of the month (31/30/29/28) and the target month has less days then the current month, the day will be set to the last day of the target month.

Example: The datetime object is

> *2017-01-30 17:00:00*

and you want to add *1 month*, the result will be

> *2017-02-28 17:00:00*

```
use ExtDateTime\DateTime;

// create a datetime object and add 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->addMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-08-09 22:35:17"

// create a datetime object and subtracts 10 months
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->subMonth(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-20 02:35:17"
```

### Instant Setter Methods

[](#instant-setter-methods)

#### toEndOfDay()

[](#toendofday)

Sets the time to the end of the day (*23:59:59*).

```
use ExtDateTime\DateTime;

// create a datetime object and sets the time to the end of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 23:59:59"
```

#### toNoon()

[](#tonoon)

Sets the time to noon (*12:00:00*).

```
use ExtDateTime\DateTime;

// create a datetime object and sets the time to noon
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toNoon(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 12:00:00"
```

#### toStartOfDay()

[](#tostartofday)

Sets the time to the start of the day (*00:00:00*).

```
use ExtDateTime\DateTime;

// create a datetime object and sets the time the start of the day
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfDay(10)
                  ->format("Y-m-d h:i:s");                   // "2020-07-30 00:00:00"
```

#### toStartOfMonth()

[](#tostartofmonth)

Sets the date to the first day of the month and additionally the time to the start of the day (*00:00:00*).

```
use ExtDateTime\DateTime;

// create a datetime object and sets the date to the first day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toStartOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-01 00:00:00"
```

#### toEndOfMonth()

[](#toendofmonth)

Sets the date to the last day of the month and additionally the time to the end of the day (*23:59:59*).

```
use ExtDateTime\DateTime;

// create a datetime object and sets the date to the last day of the month
$datetimeFuture = DateTime::create("2020-07-30 12:35:17")
                  ->toEndOfMonth()
                  ->format("Y-m-d h:i:s");                   // "2020-07-31 23:59:59"
```

###  Health Score

23

—

LowBetter than 27% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community9

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

2480d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/53d7b96b1c455fca0c2900a162ddfe96df516c81f20db1847d88be9f0345e425?d=identicon)[prokki](/maintainers/prokki)

---

Top Contributors

[![prokki](https://avatars.githubusercontent.com/u/5040666?v=4)](https://github.com/prokki "prokki (14 commits)")

---

Tags

datetimephp73phpdatetimeimmutable

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[aeon-php/calendar

PHP type safe, immutable calendar library

2079.7M16](/packages/aeon-php-calendar)[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)
