# TimeHelper

`class` **TimeHelper**(View $view, array $settings = [])

The Time Helper does what it says on the tin: saves you time. It
allows for the quick processing of time related information. The
Time Helper has two main tasks that it can perform:

1.  It can format time strings.
2.  It can test time (but cannot bend time, sorry).

::: info Changed in version 2.1
`TimeHelper` has been refactored into the `CakeTime` class to allow easier use outside of the `View` layer. Within a view, these methods are accessible via the TimeHelper class and you can call it as you would call a normal helper method: `$this->Time->method($args);`.
:::

## Using the Helper

A common use of the Time Helper is to offset the date and time to match a
user's time zone. Lets use a forum as an example. Your forum has many users who
may post messages at any time from any part of the world. An easy way to
manage the time is to save all dates and times as GMT+0 or UTC. Uncomment the
line `date_default_timezone_set('UTC');` in `config/core.php` to ensure
your application's time zone is set to GMT+0.

Next add a time zone field to your users table and make the necessary
modifications to allow your users to set their time zone. Now that we know
the time zone of the logged in user we can correct the date and time on our
posts using the Time Helper:

```php
echo $this->Time->format(
    'F jS, Y h:i A',
    $post['Post']['created'],
    null,
    $user['User']['time_zone'],
);
// Will display August 22nd, 2011 11:53 PM for a user in GMT+0
// August 22nd, 2011 03:53 PM for a user in GMT-8
// and August 23rd, 2011 09:53 AM GMT+10
```

Most of the Time Helper methods have a \$timezone parameter. The \$timezone parameter
accepts a valid timezone identifier string or an instance of `DateTimeZone` class.

<!-- start-caketime -->

## Formatting

Methods that accept `strftime()` style formats, such as `i18nFormat()` and
`nice()`, format dates with `IntlDateFormatter` through the
`php81_bc/strftime` library, because PHP's own `strftime()` is deprecated
since PHP 8.1. Install the `intl` extension to get localized month and day
names; without it the Symfony polyfill is used, which only supports English.

::: pieceofcake2
`strftime()` was replaced by an
`IntlDateFormatter` based implementation. Most formats produce the same output,
but edge cases may differ, so test your date formatting.
:::

`method` CakeTime::**convert**($serverTime, $timezone = NULL)

`method` CakeTime::**convertSpecifiers**($format, $time = NULL)

`method` CakeTime::**dayAsSql**($dateString, $field_name, $timezone = NULL)

`method` CakeTime::**daysAsSql**($begin, $end, $fieldName, $timezone = NULL)

`method` CakeTime::**format**($date, $format = NULL, $default = false, $timezone = NULL)

`method` CakeTime::**fromString**($dateString, $timezone = NULL)

`method` CakeTime::**gmt**($dateString = NULL)

`method` CakeTime::**i18nFormat**($date, $format = NULL, $invalid = false, $timezone = NULL)

`method` CakeTime::**nice**($dateString = NULL, $timezone = NULL, $format = null)

`method` CakeTime::**niceShort**($dateString = NULL, $timezone = NULL)

`method` CakeTime::**serverOffset**()

`method` CakeTime::**timeAgoInWords**($dateString, $options = [])

`method` CakeTime::**toAtom**($dateString, $timezone = NULL)

`method` CakeTime::**toQuarter**($dateString, $range = false)

`method` CakeTime::**toRSS**($dateString, $timezone = NULL)

`method` CakeTime::**toUnix**($dateString, $timezone = NULL)

`method` CakeTime::**toServer**($dateString, $timezone = NULL, $format = 'Y-m-d H:i:s')

`method` CakeTime::**timezone**($timezone = NULL)

`method` CakeTime::**listTimezones**($filter = null, $country = null, $options = [])

## Testing Time

`method` CakeTime::**isToday**($dateString, $timezone = NULL)

`method` CakeTime::**isThisWeek**($dateString, $timezone = NULL)

`method` CakeTime::**isThisMonth**($dateString, $timezone = NULL)

`method` CakeTime::**isThisYear**($dateString, $timezone = NULL)

`method` CakeTime::**wasYesterday**($dateString, $timezone = NULL)

`method` CakeTime::**isTomorrow**($dateString, $timezone = NULL)

`method` CakeTime::**isFuture**($dateString, $timezone = NULL)

`method` CakeTime::**isPast**($dateString, $timezone = NULL)

`method` CakeTime::**wasWithinLast**($timeInterval, $dateString, $timezone = NULL)

<!-- end-caketime -->
