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

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

mindplay/datetime
=================

Chainable date/time helper for convenient work with integer timestamps

1.1.0(11y ago)1181LGPL-3.0+PHPPHP &gt;=5.3.0

Since Oct 30Pushed 11y ago2 watchersCompare

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

READMEChangelogDependencies (1)Versions (4)Used By (0)

mindplay/datetime
-----------------

[](#mindplaydatetime)

[![Build Status](https://camo.githubusercontent.com/3570cd7e2cf557555062ecd53d43d2f78bb23bc7a6d61cdae324dd63e77a98e4/68747470733a2f2f7472617669732d63692e6f72672f6d696e64706c61792d646b2f6461746574696d652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/mindplay-dk/datetime)

[![Code Coverage](https://camo.githubusercontent.com/a1cd7a5bf7cacdfb9cdd9576521ba48268d96e536df5d4da82d41918be66e47c/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f6461746574696d652f6261646765732f636f7665726167652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/datetime/?branch=master)

[![Scrutinizer Code Quality](https://camo.githubusercontent.com/aaffa0bf224e7ea50915d82733f71b4b3c53ef3c4a373d5de7b4ab7d72a59bb3/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6d696e64706c61792d646b2f6461746574696d652f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/mindplay-dk/datetime/?branch=master)

This date/time helper tries to make date/time management in PHP less prickly, by providing a chainable helper that can be accessed via a global function `datetime()`.

I wanted a convenient way to work with timestamps, which would provide the same convenience (and IDE support) as e.g. `DateTime`, but without the problems.

The global `datetime()` function takes an integer timestamp as argument, or a valid date/time string compatible with `strtotime()`, applies it to the helper, and returns it.

```
$time = datetime('1975-07-07')->time; // parse date/time string to timestamp

$str = datetime(time())->datetime; // timestamp to 'YYYY-MM-DD HH:MM:SS' format

```

The helper has a configuration object that defines global date/time formats and a default timezone, which is UTC by default, regardless of any environment settings - since timestamps (unlike `DateTime`) do not carry around timezone information, the default timezone can be changed, but you can also switch to different timezones for easy string output.

```
$a = datetime()->utc()->long; // system date/time in long format

$b = datetime()->timezone('EST')->short; // short date/time in EST timezone

```

The helper implements `__toString()` and will format itself using the `'default'`format, which is also configurable. You can define as many formats as you want, or specify the format directly, and you can render these using `->format()` at the end of a chain. Default formats like `'short'`, `'long'`, `'string'` and `'date'` are also supported directly as (dynamic) properties.

```
datetime()->config->formats['weekday'] = 'l';

echo datetime()->timezone('PST')->format('weekday'); // current weekday in PST

```

Basic computations can also be performed using plain english:

```
$today = datetime()->date()->time; // date() resets the time to 00:00:00

$this_month = datetime()->month()->time; // month() resets to start of the month

$next_week = datetime()->add('1 week')->time;

$whenever = datetime()->add('1 month')->sub('3 days 2 hours 1 minute')->time;

```

One word of caution: unless you `echo` the result (invoking `__toString()`) your call-chains should always end with a property rather than a method `()` call - the properties of the helper-class never return the helper object, always a value.

```
$bad = datetime(); // reference to DateTimeHelper !

$wrong = datetime()->timezone('EST')->add('1 week'); // oh noes!

```

There's a bunch of other features, go ahead and check out the unit-test for examples of every possible operation, or play around using auto-complete in your IDE.

Why not objects?
----------------

[](#why-not-objects)

Nobody doesn't love objects, but `DateTime` is trouble - watch:

```
$today = new DateTime();
$yesterday = $today->modify('-1 day');

echo "today: " . $today->format('Y-m-d') . "\n";
echo "yesterday: " . $yesterday->format('Y-m-d');

```

If you understand how `DateTime` works, you might be prepared for this nonsense:

```
today: 2013-03-21
yesterday: 2013-03-21

```

Methods like `modify()`, `add()` and `sub()` modify the `DateTime` object, rather than returning a new `DateTime` instance.

And sure, you got `DateTimeImmutable` in more recent versions of PHP (and before it, dozens of third-party immutable date/time object implementations in userland) but you're still working with objects.

A timestamp is a value - when dealing with timestamps, I therefore want a value type, not an object, which is more complicated to handle when dealing with e.g. serialization, object/relational-mapping, etc.

When dealing with timezones, I don't want the timezone attached to the timestamp, because the timestamp and timezone do not actually have a meaningful relationship: timestamps are absolute, and that doesn't change when you attach it to a timezone; the only time that isn't true, is when you want the date/time as a string, but it's often more confusing to carry around the timezone information with the value for later use, since, when it finally gets turned into a string, it may not be obvious what timezone is being used - this can make programs rather confusing and difficult to read.

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity60

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 ~38 days

Total

3

Last Release

4143d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/9445f567f43ee7a963270651e40e533634586f959e4df3d5398d001b1cb49be8?d=identicon)[mindplay.dk](/maintainers/mindplay.dk)

---

Top Contributors

[![mindplay-dk](https://avatars.githubusercontent.com/u/103348?v=4)](https://github.com/mindplay-dk "mindplay-dk (7 commits)")

### Embed Badge

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

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

PHPackages © 2026

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