Skip to content

Commit 8a0fe1c

Browse files
authored
Remove outdated info about Date and Time classes. (#7902)
1 parent 59e8aa5 commit 8a0fe1c

File tree

1 file changed

+17
-78
lines changed

1 file changed

+17
-78
lines changed

en/core-libraries/time.rst

Lines changed: 17 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,18 @@ use the ``DateTime`` class::
3131

3232
Under the hood, CakePHP uses `Chronos <https://github.com/cakephp/chronos>`_
3333
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``.
3535

3636
For more details on Chronos please see `the API documentation
37-
<https://api.cakephp.org/chronos/1.0/>`_.
37+
<https://api.cakephp.org/chronos/>`_.
3838

3939
.. start-time
4040
4141
Creating DateTime Instances
42-
=============================
42+
===========================
4343

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.
4746

4847
There are a few ways to create ``DateTime`` instances::
4948

@@ -407,85 +406,25 @@ You can also compare a ``DateTime`` instance within a range in the past::
407406
Date
408407
====
409408

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
448409
.. php:class:: Date
449410
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.
468413

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::
475415

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()``.
478419

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+
====
483422

484-
// This change will be lost when the article is saved.
485-
$article->updated->modify('+1 hour');
423+
.. php:class:: Time
486424
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.
489428

490429
Accepting Localized Request Data
491430
================================

0 commit comments

Comments
 (0)