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

ActiveLibrary

krixon/datetime
===============

Datetime value objects.

0.2.0(6y ago)17.2k↑2900%[1 PRs](https://github.com/krixon/datetime/pulls)MITPHP

Since Mar 30Pushed 3y ago1 watchersCompare

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

READMEChangelogDependencies (3)Versions (10)Used By (0)

datetime
========

[](#datetime)

[![Build Status](https://camo.githubusercontent.com/4e7991ae68c55108554ed7870c981167cfb9eecf969e4674036b187e1394feaf/68747470733a2f2f7472617669732d63692e6f72672f6b7269786f6e2f6461746574696d652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/krixon/datetime)[![Coverage Status](https://camo.githubusercontent.com/140e09ab32907201f434bb1dfe8ab7950d5c41bb043f0346bef3ba43612438f9/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f6b7269786f6e2f6461746574696d652f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/krixon/datetime?branch=master)

[![SensioLabsInsight](https://camo.githubusercontent.com/d4da72983a5907dc66f6100b07493a7928a991e1650dc162f007037610316571/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f34393935396130322d386532302d343866622d613132642d3136663264656338326437622f6269672e706e67)](https://insight.sensiolabs.com/projects/49959a02-8e20-48fb-a12d-16f2dec82d7b)

PHP7 date/time library.

Prerequisites
=============

[](#prerequisites)

- PHP 7.0+

Installation
============

[](#installation)

Install via composer
--------------------

[](#install-via-composer)

To install datetime with Composer, run the following command:

```
$ composer require krixon/datetime
```

You can see this library on [Packagist](https://packagist.org/packages/krixon/datetime).

Install from source
-------------------

[](#install-from-source)

```
# HTTP
$ git clone https://github.com/krixon/datetime.git
# SSH
$ git clone git@github.com:krixon/datetime.git
```

Introduction
============

[](#introduction)

This library is a layer on top of PHP's built-in date and time classes which provides additional functionality and improvements such as microsecond precision and immutability (without the inconsistencies between `\DateTime`, `\DateTimeImmutable` and `DateTimeInterface`).

Creating Dates
--------------

[](#creating-dates)

There are various ways to create a new `DateTime` instance.

Using the current time and default timezone:

```
// These objects all represent the current time.
$date = DateTime::now();
$date = DateTime::create();
$date = new DateTime();
```

Using a UNIX timestamp:

```
// Standard (second) precision.
DateTime::fromTimestamp(1499789008)->format('Y-m-d H:i:s.u');
// 2017-07-11 16:03:28.000000

// Millisecond precision.
DateTime::fromTimestampWithMilliseconds(1499789008123)->format('Y-m-d H:i:s.u');
// 2017-07-11 16:03:28.123000

// Microsecond precision.
DateTime::fromTimestampWithMicroseconds(1499789008123456)->format('Y-m-d H:i:s.u');
// 2017-07-11 16:03:28.123456
```

Parsing a string using a specified format:

```
$date = DateTime::fromFormat('Y-m-d H:i:s.u', '2017-07-11 16:03:28.123456');
```

Parsing a string containing any [supported date and time format](http://php.net/manual/en/datetime.formats.php):

```
$date = DateTime::create('yesterday');
$date = DateTime::create('1 month ago');
$date = DateTime::create('first day of January 2008');
$date = DateTime::create('+5 weeks');
$date = DateTime::create('Monday next week');
// etc
```

Using an existing built-in `\DateTime` instance:

```
$date = DateTime::fromInternalDateTime(new \DateTime());
```

Using an existing `\IntlCalendar` instance:

```
$calendar = \IntlCalendar::createInstance();
$calendar->setTime(1499789008123);
$date = DateTime::fromIntlCalendar($calendar);
```

Modifying Dates
---------------

[](#modifying-dates)

All `DateTime` instances are immutable. However methods are provided for creating new instances with modifications applied.

Adjusting the date:

```
$date = DateTime::create('21st March 2017 09:45:00');

$date->withDateAt(2016, 09, 15); // 2016-09-15 09:45:00

// Any components not specified will not be changed.
$date->withDateAt(null, null, 15); // 2017-01-15 09:45:00

// There are also methods for setting the components individually.
$date->withYear(1981);           // 1981-03-21 09:45:00
$date->withMonth(DateTime::JAN); // 2017-01-21 09:45:00
$date->withDay(15);              // 2017-03-15 09:45:00

// Convenience methods for common date adjustments.
$date->withDateAtStartOfYear();                       // 2017-01-01 00:00:00
$date->withDateAtStartOfMonth();                      // 2017-03-01 00:00:00
$date->withDateAtEndOfMonth();                        // 2017-03-31 00:00:00
$date->withDateAtDayOfWeekInMonth(DateTime::TUE, 4);  // 2017-03-28 00:00:00 (4th Tuesday in March 2017)
$date->withDateAtDayOfWeekInMonth(DateTime::MON, -2); // 2017-03-20 00:00:00 (Penultimate Tuesday in March 2017)
$date->withDateAtStartOfWeek('en_GB');                // 2017-03-20 00:00:00 (Monday, start of the week of 21st Match 2017 in Great Britain).
$date->withDateAtStartOfWeek('en_US');                // 2017-03-19 00:00:00 (Sunday, start of the week of 21st Match 2017 in USA).
```

If you are making many changes to a `DateTime` without needing the intermediate objects, you can use the `DateTimeCalculator` class. This supports all of the operations you can do on a `DateTime` object itself but without the overhead of creating new objects which are then thrown away.

For example, imagine you want to add an interval to a base date a number of times, but you are only interested in the final result. While you could call `$date = $date->add('PT1D')` repeatedly, a more efficient method would be:

```
$calculator = DateTime::create('2017-01-01')->calculator();

for ($i = 0; $i < 50; $i++) {
    $calculator->addInterval('PT1D');
}

$date = $calculator->result(); // 2017-02-19
```

Of course this is a contrived example and in reality you would just call `$date = $date->add('PT50D')`, but there are many arithmetic operations you can perform with the calculator which cannot necessarily be achieved as efficiently using just the `DateTime` API.

###  Health Score

30

—

LowBetter than 65% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity59

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

Every ~182 days

Recently: every ~272 days

Total

7

Last Release

2233d ago

### Community

Maintainers

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

---

Top Contributors

[![krixon](https://avatars.githubusercontent.com/u/559165?v=4)](https://github.com/krixon "krixon (39 commits)")

---

Tags

datedatetimetime

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[robmorgan/phinx

Phinx makes it ridiculously easy to manage the database migrations for your PHP app.

4.5k46.2M403](/packages/robmorgan-phinx)[maennchen/zipstream-php

ZipStream is a library for dynamically streaming dynamic zip files from PHP without writing to the disk at all on the server.

1.9k286.3M147](/packages/maennchen-zipstream-php)[paragonie/sodium_compat

Pure PHP implementation of libsodium; uses the PHP extension if it exists

934131.6M153](/packages/paragonie-sodium-compat)[danog/madelineproto

Async PHP client API for the telegram MTProto protocol.

3.4k855.0k18](/packages/danog-madelineproto)[pocketmine/pocketmine-mp

A server software for Minecraft: Bedrock Edition written in PHP

3.5k74.6k86](/packages/pocketmine-pocketmine-mp)[godruoyi/php-snowflake

An ID Generator for PHP based on Snowflake Algorithm (Twitter announced).

8582.3M61](/packages/godruoyi-php-snowflake)

PHPackages © 2026

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