Date Class
Date Class
The Date class provides a suite of utilities for handling timezones, date formatting, localized date strings, and relative timestamps. It ensures that dates are consistently displayed according to the site's global configuration.
⚙️ Configuration & Properties
The class initializes its temporal settings based on the system options during boot:
Date::$timezone
- Type:
string - Description: The default timezone (e.g.,
'Asia/Jakarta'). Falls back to'UTC'if unconfigured. This is set globally viadate_default_timezone_set()inside the constructor.
🛠️ Public Methods Reference
Date::format(string $date, string $format = '')
Formats a date string into a specific pattern, adjusted to the site's configured $timezone.
| Parameter | Type | Default | Description |
|---|---|---|---|
$date |
string |
✅ | Any valid PHP date string (e.g., from DB). If empty, evaluates to current time. |
$format |
string |
'j F Y H:i A T' |
Standard PHP date() format pattern (e.g., d/m/Y). |
Example:
echo Date::format('2024-03-29 10:00:00', 'd/m/Y');
// Outputs: 29/03/2024 (adjusted to site timezone)
echo Date::format('2024-03-29 10:00:00');
// Outputs: 29 March 2024 10:00 AM WIB
Date::local(string $date, string $format = '')
Returns a localized date string using the PHP IntlDateFormatter extension, respecting the site's country_id locale.
Date::format, this method uses ICU Date Field Symbols, not standard PHP date strings. Also, this method automatically appends the timezone abbreviation (e.g., "WIB", "EST") to the end of the returned string.- Note: Requires the
intlPHP extension to be enabled on your server. - Format System: ICU Date Field Symbol Table.
- Default format:
'MMMM d, Y H:m:s'
Example:
// If Options::v('country_id') is set to 'id_ID' (Indonesian Locale)
echo Date::local('2024-03-29', 'EEEE, d MMMM Y');
// Outputs: Jumat, 29 Maret 2024 WIB
Date::rel(string $ptime)
Returns a human-readable relative time string comparing the target timestamp to the current time.
echo Date::rel('2024-03-29 08:00:00');
// Outputs: 5 hours ago
Supported conversions: seconds, minutes, hours, days, weeks, months, and years.
Date::monthName(int $month)
Returns the full English name of a month given its numeric value (1-12).
echo Date::monthName(3);
// Outputs: March
Date::timeZone()
Returns an associative array of all available world timezones filtered to major geographic regions (America, Asia, Europe, Pacific, etc.). It groups closely related identifiers into a comma-separated string for concise UI rendering.
Date::optTimeZone(string $val = '')
Generates a raw string of HTML <option> tags containing the list of all world timezones.
| Parameter | Type | Description |
|---|---|---|
$val |
string |
The timezone string to be marked as SELECTED (e.g. 'Asia/Jakarta'). |
Date::countryList()
Returns an exhaustive associative array mapping ISO 3166-1 alpha-2 country codes to their full English names.
(Example: ['ID' => 'Indonesia', 'US' => 'United States'])
Date::optCountry(string $val = '')
Generates HTML <option> tags for all countries in the countryList().
echo '<select name="country" class="form-control">' . Date::optCountry('ID') . '</select>';
See Also
- Options Class — Where
timezoneandcountry_idpreferences are saved natively. - PHP DateTime Class — Underlying engine for the
Dateclass. - PHP IntlDateFormatter — Engine required for
Date::local().