cakephp / chronos
A simple API extension for DateTime.
Installs: 38 810 836
Dependents: 121
Suggesters: 4
Security: 0
Stars: 1 355
Watchers: 44
Forks: 63
Open Issues: 3
Requires
- php: >=8.1
- psr/clock: ^1.0
Requires (Dev)
- cakephp/cakephp-codesniffer: ^5.0
- phpunit/phpunit: ^10.1.0 || ^11.1.3
Provides
- 3.x-dev
- 3.1.0
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 3.0.0-beta3
- 3.0.0-beta2
- 3.0.0-beta1
- 2.x-dev
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.4.0-RC2
- 2.4.0-RC1
- 2.3.3
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.3.0
- 1.2.8
- 1.2.7
- 1.2.6
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.4.11
- 0.4.10
- 0.4.9
- 0.4.8
- 0.4.7
- 0.4.6
- 0.4.5
- 0.4.4
- 0.4.3
- 0.4.2
- 0.4.1
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- dev-3.next
- dev-2.x-interval
This package is auto-updated.
Last update: 2024-10-20 02:58:26 UTC
README
Chronos focuses on providing immutable date/datetime objects. Immutable objects help ensure that datetime objects aren't accidentally modified keeping data more predictable.
Installation
Installing with composer:
$ composer require cakephp/chronos
You can then use Chronos:
<?php require 'vendor/autoload.php'; use Cake\Chronos\Chronos; printf("Now: %s", Chronos::now());
Differences with nesbot/carbon
Chronos was originally compatible with Carbon but has diverged and no longer extends the PHP DateTime and DateTimeImmutable classes.
Immutable Object Changes
Immutable objects have a number of advantages:
- Using immutable objects is always free of side-effects.
- Dates and times don't accidentally change underneath other parts of your code.
With those benefits in mind, there are a few things you need to keep in mind when modifying immutable objects:
// This will lose modifications $date = new Chronos('2015-10-21 16:29:00'); $date->modify('+2 hours'); // This will keep modifications $date = new Chronos('2015-10-21 16:29:00'); $date = $date->modify('+2 hours');
Calendar Dates
PHP only offers datetime objects as part of the native extensions. Chronos adds
a number of conveniences to the traditional DateTime object and introduces
a ChronosDate
object. ChronosDate
instances their time frozen to 00:00:00
and the timezone
set to the server default timezone. This makes them ideal when working with
calendar dates as the time components will always match.
use Cake\Chronos\ChronosDate; $today = new ChronosDate(); echo $today; // Outputs '2015-10-21' echo $today->modify('+3 hours'); // Outputs '2015-10-21'
Like instances of Chronos
, ChronosDate
objects are also immutable.
Documentation
A more descriptive documentation can be found at book.cakephp.org/chronos/3/en/.
API Documentation
API documentation can be found on api.cakephp.org/chronos.