Skip to content

Abstract Class DateHelper

Constants

EVENT_DEFINE_TIMEZONE_LABEL

See DefineTimezoneLabelEvent

Methods

systemTimezone

Returns the system timezone.

Unlike Craft::$app->getTimeZone(), this isn't the user's preferred timezone in control panel requests.

public static systemTimezone(): string
return string

setTime

Returns a new DateTime object with the time set to the given time.

public static setTime(DateTimeInterface $date, DateTimeInterface $time): DateTime
$date DateTimeInterface
$time DateTimeInterface
return DateTime

hasFixedOffset

Returns whether a date is in a fixed offset timezone (e.g. +02:00 or EST), rather than a timezone identifier (e.g. America/Toronto or UTC).

public static hasFixedOffset(DateTimeInterface $date): bool
$date DateTimeInterface
return bool

endOfDay

Returns a new DateTime object with the time set to the end of the day.

public static endOfDay(DateTimeInterface $date): DateTime
$date DateTimeInterface
return DateTime

hasSameOffsets

Returns whether two timezones have the same offsets from a year before to a year after a date, so they display the same times (e.g. Toronto and New York, but not Phoenix and Denver, since Phoenix doesn't observe DST).

public static hasSameOffsets(string $timezone, string $otherTimezone, DateTimeInterface $date): bool
$timezone string
$otherTimezone string
$date DateTimeInterface
return bool

formatDate

Formats a date (without a time).

public static formatDate(DateTimeInterface $startDate, FormatLength|string $format = \boundstate\eventful\enums\FormatLength::MEDIUM, ?string $timezone = null): string
$startDate DateTimeInterface
$format FormatLength|string A length or its value (e.g. 'long')
$timezone ?string
return string
throws InvalidArgumentException if the format isn't valid

formatDateRange

Formats a range of dates (without times).

public static formatDateRange(DateTimeInterface $startDate, ?DateTimeInterface $endDate, FormatLength|string $format = \boundstate\eventful\enums\FormatLength::MEDIUM, ?string $timezone = null): string
$startDate DateTimeInterface
$endDate ?DateTimeInterface
$format FormatLength|string A length or its value (e.g. 'long')
$timezone ?string
return string
throws InvalidArgumentException if the format isn't valid

formatDatetimeRange

Formats a range of dates and times, or only the dates or the times, depending on the format.

public static formatDatetimeRange(DateTimeInterface $startDate, DateTimeInterface $endDate, DateFormat|string $format = \boundstate\eventful\enums\DateFormat::MEDIUM, ?string $timezone = null, bool $displayTimezone = false): string
$startDate DateTimeInterface
$endDate DateTimeInterface
$format DateFormat|string
$timezone ?string
$displayTimezone bool
return string
throws InvalidArgumentException if the format isn't valid

formatTimeRange

Formats a range of times (without dates).

public static formatTimeRange(DateTimeInterface $startDate, DateTimeInterface $endDate, FormatLength|string $format = \boundstate\eventful\enums\FormatLength::MEDIUM, ?string $timezone = null, bool $displayTimezone = false): string
$startDate DateTimeInterface
$endDate DateTimeInterface
$format FormatLength|string A length or its value (e.g. 'long')
$timezone ?string
$displayTimezone bool
return string
throws InvalidArgumentException if the format isn't valid

timezoneOptions

Returns the options for a timezone input, sorted by location (e.g. "Toronto, Canada", with the city on its own), with the timezone's label on the given date as a hint (e.g. "EST"), or its offset if it doesn't have one (e.g. "GMT+1"), since they can change during the year.

The label is the one added to formatted times, so it can be overridden in the same way. The options can also be searched by their generic name (e.g. "Eastern Time"), identifier and offset.

public static timezoneOptions(?DateTimeInterface $date = null): array{value: string, label: string, data: array{city: string, hint: string, keywords: string}}[]
$date ?DateTimeInterface
return array{value: string, label: string, data: array{city: string, hint: string, keywords: string}}[]