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

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

choval/datetime
===============

A drop-in replacement for PHP's DateTime with more flexibility

v0.3.0(2y ago)2201[1 PRs](https://github.com/choval/datetime/pulls)MITPHPCI passing

Since Aug 12Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/choval/datetime)[ Packagist](https://packagist.org/packages/choval/datetime)[ RSS](/packages/choval-datetime/feed)WikiDiscussions master Synced 4d ago

READMEChangelog (10)Dependencies (1)Versions (13)Used By (0)

Choval\\DateTime
================

[](#chovaldatetime)

This is a drop-in replacement for [PHP's DateTime](https://www.php.net/manual/en/class.datetime.php).
Some parsers have been added to ease the use, see Usage and Differences.

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

[](#installation)

```
composer require choval/datetime
```

Simple drop-in
--------------

[](#simple-drop-in)

Before

```
$a = new DateTime;
```

With it replaced

```
use Choval\DateTime;
$a = new DateTime;
```

The following examples will use the full class name to avoid confussion.

Usage
-----

[](#usage)

```
// Working with timestamps
$t = 946684800;     // 2000-01-01 00:00:00+00:00

// An int is interpretated as a timestamp
$a = new Choval\DateTime( $t );
echo $a->format('c');       // 2000-01-01T00:00:00+00:00
echo $a->getTimestamp();    // 946684800

// Using PHP's DateTime for the same task
$b = \DateTime::createFromFormat( 'U', $t );
echo $b->format('c');       // 2000-01-01T00:00:00+00:00
echo $b->getTimestamp();    // 946684800

// Still a drop-in ;-)
$c = Choval\DateTime::createFromFormat( 'U', $t );
echo $c->format('c');       // 2000-01-01T00:00:00+00:00
echo $c->getTimestamp();    // 946684800
```

### Differences

[](#differences)

If replacing PHP's DateTime, this class behaves almost exactly alike. With the exception of the constructor handling `int` as a timestamp even for low numbers.

```
// Flexible timezone parameter
// -3, '-03' , '-0300', '-03:00', 'America/Argentina/Buenos_Aires'
// or (new DateTimeZone('America/Argentina/Buenos_Aires'))
$d = new Choval\DateTime( '2000-01-01', '-0300');
echo $d->format('c');       // 2000-01-01T00:00:00-03:00
echo $d->getTimestamp();    // 946674000

// The constructor accepts a format as the third parameter
$e = new Choval\DateTime( '31/01/2000', '-3', 'd/m/Y' );
echo $e->format('c');       // 2000-01-31T00:00:00-03:00

// Similarly in PHP's DateTime
$f = \DateTime::createFromFormat( 'd/m/Y', '31/01/2000', (new DateTimeZone('America/Argentina/Buenos_Aires')) );

// Yet again, still a drop-in ;-)
$g = Choval\DateTime::createFromFormat( 'd/m/Y', '31/01/2000', (new DateTimeZone('America/Argentina/Buenos_Aires')) );

// Or ease it
$h = Choval\DateTime::createFromFormat( 'd/m/Y', '31/01/2000', '-03:00' );

// `add` and `sub` accept DateTime modifier formats, DateInterval objects or interval_spec (ie: P1Y for 1 year)
$e->add('+3 hours');
$e->add('PT3H');
$e->add(new DateInterval('PT3H'));
$e->modify('+3 hours');
echo $e->format('c');       // 2000-01-31T12:00:00-03:00
```

#### Constructor

[](#constructor)

The `__constructor` method allows a third parameter for passing the format of the first.

```
__construct(
	string $time='now',
	$timezone=NULL,
	string $format=NULL
	)
```

This allows passing custom format date time strings.

```
$a = new Choval\DateTime( '31/12/2019', '-3', 'd/m/Y' );
echo $a->format('c');	// 2019-12-31T00:00:00-03:00
```

#### Format

[](#format)

The `format` method allows a second paratemer for passing the timezone of the output, otherwise the timezone of the object is used.

The passed timezone will not change the one of the object itself. See the following example:

```
$a = new Choval\DateTime('2019-01-01 00:00:00', '-03:00');

echo $a->format('c');
// 2019-01-01T00:00:00-03:00

echo $a->format('c', '+08:00');
// 2019-01-01T11:00:00+08:00

echo $a->format('c', 'UTC');
// 2019-01-01T03:00:00+00:00

echo $a->format('c');
// 2019-01-01T00:00:00-03:00
// Notice how the original timezone is kept
```

### Extras

[](#extras)

The following methods were added to move around dates and printing/returning them as strings.

#### Time modifiers

[](#time-modifiers)

- startOfDay(void) : self
- midDay(void) : self
- endOfDay(void) : self

```
$a = new Choval\DateTime('2000-01-01 12:34:56', 'UTC');
echo $a->startOfDay()->format('H:i:s');	// 00:00:00
echo $a->midDay()->format('H:i:s');		// 12:00:00
echo $a->endOfDay()->format('H:i:s');	// 23:59:59

echo $a->format('c');			// 2000-01-01T23:59:59+00:00

$a->setTimezone('+01:00');
echo $a->format('H:i:s');					// 00:59:59
echo $a->format('c');			// 2000-01-02T00:59:59+01:00
echo $a->format('c', 'UTC');	// 2000-01-01T23:59:59+00:00

echo $a->endOfDay()->format('H:i:s');	// 23:59:59
echo $a->format('c');			// 2000-01-02T23:59:59+01:00
```

#### Date modifiers for the year

[](#date-modifiers-for-the-year)

- firstDayOfYear(\[int $year\]) : self
- lastDayOfYear(\[int $year\]) : self

```
$year = new Choval\DateTime;
$since = $year->firstDayOfYear()->startOfDay();
$till = $year->lastDayOfYear()->endOfDay();

// Useful for SQL
$sql = "SELECT * FROM users WHERE created >= ? AND created
