Date & Time
If you need TimeHelper
functionalities outside of a View
,use the FrozenTime
class:
- use Cake\I18n\FrozenTime;
- class UsersController extends AppController
- {
- public function initialize(): void
- {
- parent::initialize();
- $this->loadComponent('Auth');
- }
- public function afterLogin()
- {
- $time = new FrozenTime($this->Auth->user('date_of_birth'));
- if ($time->isToday()) {
- // Greet user with a happy birthday message
- $this->Flash->success(__('Happy birthday to you...'));
- }
- }
- }
Under the hood, CakePHP uses Chronosto power its FrozenTime
utility. Anything you can do with Chronos
andDateTime
, you can do with FrozenTime
and FrozenDate
.
For more details on Chronos please see the API documentation.
Creating Time Instances
There are a few ways to create FrozenTime
instances:
- use Cake\I18n\FrozenTime;
- // Create from a string datetime.
- $time = FrozenTime::createFromFormat(
- 'Y-m-d H:i:s',
- $datetime,
- 'America/New_York'
- );
- // Create from a timestamp
- $time = FrozenTime::createFromTimestamp($ts);
- // Get the current time.
- $time = FrozenTime::now();
- // Or just use 'new'
- $time = new FrozenTime('2014-01-10 11:11', 'America/New_York');
- $time = new FrozenTime('2 hours ago');
The FrozenTime
class constructor can take any parameter that the internal DateTimeImmutable
PHP class can. When passing a number or numeric string, it will be interpretedas a UNIX timestamp.
In test cases you can mock out now()
using setTestNow()
:
- // Fixate time.
- $now = new FrozenTime('2014-04-12 12:22:30');
- FrozenTime::setTestNow($now);
- // Returns '2014-04-12 12:22:30'
- $now = FrozenTime::now();
- // Returns '2014-04-12 12:22:30'
- $now = FrozenTime::parse('now');
Manipulation
Once created, you can manipulate FrozenTime
instances using setter methods:
- $now = FrozenTime::now();
- $now->year(2013)
- ->month(10)
- ->day(31);
You can also use the methods provided by PHP’s built-in DateTime
class:
- $now = $now->setDate(2013, 10, 31);
Dates can be modified through subtraction and addition of their components:
- $now = FrozenTime::now();
- $now = $now->subDays(5)
- ->addMonth(1);
- // Using strtotime strings.
- $now = $now->modify('+5 days');
You can get the internal components of a date by accessing its properties:
- $now = FrozenTime::now();
- echo $now->year; // 2014
- echo $now->month; // 5
- echo $now->day; // 10
- echo $now->timezone; // America/New_York
Formatting
This method sets the default format used when converting an object to json:
- Time::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss'); // For any mutable DateTime
- FrozenTime::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss'); // For any immutable DateTime
- Date::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss'); // For any mutable Date
- FrozenDate::setJsonEncodeFormat('yyyy-MM-dd HH:mm:ss'); // For any immutable Date
Note
This method must be called statically.
A very common thing to do with Time
instances is to print out formatteddates. CakePHP makes this a snap:
- $now = Time::parse('2014-10-31');
- // Prints a localized datetime stamp.
- echo $now;
- // Outputs '10/31/14, 12:00 AM' for the en-US locale
- $now->i18nFormat();
- // Use the full date and time format
- $now->i18nFormat(\IntlDateFormatter::FULL);
- // Use full date but short time format
- $now->i18nFormat([\IntlDateFormatter::FULL, \IntlDateFormatter::SHORT]);
- // Outputs '2014-10-31 00:00:00'
- $now->i18nFormat('yyyy-MM-dd HH:mm:ss');
It is possible to specify the desired format for the string to be displayed.You can either pass IntlDateFormatter constants as the firstargument of this function, or pass a full ICU date formatting string asspecified in the following resource:http://www.icu-project.org/apiref/icu4c/classSimpleDateFormat.html#details.
You can also format dates with non-gregorian calendars:
- // Outputs 'Friday, Aban 9, 1393 AP at 12:00:00 AM GMT'
- $result = $now->i18nFormat(\IntlDateFormatter::FULL, null, 'en-IR@calendar=persian');
The following calendar types are supported:
- japanese
- buddhist
- chinese
- persian
- indian
- islamic
- hebrew
- coptic
- ethiopic
Note
For constant strings i.e. IntlDateFormatter::FULL Intl uses ICU librarythat feeds its data from CLDR (http://cldr.unicode.org/) which versionmay vary depending on PHP installation and give different results.
Print out a predefined ‘nice’ format:
- $now = Time::parse('2014-10-31');
- // Outputs 'Oct 31, 2014 12:00 AM' in en-US
- echo $now->nice();
You can alter the timezone in which the date is displayed without altering theTime
object itself. This is useful when you store dates in one timezone, butwant to display them in a user’s own timezone:
- $now->i18nFormat(\IntlDateFormatter::FULL, 'Europe/Paris');
Leaving the first parameter as null
will use the default formatting string:
- $now->i18nFormat(null, 'Europe/Paris');
Finally, it is possible to use a different locale for displaying a date:
- echo $now->i18nFormat(\IntlDateFormatter::FULL, 'Europe/Paris', 'fr-FR');
- echo $now->nice('Europe/Paris', 'fr-FR');
Setting the Default Locale and Format String
The default locale in which dates are displayed when using nice
i18nFormat
is taken from the directiveintl.default_locale.You can, however, modify this default at runtime:
- Time::setDefaultLocale('es-ES'); // For any mutable DateTime
- FrozenTime::setDefaultLocale('es-ES'); // For any immutable DateTime
- Date::setDefaultLocale('es-ES'); // For any mutable Date
- FrozenDate::setDefaultLocale('es-ES'); // For any immutable Date
From now on, datetimes will be displayed in the Spanish preferred format unlessa different locale is specified directly in the formatting method.
Likewise, it is possible to alter the default formatting string to be used fori18nFormat
:
- Time::setToStringFormat(\IntlDateFormatter::SHORT); // For any mutable DateTime
- FrozenTime::setToStringFormat(\IntlDateFormatter::SHORT); // For any immutable DateTime
- Date::setToStringFormat(\IntlDateFormatter::SHORT); // For any mutable Date
- FrozenDate::setToStringFormat(\IntlDateFormatter::SHORT); // For any immutable Date
- // The same method exists on Date, FrozenDate and FrozenTime
- Time::setToStringFormat([
- \IntlDateFormatter::FULL,
- \IntlDateFormatter::SHORT
- ]);
- // The same method exists on Date, FrozenDate and FrozenTime
- Time::setToStringFormat('yyyy-MM-dd HH:mm:ss');
It is recommended to always use the constants instead of directly passing a dateformat string.
Formatting Relative Times
Often it is useful to print times relative to the present:
- $now = new Time('Aug 22, 2011');
- echo $now->timeAgoInWords(
- ['format' => 'MMM d, YYY', 'end' => '+1 year']
- );
- // On Nov 10th, 2011 this would display: 2 months, 2 weeks, 6 days ago
The end
option lets you define at which point after which relative timesshould be formatted using the format
option. The accuracy
option letsus control what level of detail should be used for each interval range:
- // If $timestamp is 1 month, 1 week, 5 days and 6 hours ago
- echo $timestamp->timeAgoInWords([
- 'accuracy' => ['month' => 'month'],
- 'end' => '1 year'
- ]);
- // Outputs '1 month ago'
By setting accuracy
to a string, you can specify what is the maximum levelof detail you want output:
- $time = new Time('+23 hours');
- // Outputs 'in about a day'
- $result = $time->timeAgoInWords([
- 'accuracy' => 'day'
- ]);
Conversion
Once created, you can convert Time
instances into timestamps or quartervalues:
- $time = new Time('2014-06-15');
- $time->toQuarter();
- $time->toUnixString();
Comparing With the Present
You can compare a Time
instance with the present in a variety of ways:
- $time = new Time('2014-06-15');
- echo $time->isYesterday();
- echo $time->isThisWeek();
- echo $time->isThisMonth();
- echo $time->isThisYear();
Each of the above methods will return true
/false
based on whether ornot the Time
instance matches the present.
Comparing With Intervals
You can see if a Time
instance falls within a given range usingwasWithinLast()
and isWithinNext()
:
- $time = new Time('2014-06-15');
- // Within 2 days.
- echo $time->isWithinNext(2);
- // Within 2 next weeks.
- echo $time->isWithinNext('2 weeks');
You can also compare a Time
instance within a range in the past:
- // Within past 2 days.
- echo $time->wasWithinLast(2);
- // Within past 2 weeks.
- echo $time->wasWithinLast('2 weeks');
Dates
The Date
class in CakePHP implements the same API and methods asCake\I18n\Time
does. The main difference between Time
andDate
is that Date
does not track time components, and is always in UTC.As an example:
- use Cake\I18n\Date;
- $date = new Date('2015-06-15');
- $date->modify('+2 hours');
- // Outputs 2015-06-15 00:00:00
- echo $date->format('Y-m-d H:i:s');
- $date->modify('+36 hours');
- // Outputs 2015-06-15 00:00:00
- echo $date->format('Y-m-d H:i:s');
Attempts to modify the timezone on a Date
instance are also ignored:
- use Cake\I18n\Date;
- $date = new Date('2015-06-15');
- $date->setTimezone(new \DateTimeZone('America/New_York'));
- // Outputs UTC
- echo $date->format('e');
Immutable Dates and Times
- class
Cake\I18n\
FrozenTime
CakePHP uses immutable date and time classes that implement the same interfaceas their mutable siblings. Immutable objects are useful when you want to preventaccidental changes to data, or when you want to avoid order based dependencyissues. Take the following code:
- use Cake\I18n\Time;
- $time = new Time('2015-06-15 08:23:45');
- $time->modify('+2 hours');
- // This method also modifies the $time instancegg/
- $this->someOtherFunction($time);
- // Output here is unknown.
- echo $time->format('Y-m-d H:i:s');
If the method call was re-ordered, or if someOtherFunction
changed theoutput could be unexpected. The mutability of our object creates temporalcoupling. If we were to use immutable objects, we could avoid this issue:
- use Cake\I18n\FrozenTime;
- $time = new FrozenTime('2015-06-15 08:23:45');
- $time = $time->modify('+2 hours');
- // This method's modifications don't change $time
- $this->someOtherFunction($time);
- // Output here is known.
- echo $time->format('Y-m-d H:i:s');
Immutable dates and times are useful in entities as they preventaccidental modifications, and force changes to be explicit. Usingimmutable objects helps the ORM to more easily track changes, and ensure thatdate and datetime columns are persisted correctly:
- // This change will be lost when the article is saved.
- $article->updated->modify('+1 hour');
- // By replacing the time object the property will be saved.
- $article->updated = $article->updated->modify('+1 hour');
Accepting Localized Request Data
When creating text inputs that manipulate dates, you’ll probably want to acceptand parse localized datetime strings. See the Parsing Localized Datetime Data.
Supported Timezones
CakePHP supports all valid PHP timezones. For a list of supported timezones, see this page.