@@ -31,19 +31,18 @@ use the ``DateTime`` class::
31
31
32
32
Under the hood, CakePHP uses `Chronos <https://github.com/cakephp/chronos >`_
33
33
to power its ``DateTime `` utility. Anything you can do with ``Chronos `` and
34
- PHP's ``DateTime ``, you can do with ``DateTime `` and `` Date ``.
34
+ PHP's ``DateTimeImmutable ``, you can do with ``DateTime ``.
35
35
36
36
For more details on Chronos please see `the API documentation
37
- <https://api.cakephp.org/chronos/1.0/ > `_.
37
+ <https://api.cakephp.org/chronos/> `_.
38
38
39
39
.. start-time
40
40
41
41
Creating DateTime Instances
42
- =============================
42
+ ===========================
43
43
44
- ``DateTime `` are immutable objects that are useful when you want to prevent
45
- accidental changes to data, or when you want to avoid order based dependency
46
- issues.
44
+ ``DateTime `` are immutable objects as immutability prevents accidental changes
45
+ to data, and avoids order based dependency issues.
47
46
48
47
There are a few ways to create ``DateTime `` instances::
49
48
@@ -407,85 +406,25 @@ You can also compare a ``DateTime`` instance within a range in the past::
407
406
Date
408
407
====
409
408
410
- .. php:class: Date
411
-
412
- The immutable ``Date `` class in CakePHP implements a similar API and methods as
413
- :php:class: `Cake\\ I18n\\ DateTime ` does. The main difference between ``DateTime ``
414
- and ``Date `` is that ``Date `` does not track time components. As an example::
415
-
416
- use Cake\I18n\Date;
417
-
418
- $date = new Date('2021-01-31');
419
-
420
- $newDate = $date->modify('+2 hours');
421
- // Outputs '2021-01-31 00:00:00'
422
- echo $newDate->format('Y-m-d H:i:s');
423
-
424
- $newDate = $date->addHours(36);
425
- // Outputs '2021-01-31 00:00:00'
426
- echo $newDate->format('Y-m-d H:i:s');
427
-
428
- $newDate = $date->addDays(10);
429
- // Outputs '2021-02-10 00:00:00'
430
- echo $newDate->format('Y-m-d H:i:s');
431
-
432
-
433
- Attempts to modify the timezone on a ``Date `` instance are also ignored::
434
-
435
- use Cake\I18n\Date;
436
- $date = new Date('2021-01-31', new \DateTimeZone('America/New_York'));
437
- $newDate = $date->setTimezone(new \DateTimeZone('Europe/Berlin'));
438
-
439
- // Outputs 'America/New_York'
440
- echo $newDate->format('e');
441
-
442
- .. _mutable-time :
443
-
444
- Mutable Dates and Times
445
- =======================
446
-
447
- .. php :class :: Time
448
409
.. php :class :: Date
449
410
450
- CakePHP uses mutable date and time classes that implement the same interface
451
- as their immutable siblings. Immutable objects are useful when you want to prevent
452
- accidental changes to data, or when you want to avoid order based dependency
453
- issues. Take the following code::
454
-
455
- use Cake\I18n\Time;
456
- $time = new Time('2015-06-15 08:23:45');
457
- $time->modify('+2 hours');
458
-
459
- // This method also modifies the $time instance
460
- $this->someOtherFunction($time);
461
-
462
- // Output here is unknown.
463
- echo $time->format('Y-m-d H:i:s');
464
-
465
- If the method call was re-ordered, or if ``someOtherFunction `` changed the
466
- output could be unexpected. The mutability of our object creates temporal
467
- coupling. If we were to use immutable objects, we could avoid this issue::
411
+ The immutable ``Date `` class in CakePHP represents calendar dates unaffected by
412
+ time and timezones. The ``Date `` class wraps the ``Cake\\Chronos\\ChronosDate `` class.
468
413
469
- use Cake\I18n\DateTime;
470
- $time = new DateTime('2015-06-15 08:23:45');
471
- $time = $time->modify('+2 hours');
472
-
473
- // This method's modifications don't change $time
474
- $this->someOtherFunction($time);
414
+ .. note ::
475
415
476
- // Output here is known.
477
- echo $time->format('Y-m-d H:i:s');
416
+ Unlike the ``DateTime `` class, ``Date `` does not extends the ``DateTimeInterface ``.
417
+ So you cannot cannot directly compare a ``Date `` instance with a ``DateTime `` instance.
418
+ But you can do comparisons like ``$dateTime->toNative() > $date->toNative() ``.
478
419
479
- Immutable dates and times are useful in entities as they prevent
480
- accidental modifications, and force changes to be explicit. Using
481
- immutable objects helps the ORM to more easily track changes, and ensure that
482
- date and datetime columns are persisted correctly::
420
+ Time
421
+ ====
483
422
484
- // This change will be lost when the article is saved.
485
- $article->updated->modify('+1 hour');
423
+ .. php :class :: Time
486
424
487
- // By replacing the time object the property will be saved.
488
- $article->updated = $article->updated->modify('+1 hour');
425
+ The ``Time `` class represents clock times independent of date or time zones
426
+ Similar to the ``DateTime `` and ```Date `` classes, the ``Time `` class is also immutable.
427
+ It wraps the ``Cake\\Chronos\\ChronosTime `` class.
489
428
490
429
Accepting Localized Request Data
491
430
================================
0 commit comments