From 71657081141366a5981b2e05dac35b216464104e Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Tue, 4 Nov 2025 18:12:37 +0100 Subject: [PATCH 01/15] Added (Helper) Classes section and implemented (copied) the content from the old manual about JDate. --- docs/general-concepts/classes/date.md | 208 +++++++++++++++++++++++++ docs/general-concepts/classes/index.md | 8 + 2 files changed, 216 insertions(+) create mode 100644 docs/general-concepts/classes/date.md create mode 100644 docs/general-concepts/classes/index.md diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md new file mode 100644 index 00000000..e99735c1 --- /dev/null +++ b/docs/general-concepts/classes/date.md @@ -0,0 +1,208 @@ +--- +title: Date +--- + +Introduction +============ + +Joomla's Date class is a helper class, extended from PHP's DateTime class, which allows developers to handle date +formatting more efficiently. The class allows developers to format dates for readable strings, MySQL interaction, UNIX +timestamp calculation, and also provides helper methods for working in different time zones. + +## Creating a Date Instance + +All the date helper methods require an instance of the Date class. To begin, you must create one. A Date object may +be created in two ways. One is the typical native method of simply creating a new instance: + +```php +use Joomla\CMS\Date\Date; + +$date = new Date(); // Creates a new Date object equal to the current time. +``` + +You may also create an instance using the static method defined in Date: + +```php +use Joomla\CMS\Date\Date; + +$date = Date::getInstance(); // Alias of 'new Date();' +``` + +There is no difference between these methods, as Date::getInstance simply creates a new instance of Date exactly like +the first method shown. Alternatively, you may also retrieve the current date (as a Date object) from the Application +object by using: + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate(); +``` + +## Arguments + +The Date constructor (and getInstance static method) accepts two optional parameters: A date string to format and a +timezone. Not passing a date string will create a Date object with the current date and time, while not passing a +timezone will allow the Date object to use the default timezone set. The first argument, if used, should be a string +that can be parsed using PHP's native DateTime constructor. For example: + +```php +use Joomla\CMS\Date\Date; + +$currentTime = new Date('now'); // Current date and time +$tomorrowTime = new Date('now +1 day'); // Current date and time, + 1 day. +$plus1MonthTime = new Date('now +1 month'); // Current date and time, + 1 month. +$plus1YearTime = new Date('now +1 year'); // Current date and time, + 1 year. +$plus1YearAnd1MonthTime = new Date('now +1 year +1 month'); // Current date and time, + 1 year and 1 month. +$plusTimeToTime = new Date('now +1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$plusTimeToTime = new Date('now -1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$combinedTimeToTime = new Date('now -1 hour -30 minutes 23 seconds'); // Current date and time, - 1 hour, +30 minutes and +23 seconds + +$date = new Date('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 +``` + +A Unix timestamp (in seconds) can also be passed as the first argument, in which case it will be internally uplifted +into a date. If a timezone has been specified as the second argument to the constructor, it will be converted to that +timezone. + +## Outputting Dates + +One note of caution when outputting Date objects in a user context: do not simply print them to the screen. The Date +object's toString() method simply calls its parent's format() method, without consideration for time zones or localized +date formatting. This will not result in a good user experience and will lead to inconsistencies between the formatting +in your extension, and elsewhere in Joomla. Instead, you should always output Dates using the methods shown below. + +### Common Date Formats + +A number of date formats are predefined in Joomla as part of the base language packs. This is beneficial because it +means date formats can be easily internationalised. A sample of the available format strings is below, from the en-GB +language pack. It is highly recommended to utilise these formatting strings when outputting dates so that your dates +will be automatically re-formatted according to a user's locale. They can be retrieved in the same way as any language +string. + +```ini +DATE_FORMAT_LC = "l, d F Y" +DATE_FORMAT_LC1 = "l, d F Y" +DATE_FORMAT_LC2 = "l, d F Y H:i" +DATE_FORMAT_LC3 = "d F Y" +DATE_FORMAT_LC4 = "Y-m-d" +DATE_FORMAT_LC5 = "Y-m-d H:i" +DATE_FORMAT_LC6 = "Y-m-d H:i:s" +DATE_FORMAT_JS1 = "y-m-d" +DATE_FORMAT_CALENDAR_DATE = "%Y-%m-%d" +DATE_FORMAT_CALENDAR_DATETIME = "%Y-%m-%d %H:%M:%S" +DATE_FORMAT_FILTER_DATE = "Y-m-d" +DATE_FORMAT_FILTER_DATETIME = "Y-m-d H:i:s" +``` + +### Using HtmlHelper Method (Recommended) + +As with many common output items, the HtmlHelper class is here to... help! HtmlHelper's date() method will take any +date-time string that the Date constructor would accept, along with a formatting string, and output the date appropriate +for the current user's timezone settings. As such, this is the recommended method for outputting dates for the user. + +```php +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + +$myDateString = '2025-12-1 15:20:00'; +echo HtmlHelper::date($myDateString, Text::_('DATE_FORMAT_FILTER_DATETIME')); +``` + +### Using Date::format() Method + +Another option is to format the Date manually. If this method is used, you will have to also manually retrieve and set +the user's timezone. This method is more useful for formatting dates outside the user interface, such as in system +logs or API calls. + +```php +use Joomla\CMS\Language\Text; +use Joomla\CMS\Date\Date; +use Joomla\CMS\Factory; + +$myDateString = '2025-12-1 15:20:00'; +$timezone = Factory::getUser()->getTimezone(); + +$date = new Date($myDateString); +$date->setTimezone($timezone); +echo $date->format(Text::_('DATE_FORMAT_FILTER_DATETIME')); +``` + +## Other Useful Code Examples + +### Quickly Outputting the Current Time + +There are two easy ways of doing this. + +1. The HtmlHelper's date() method, if no date value is provided, will default to the current time. +2. Factory::getDate() gets the current date as a Date object, which we can then format. + +```php +use Joomla\CMS\Factory; +use Joomla\CMS\HTML\HTMLHelper; +use Joomla\CMS\Language\Text; + +// These two are functionally equivalent +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_FILTER_DATETIME')); + +$timezone = Factory::getUser()->getTimezone(); +echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_FILTER_DATETIME')); +``` + +### Adding and Subtracting from Dates + +Because the Joomla Date object extends the PHP DateTime object, it provides methods for adding and subtracting from +dates. The easiest of these methods to use is the modify() method, which accepts any relative modification string that +the PHP [strtotime()](https://www.php.net/manual/en/function.strtotime.php) method would accept. For example: + +```php +use Joomla\CMS\Date\Date; + +$date = new Date('2025-12-1 15:20:00'); +$date->modify('+1 year'); +echo $date->toSQL(); // 2026-12-01 15:20:00 +``` + +There are also separate add() and sub() methods, for adding or subtracting time from a date object respectively. These +accept PHP-standard DateInterval objects: + +```php +use Joomla\CMS\Date\Date; + +$interval = new \DateInterval('P1Y1D'); // Interval represents 1 year and 1 day + +$date1 = new Date('2025-12-1 15:20:00'); +$date1->add($interval); +echo $date1->toSQL(); // 2026-12-02 15:20:00 + +$date2 = new Date('2025-12-1 15:20:00'); +$date2->sub($interval); +echo $date2->toSQL(); // 2024-11-30 15:20:00 +``` + +### Outputting Dates in ISO 8601 Format + +```php +$date = new Date('2025-12-1 15:20:00'); +$date->toISO8601(); // 20251201T152000Z +``` + +### Outputting Dates in RFC 822 Format + +```php +$date = new Date('2025-12-1 15:20:00'); +$date->toRFC822(); // Sat, 01 Dec 2025 15:20:00 +0000 +``` + +### Outputting Dates in SQL Date-Time Format + +```php +$date = new Date('20251201T152000Z'); +$date->toSQL(); // 2025-12-01 15:20:00 +``` + +### Outputting Dates as Unix Timestamps + +```php +$date = new Date('20251201T153000Z'); +$date->toUnix(); // 1764599400 +``` \ No newline at end of file diff --git a/docs/general-concepts/classes/index.md b/docs/general-concepts/classes/index.md new file mode 100644 index 00000000..a9fab357 --- /dev/null +++ b/docs/general-concepts/classes/index.md @@ -0,0 +1,8 @@ +--- +title: Classes +--- + +Classes +======= + +This section provides an overview of the available (helper) classes in Joomla. \ No newline at end of file From 22b5533085c1dffaa3438796a9facbcdc97c84c4 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 06:44:28 +0100 Subject: [PATCH 02/15] Marked static Date::getInstance() method as legacy --- docs/general-concepts/classes/date.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index e99735c1..16a09907 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -23,11 +23,19 @@ $date = new Date(); // Creates a new Date object equal to the current time. You may also create an instance using the static method defined in Date: ```php +// Legacy use Joomla\CMS\Date\Date; $date = Date::getInstance(); // Alias of 'new Date();' ``` +:::caution Legacy + +The static method is deprecated and will be removed in a future release. + +::: + + There is no difference between these methods, as Date::getInstance simply creates a new instance of Date exactly like the first method shown. Alternatively, you may also retrieve the current date (as a Date object) from the Application object by using: From 91eafbfafac3c1768d87968d742a1fc1d8dc6ec0 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 10:34:49 +0100 Subject: [PATCH 03/15] Using timezones explained --- docs/general-concepts/classes/date.md | 260 +++++++++++++++++++++----- 1 file changed, 214 insertions(+), 46 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 16a09907..3677ad87 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -12,7 +12,11 @@ timestamp calculation, and also provides helper methods for working in different ## Creating a Date Instance All the date helper methods require an instance of the Date class. To begin, you must create one. A Date object may -be created in two ways. One is the typical native method of simply creating a new instance: +be created in three ways. + +### Native Method + +The native method is the most straightforward and is shown below: ```php use Joomla\CMS\Date\Date; @@ -20,7 +24,20 @@ use Joomla\CMS\Date\Date; $date = new Date(); // Creates a new Date object equal to the current time. ``` -You may also create an instance using the static method defined in Date: +### Factory::getDate() Method + +There is no difference between these methods, as Date::getDate simply creates a new instance of Date exactly like +the first method shown. + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate(); +``` + +### Static Method (Legacy) + +You may also create an instance using the legacy static method defined in Date: ```php // Legacy @@ -29,29 +46,64 @@ use Joomla\CMS\Date\Date; $date = Date::getInstance(); // Alias of 'new Date();' ``` -:::caution Legacy +:::info Legacy -The static method is deprecated and will be removed in a future release. +The static method is only still available for backward compatibility and should no longer be used in new projects. ::: +## Using Timezone + +If no time zone has been defined, the Date class uses the UTC time zone. + +### Using the timezone from Global Configuration + +To use the configured time zone of the global configuration, you can use the following (recommended) code: + +```php +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + +// Get Timezone from Global Configuration +$config = Factory::getApplication()->getConfig(); +$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' -There is no difference between these methods, as Date::getInstance simply creates a new instance of Date exactly like -the first method shown. Alternatively, you may also retrieve the current date (as a Date object) from the Application -object by using: +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); +``` + +Or using the Factory::getDate() method, setting the timezone manually and formatting the date: ```php + use Joomla\CMS\Factory; -$date = Factory::getDate(); +// Get Timezone from Global Configuration +$config = Factory::getApplication()->getConfig(); +$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' + +// Catch a potential error and set to UTC as fallback. +try +{ + $siteTimezone = new DateTimeZone($timezoneString); +} +catch (DateInvalidTimeZoneException $e) +{ + $siteTimezone = new DateTimeZone('UTC'); +} + +echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); + ``` + ## Arguments -The Date constructor (and getInstance static method) accepts two optional parameters: A date string to format and a +The Date constructor accepts two optional parameters: A date string to format and a timezone. Not passing a date string will create a Date object with the current date and time, while not passing a timezone will allow the Date object to use the default timezone set. The first argument, if used, should be a string -that can be parsed using PHP's native DateTime constructor. For example: +that can be parsed using PHP's native DateTime constructor. + +### Using the native Method with the default timezone ```php use Joomla\CMS\Date\Date; @@ -66,11 +118,78 @@ $plusTimeToTime = new Date('now -1 hour +30 minutes +3 seconds'); // Current dat $combinedTimeToTime = new Date('now -1 hour -30 minutes 23 seconds'); // Current date and time, - 1 hour, +30 minutes and +23 seconds $date = new Date('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 +$dateFromTimestamp = new Date(1764599400); // 3:30 PM, December 1st, 2025 +``` + +### Using the native Method with a custom timezone + +```php +use Joomla\CMS\Date\Date; + +$tz = new DateTimeZone('Australia/Melbourne'); + +$currentTime = new Date('now', $tz); // Current date and time +$tomorrowTime = new Date('now +1 day', $tz); // Current date and time, + 1 day. +$plus1MonthTime = new Date('now +1 month', $tz); // Current date and time, + 1 month. +$plus1YearTime = new Date('now +1 year', $tz); // Current date and time, + 1 year. +$plus1YearAnd1MonthTime = new Date('now +1 year +1 month', $tz); // Current date and time, + 1 year and 1 month. +$plusTimeToTime = new Date('now +1 hour +30 minutes +3 seconds', $tz); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$plusTimeToTime = new Date('now -1 hour +30 minutes +3 seconds', $tz); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$combinedTimeToTime = new Date('now -1 hour -30 minutes 23 seconds', $tz); // Current date and time, - 1 hour, +30 minutes and +23 seconds + +$date = new Date('2025-12-1 15:20:00', $tz); // 3:20 PM, December 1st, 2025 +$dateFromTimestamp = new Date(1764599400, $tz); // 3:30 PM, December 1st, 2025 +``` + +:::note + +See Outputting Dates for more information on time zones. + +::: + +### Using the Factory Method with the default timezone + +```php +use Joomla\CMS\Factory; + +$currentTime = Factory::getDate('now'); // Current date and time +$tomorrowTime = Factory::getDate('now +1 day'); // Current date and time, + 1 day. +$plus1MonthTime = Factory::getDate('now +1 month'); // Current date and time, + 1 month. +$plus1YearTime = Factory::getDate('now +1 year'); // Current date and time, + 1 year. +$plus1YearAnd1MonthTime = Factory::getDate('now +1 year +1 month'); // Current date and time, + 1 year and 1 month. +$plusTimeToTime = Factory::getDate('now +1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$plusTimeToTime = Factory::getDate('now -1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds +$combinedTimeToTime = Factory::getDate('now -1 hour -30 minutes 23 seconds'); // Current date and time, - 1 hour, +30 minutes and +23 seconds + +$date = Factory::getDate('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 +$dateFromTimestamp = Factory::getDate(1764599400); // 3:30 PM, December 1st, 2025 +``` + +### Using the Factory Method with a custom timezone + +```php +use Joomla\CMS\Factory; + +$tz = new DateTimeZone('Australia/Melbourne'); + +$currentTime = Factory::getDate('now', $tz); // Current date and time in Melbourne. +$tomorrowTime = Factory::getDate('now +1 day', $tz); // Current date and time in Melbourne, + 1 day. +$plus1MonthTime = Factory::getDate('now +1 month', $tz); // Current date and time in Melbourne, + 1 month. +$plus1YearTime = Factory::getDate('now +1 year', $tz); // Current date and time in Melbourne, + 1 year. +$plus1YearAnd1MonthTime = Factory::getDate('now +1 year +1 month', $tz); // Current date and time in Melbourne, + 1 year and 1 month. +$plusTimeToTime = Factory::getDate('now +1 hour +30 minutes +3 seconds', $tz); // Current date and time in Melbourne, + 1 hour, 30 minutes and 3 seconds +$plusTimeToTime = Factory::getDate('now -1 hour +30 minutes +3 seconds', $tz); // Current date and time in Melbourne, + 1 hour, 30 minutes and 3 seconds +$combinedTimeToTime = Factory::getDate('now -1 hour -30 minutes 23 seconds', $tz); // Current date and time in Melbourne, - 1 hour, +30 minutes and +23 seconds + +$date = Factory::getDate('2025-12-1 15:20:00', $tz); // 3:20 PM, December 1st, 2025 +$dateFromTimestamp = Factory::getDate(1764599400, $tz); // 3:30 PM, December 1st, 2025 ``` -A Unix timestamp (in seconds) can also be passed as the first argument, in which case it will be internally uplifted -into a date. If a timezone has been specified as the second argument to the constructor, it will be converted to that -timezone. +:::note + +See Outputting Dates for more information on time zones. + +::: ## Outputting Dates @@ -112,8 +231,18 @@ for the current user's timezone settings. As such, this is the recommended metho use Joomla\CMS\Html\HtmlHelper; use Joomla\CMS\Language\Text; +$tz = new DateTimeZone('Australia/Melbourne'); +$date = Factory::getDate(1764599400, $tz); + +echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); // date and time in Melbourne for the given timestamp. +``` + +```php +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + $myDateString = '2025-12-1 15:20:00'; -echo HtmlHelper::date($myDateString, Text::_('DATE_FORMAT_FILTER_DATETIME')); +echo HtmlHelper::date($myDateString, Text::_('DATE_FORMAT_LC6')); ``` ### Using Date::format() Method @@ -122,6 +251,14 @@ Another option is to format the Date manually. If this method is used, you will the user's timezone. This method is more useful for formatting dates outside the user interface, such as in system logs or API calls. +The Date::format() method accepts up to three parameters: + +1. Date Formatting string +2. Boolean that indicating whether to use the configured timezone from the date object (default false) +3. Boolean to translate localised strings (default true). + +The formatting string is the same as the one used by the HtmlHelper::date() method. + ```php use Joomla\CMS\Language\Text; use Joomla\CMS\Date\Date; @@ -132,7 +269,42 @@ $timezone = Factory::getUser()->getTimezone(); $date = new Date($myDateString); $date->setTimezone($timezone); -echo $date->format(Text::_('DATE_FORMAT_FILTER_DATETIME')); +echo $date->format(Text::_('DATE_FORMAT_LC6'), true, true); +``` + +:::caution Using Custom Timezones + +If you are using a custom timezone, you will need to set the second parameter to true. Otherwise, the Date object will +use the GMT timezone by default, which may result in the wrong date being output. + +::: + +### Outputting Dates in ISO 8601 Format + +```php +$date = new Date('2025-12-1 15:20:00'); +$isoDate = $date->toISO8601(); // 20251201T152000Z +``` + +### Outputting Dates in RFC 822 Format + +```php +$date = new Date('2025-12-1 15:20:00'); +$rfcDate = $date->toRFC822(); // Sat, 01 Dec 2025 15:20:00 +0000 +``` + +### Outputting Dates in SQL Date-Time Format + +```php +$date = new Date('20251201T152000Z'); +$sqlDate = $date->toSQL(); // 2025-12-01 15:20:00 +``` + +### Outputting Dates as Unix Timestamps + +```php +$date = new Date('20251201T153000Z'); +$unixTime = $date->toUnix(); // 1764599400 ``` ## Other Useful Code Examples @@ -150,10 +322,34 @@ use Joomla\CMS\HTML\HTMLHelper; use Joomla\CMS\Language\Text; // These two are functionally equivalent -echo HtmlHelper::date('now', Text::_('DATE_FORMAT_FILTER_DATETIME')); +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); + +echo Factory::getDate()->format(Text::_('DATE_FORMAT_LC6')); + +// Or, if you want to output the current time in a different timezone +$timezone = new DateTimeZone('Australia/Melbourne'); +$date = Factory::getDate('now', $timezone); + +echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); + +echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); + + +``` + +### Using the current user's timezone + +```php +use Joomla\CMS\Factory; +use Joomla\CMS\Language\Text; + +$userTimezone = Factory::getUser()->getTimezone(); +$dateInUsersTimezone = Factory::getDate('now', $userTimezone); + +echo HtmlHelper::date($dateInUsersTimezone, Text::_('DATE_FORMAT_LC6')); + +echo Factory::getDate()->setTimezone($userTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); -$timezone = Factory::getUser()->getTimezone(); -echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_FILTER_DATETIME')); ``` ### Adding and Subtracting from Dates @@ -185,32 +381,4 @@ echo $date1->toSQL(); // 2026-12-02 15:20:00 $date2 = new Date('2025-12-1 15:20:00'); $date2->sub($interval); echo $date2->toSQL(); // 2024-11-30 15:20:00 -``` - -### Outputting Dates in ISO 8601 Format - -```php -$date = new Date('2025-12-1 15:20:00'); -$date->toISO8601(); // 20251201T152000Z -``` - -### Outputting Dates in RFC 822 Format - -```php -$date = new Date('2025-12-1 15:20:00'); -$date->toRFC822(); // Sat, 01 Dec 2025 15:20:00 +0000 -``` - -### Outputting Dates in SQL Date-Time Format - -```php -$date = new Date('20251201T152000Z'); -$date->toSQL(); // 2025-12-01 15:20:00 -``` - -### Outputting Dates as Unix Timestamps - -```php -$date = new Date('20251201T153000Z'); -$date->toUnix(); // 1764599400 ``` \ No newline at end of file From c7e32fcd4c0fc2bcf1d727541699236c83cb020d Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 10:59:07 +0100 Subject: [PATCH 04/15] Added examples for using timezone from the global configuration or the user object --- docs/general-concepts/classes/date.md | 38 ++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 3677ad87..8c6286a5 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -84,7 +84,7 @@ $timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' // Catch a potential error and set to UTC as fallback. try { - $siteTimezone = new DateTimeZone($timezoneString); + $siteTimezone = new DateTimeZone($timezoneString); } catch (DateInvalidTimeZoneException $e) { @@ -95,6 +95,38 @@ echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT ``` +### Using the timezone from the User Object + +You can also use the timezone of the current user. By getting the user's timezone from the user object, you can then +use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the +timezone of the Date object. + +```php +use Joomla\CMS\Factory; +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + +// Get Timezone from the current User or Global Configuration as a fallback. +$user = Factory::getApplication()->getIdentity(); +$timezone = $user->getTimezone(); // Returns DateTimeZone object +$timezoneString = $timezone->getName(); // e.g. 'Australia/Melbourne' + +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); +``` + +Using the Factory::getDate() method, setting the timezone manually and formatting the date: + +```php + +use Joomla\CMS\Factory; + +// Get Timezone from the current User or Global Configuration as a fallback. +$user = Factory::getApplication()->getIdentity(); +$timezone = $user->getTimezone(); // Returns DateTimeZone object + +echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); + +``` ## Arguments @@ -251,11 +283,11 @@ Another option is to format the Date manually. If this method is used, you will the user's timezone. This method is more useful for formatting dates outside the user interface, such as in system logs or API calls. -The Date::format() method accepts up to three parameters: +The Date::format() method accepts up to three parameters: 1. Date Formatting string 2. Boolean that indicating whether to use the configured timezone from the date object (default false) -3. Boolean to translate localised strings (default true). +3. Boolean to translate localised strings (default true). The formatting string is the same as the one used by the HtmlHelper::date() method. From 6314adc06cea8f1f862dc1b2c5746ed202fa1462 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 11:05:27 +0100 Subject: [PATCH 05/15] Re-Arranging sections Basics > Arguments > Output > Using GLobal / User Time Zones --- docs/general-concepts/classes/date.md | 156 +++++++++++++------------- 1 file changed, 78 insertions(+), 78 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 8c6286a5..8b84e4aa 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -52,82 +52,6 @@ The static method is only still available for backward compatibility and should ::: -## Using Timezone - -If no time zone has been defined, the Date class uses the UTC time zone. - -### Using the timezone from Global Configuration - -To use the configured time zone of the global configuration, you can use the following (recommended) code: - -```php -use Joomla\CMS\Html\HtmlHelper; -use Joomla\CMS\Language\Text; - -// Get Timezone from Global Configuration -$config = Factory::getApplication()->getConfig(); -$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' - -echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); -``` - -Or using the Factory::getDate() method, setting the timezone manually and formatting the date: - -```php - -use Joomla\CMS\Factory; - -// Get Timezone from Global Configuration -$config = Factory::getApplication()->getConfig(); -$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' - -// Catch a potential error and set to UTC as fallback. -try -{ - $siteTimezone = new DateTimeZone($timezoneString); -} -catch (DateInvalidTimeZoneException $e) -{ - $siteTimezone = new DateTimeZone('UTC'); -} - -echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); - -``` - -### Using the timezone from the User Object - -You can also use the timezone of the current user. By getting the user's timezone from the user object, you can then -use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the -timezone of the Date object. - -```php -use Joomla\CMS\Factory; -use Joomla\CMS\Html\HtmlHelper; -use Joomla\CMS\Language\Text; - -// Get Timezone from the current User or Global Configuration as a fallback. -$user = Factory::getApplication()->getIdentity(); -$timezone = $user->getTimezone(); // Returns DateTimeZone object -$timezoneString = $timezone->getName(); // e.g. 'Australia/Melbourne' - -echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); -``` - -Using the Factory::getDate() method, setting the timezone manually and formatting the date: - -```php - -use Joomla\CMS\Factory; - -// Get Timezone from the current User or Global Configuration as a fallback. -$user = Factory::getApplication()->getIdentity(); -$timezone = $user->getTimezone(); // Returns DateTimeZone object - -echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); - -``` - ## Arguments The Date constructor accepts two optional parameters: A date string to format and a @@ -135,7 +59,7 @@ timezone. Not passing a date string will create a Date object with the current d timezone will allow the Date object to use the default timezone set. The first argument, if used, should be a string that can be parsed using PHP's native DateTime constructor. -### Using the native Method with the default timezone +### Using the native Method with the Default Time Zone ```php use Joomla\CMS\Date\Date; @@ -153,7 +77,7 @@ $date = new Date('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 $dateFromTimestamp = new Date(1764599400); // 3:30 PM, December 1st, 2025 ``` -### Using the native Method with a custom timezone +### Using the native Method with a custom Time Zone ```php use Joomla\CMS\Date\Date; @@ -339,6 +263,82 @@ $date = new Date('20251201T153000Z'); $unixTime = $date->toUnix(); // 1764599400 ``` +## Using Different Time Zones + +If no time zone has been defined, the Date class uses the UTC time zone. + +### Using the timezone from Global Configuration + +To use the configured time zone of the global configuration, you can use the following (recommended) code: + +```php +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + +// Get Timezone from Global Configuration +$config = Factory::getApplication()->getConfig(); +$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' + +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); +``` + +Or using the Factory::getDate() method, setting the timezone manually and formatting the date: + +```php + +use Joomla\CMS\Factory; + +// Get Timezone from Global Configuration +$config = Factory::getApplication()->getConfig(); +$timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' + +// Catch a potential error and set to UTC as fallback. +try +{ + $siteTimezone = new DateTimeZone($timezoneString); +} +catch (DateInvalidTimeZoneException $e) +{ + $siteTimezone = new DateTimeZone('UTC'); +} + +echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); + +``` + +### Using the timezone from the User Object + +You can also use the timezone of the current user. By getting the user's timezone from the user object, you can then +use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the +timezone of the Date object. + +```php +use Joomla\CMS\Factory; +use Joomla\CMS\Html\HtmlHelper; +use Joomla\CMS\Language\Text; + +// Get Timezone from the current User or Global Configuration as a fallback. +$user = Factory::getApplication()->getIdentity(); +$timezone = $user->getTimezone(); // Returns DateTimeZone object +$timezoneString = $timezone->getName(); // e.g. 'Australia/Melbourne' + +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); +``` + +Using the Factory::getDate() method, setting the timezone manually and formatting the date: + +```php + +use Joomla\CMS\Factory; + +// Get Timezone from the current User or Global Configuration as a fallback. +$user = Factory::getApplication()->getIdentity(); +$timezone = $user->getTimezone(); // Returns DateTimeZone object + +echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); + +``` + ## Other Useful Code Examples ### Quickly Outputting the Current Time From a80e369e9e7ffbaa231cb9ecd5b608b382448649 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 11:10:25 +0100 Subject: [PATCH 06/15] always written as "time zone" and fixed headers --- docs/general-concepts/classes/date.md | 40 +++++++++++++-------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 8b84e4aa..28f6c0db 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -55,11 +55,11 @@ The static method is only still available for backward compatibility and should ## Arguments The Date constructor accepts two optional parameters: A date string to format and a -timezone. Not passing a date string will create a Date object with the current date and time, while not passing a -timezone will allow the Date object to use the default timezone set. The first argument, if used, should be a string +time zone. Not passing a date string will create a Date object with the current date and time, while not passing a +time zone will allow the Date object to use the default time zone set. The first argument, if used, should be a string that can be parsed using PHP's native DateTime constructor. -### Using the native Method with the Default Time Zone +### Using the native Method with the default Time Zone ```php use Joomla\CMS\Date\Date; @@ -103,7 +103,7 @@ See Outputting Dates for more information on time zones. ::: -### Using the Factory Method with the default timezone +### Using the Factory Method with the default Time Zone ```php use Joomla\CMS\Factory; @@ -121,7 +121,7 @@ $date = Factory::getDate('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 $dateFromTimestamp = Factory::getDate(1764599400); // 3:30 PM, December 1st, 2025 ``` -### Using the Factory Method with a custom timezone +### Using the Factory Method with a custom Time Zone ```php use Joomla\CMS\Factory; @@ -143,7 +143,7 @@ $dateFromTimestamp = Factory::getDate(1764599400, $tz); // 3:30 PM, December 1st :::note -See Outputting Dates for more information on time zones. +See Outputting dates for more information on time zones. ::: @@ -181,7 +181,7 @@ DATE_FORMAT_FILTER_DATETIME = "Y-m-d H:i:s" As with many common output items, the HtmlHelper class is here to... help! HtmlHelper's date() method will take any date-time string that the Date constructor would accept, along with a formatting string, and output the date appropriate -for the current user's timezone settings. As such, this is the recommended method for outputting dates for the user. +for the current user's time zone settings. As such, this is the recommended method for outputting dates for the user. ```php use Joomla\CMS\Html\HtmlHelper; @@ -204,13 +204,13 @@ echo HtmlHelper::date($myDateString, Text::_('DATE_FORMAT_LC6')); ### Using Date::format() Method Another option is to format the Date manually. If this method is used, you will have to also manually retrieve and set -the user's timezone. This method is more useful for formatting dates outside the user interface, such as in system +the user's time zone. This method is more useful for formatting dates outside the user interface, such as in system logs or API calls. The Date::format() method accepts up to three parameters: 1. Date Formatting string -2. Boolean that indicating whether to use the configured timezone from the date object (default false) +2. Boolean that indicating whether to use the configured time zone from the date object (default false) 3. Boolean to translate localised strings (default true). The formatting string is the same as the one used by the HtmlHelper::date() method. @@ -228,10 +228,10 @@ $date->setTimezone($timezone); echo $date->format(Text::_('DATE_FORMAT_LC6'), true, true); ``` -:::caution Using Custom Timezones +:::caution Using Custom Time Zones -If you are using a custom timezone, you will need to set the second parameter to true. Otherwise, the Date object will -use the GMT timezone by default, which may result in the wrong date being output. +If you are using a custom time zone, you will need to set the second parameter to true. Otherwise, the Date object will +use the GMT time zone by default, which may result in the wrong date being output. ::: @@ -267,7 +267,7 @@ $unixTime = $date->toUnix(); // 1764599400 If no time zone has been defined, the Date class uses the UTC time zone. -### Using the timezone from Global Configuration +### Using the Time Zone from Global Configuration To use the configured time zone of the global configuration, you can use the following (recommended) code: @@ -282,7 +282,7 @@ $timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); ``` -Or using the Factory::getDate() method, setting the timezone manually and formatting the date: +Or using the Factory::getDate() method, setting the time zone manually and formatting the date: ```php @@ -306,11 +306,11 @@ echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT ``` -### Using the timezone from the User Object +### Using the Time Zone from the User Object -You can also use the timezone of the current user. By getting the user's timezone from the user object, you can then +You can also use the time zone of the current user. By getting the user's time zone from the user object, you can then use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the -timezone of the Date object. +time zone of the Date object. ```php use Joomla\CMS\Factory; @@ -325,7 +325,7 @@ $timezoneString = $timezone->getName(); // e.g. 'Australia/Melbourne' echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); ``` -Using the Factory::getDate() method, setting the timezone manually and formatting the date: +Using the Factory::getDate() method, setting the time zone manually and formatting the date: ```php @@ -358,7 +358,7 @@ echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); echo Factory::getDate()->format(Text::_('DATE_FORMAT_LC6')); -// Or, if you want to output the current time in a different timezone +// Or, if you want to output the current time in a different time zone $timezone = new DateTimeZone('Australia/Melbourne'); $date = Factory::getDate('now', $timezone); @@ -369,7 +369,7 @@ echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6 ``` -### Using the current user's timezone +### Using the current User's Time Zone ```php use Joomla\CMS\Factory; From 713bce7a501d17ad9d3f553df50eaf42453c2f2b Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 11:15:11 +0100 Subject: [PATCH 07/15] Re-Arrange and removed duplicated example for getting / using Global Config Time Zone and User's Time Zone --- docs/general-concepts/classes/date.md | 83 +++++++++++---------------- 1 file changed, 33 insertions(+), 50 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 28f6c0db..db9221e4 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -263,9 +263,33 @@ $date = new Date('20251201T153000Z'); $unixTime = $date->toUnix(); // 1764599400 ``` -## Using Different Time Zones +## Other Useful Code Examples + +### Quickly Outputting the Current Time + +There are two easy ways of doing this. + +1. The HtmlHelper's date() method, if no date value is provided, will default to the current time. +2. Factory::getDate() gets the current date as a Date object, which we can then format. + +```php +use Joomla\CMS\Factory; +use Joomla\CMS\HTML\HTMLHelper; +use Joomla\CMS\Language\Text; + +// These two are functionally equivalent +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); + +echo Factory::getDate()->format(Text::_('DATE_FORMAT_LC6')); -If no time zone has been defined, the Date class uses the UTC time zone. +// Or, if you want to output the current time in a different time zone +$timezone = new DateTimeZone('Australia/Melbourne'); +$date = Factory::getDate('now', $timezone); + +echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); + +echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); +``` ### Using the Time Zone from Global Configuration @@ -303,10 +327,9 @@ catch (DateInvalidTimeZoneException $e) } echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); - ``` -### Using the Time Zone from the User Object +### Using the current User's Time Zone You can also use the time zone of the current user. By getting the user's time zone from the user object, you can then use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the @@ -339,51 +362,6 @@ echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6 ``` -## Other Useful Code Examples - -### Quickly Outputting the Current Time - -There are two easy ways of doing this. - -1. The HtmlHelper's date() method, if no date value is provided, will default to the current time. -2. Factory::getDate() gets the current date as a Date object, which we can then format. - -```php -use Joomla\CMS\Factory; -use Joomla\CMS\HTML\HTMLHelper; -use Joomla\CMS\Language\Text; - -// These two are functionally equivalent -echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); - -echo Factory::getDate()->format(Text::_('DATE_FORMAT_LC6')); - -// Or, if you want to output the current time in a different time zone -$timezone = new DateTimeZone('Australia/Melbourne'); -$date = Factory::getDate('now', $timezone); - -echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); - -echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); - - -``` - -### Using the current User's Time Zone - -```php -use Joomla\CMS\Factory; -use Joomla\CMS\Language\Text; - -$userTimezone = Factory::getUser()->getTimezone(); -$dateInUsersTimezone = Factory::getDate('now', $userTimezone); - -echo HtmlHelper::date($dateInUsersTimezone, Text::_('DATE_FORMAT_LC6')); - -echo Factory::getDate()->setTimezone($userTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); - -``` - ### Adding and Subtracting from Dates Because the Joomla Date object extends the PHP DateTime object, it provides methods for adding and subtracting from @@ -413,4 +391,9 @@ echo $date1->toSQL(); // 2026-12-02 15:20:00 $date2 = new Date('2025-12-1 15:20:00'); $date2->sub($interval); echo $date2->toSQL(); // 2024-11-30 15:20:00 -``` \ No newline at end of file +``` + +## Related external References + +- [PHP DateTime](https://www.php.net/manual/en/class.datetime.php) +- [PHP Time Zones](https://www.php.net/manual/en/timezones.php) \ No newline at end of file From 2f6380c66af521f26c918ca9998926db335d5dbb Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 11:16:08 +0100 Subject: [PATCH 08/15] Removed unnexessary line breaks in code examples --- docs/general-concepts/classes/date.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index db9221e4..3ca1650b 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -309,7 +309,6 @@ echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); Or using the Factory::getDate() method, setting the time zone manually and formatting the date: ```php - use Joomla\CMS\Factory; // Get Timezone from Global Configuration @@ -359,7 +358,6 @@ $user = Factory::getApplication()->getIdentity(); $timezone = $user->getTimezone(); // Returns DateTimeZone object echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); - ``` ### Adding and Subtracting from Dates From 0ce2376aa28be4f6b494722fc9599fb6526c589c Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 11:23:47 +0100 Subject: [PATCH 09/15] changed example and removed tz setup to match explication from the text. HtmlHelper uses the users timezone without any need to set it up before. --- docs/general-concepts/classes/date.md | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 3ca1650b..5c903dd0 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -179,18 +179,15 @@ DATE_FORMAT_FILTER_DATETIME = "Y-m-d H:i:s" ### Using HtmlHelper Method (Recommended) -As with many common output items, the HtmlHelper class is here to... help! HtmlHelper's date() method will take any -date-time string that the Date constructor would accept, along with a formatting string, and output the date appropriate -for the current user's time zone settings. As such, this is the recommended method for outputting dates for the user. +HtmlHelper's date() method will take any date-time string that the Date constructor would accept, along with a +formatting string, and output the date appropriate for the current user's time zone settings. As such, this is the +recommended method for outputting dates for the user. ```php use Joomla\CMS\Html\HtmlHelper; use Joomla\CMS\Language\Text; -$tz = new DateTimeZone('Australia/Melbourne'); -$date = Factory::getDate(1764599400, $tz); - -echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); // date and time in Melbourne for the given timestamp. +echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); // date and time in Melbourne for the given timestamp. ``` ```php From 54449458225306bd5cab2ee10801ce89e61ee321 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 13:28:36 +0100 Subject: [PATCH 10/15] Added Default PHP DateTime methods as examples and moved "Adding and Subtracting" to the new h2 for those examples --- docs/general-concepts/classes/date.md | 126 +++++++++++++++++++------- 1 file changed, 95 insertions(+), 31 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 5c903dd0..fa3d9884 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -52,6 +52,98 @@ The static method is only still available for backward compatibility and should ::: +## Modifying a Date Instance + +Since the Joomla! Date class inherits directly from PHP's DateTime class, various native methods of the PHP class are +available for modification. The full list of methods is available in the PHP documentation, which can be +found [here](https://www.php.net/manual/en/class.datetime.php). The following is a list of the most commonly used +methods as well as some examples of their use in Joomla! in combination with the Factory::getDate() method but can be +used with the other methods as well. + +### Adding and Subtracting from Dates + +One of the easiest of these methods to modify a date is the modify() method, which accepts any relative modification string that +the PHP [strtotime()](https://www.php.net/manual/en/function.strtotime.php) method would accept as shown below. + +```php +use Joomla\CMS\Date\Date; + +$date = new Date('2025-12-1 15:20:00'); +$date->modify('+1 year'); +echo $date->toSQL(); // 2026-12-01 15:20:00 +``` + +There are also separate add() and sub() methods, for adding or subtracting time from a date object respectively. These +accept PHP-standard DateInterval objects: + +```php +use Joomla\CMS\Date\Date; + +$interval = new \DateInterval('P1Y1D'); // Interval represents 1 year and 1 day + +$date1 = new Date('2025-12-1 15:20:00'); +$date1->add($interval); +echo $date1->toSQL(); // 2026-12-02 15:20:00 + +$date2 = new Date('2025-12-1 15:20:00'); +$date2->sub($interval); +echo $date2->toSQL(); // 2024-11-30 15:20:00 +``` + +### Set Time on Date Objects + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate(); +echo $date->toSql() // 2025-12-01 15:20:00 + +$date->setTime( 12, 0, 0 ); +echo $date->toSql(); // 2025-12-01 12:00:00 +``` + +### Set Timestamp on Date Objects + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate(); +$date->setTimestamp(1764599400); // Mon Dec 01 2025 14:30:00 UTC+0000 + +echo $date->format('Y-m-d H:i:s'); // 2025-12-01 14:30:00 +``` + +### Get Offset from UTC Time Zone + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate(); +$date->setTimezone(new DateTimeZone('Europe/Berlin')); + +echo $date->getOffset(); // 3600 +``` + +### Get the difference between two Dates + +```php +use Joomla\CMS\Factory; + +$date1 = Factory::getDate('now'); +$date2 = Factory::getDate('+1 year +35 days'); +$interval = $date1->diff($date2); + +echo $interval->format('%R%a days'); // +400 days +``` + +:::info Nice to know + +The diff() method returns a DateInterval object, which can be used to get the difference between two dates in a +human-readable format. The format string used in the example above is a standard PHP format string, more examples can be found +[here](https://www.php.net/manual/en/dateinterval.format.php). + +::: + ## Arguments The Date constructor accepts two optional parameters: A date string to format and a @@ -228,7 +320,7 @@ echo $date->format(Text::_('DATE_FORMAT_LC6'), true, true); :::caution Using Custom Time Zones If you are using a custom time zone, you will need to set the second parameter to true. Otherwise, the Date object will -use the GMT time zone by default, which may result in the wrong date being output. +use the GMT / UTC time zone by default, which may result in the wrong date being output. ::: @@ -357,38 +449,10 @@ $timezone = $user->getTimezone(); // Returns DateTimeZone object echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); ``` -### Adding and Subtracting from Dates - -Because the Joomla Date object extends the PHP DateTime object, it provides methods for adding and subtracting from -dates. The easiest of these methods to use is the modify() method, which accepts any relative modification string that -the PHP [strtotime()](https://www.php.net/manual/en/function.strtotime.php) method would accept. For example: -```php -use Joomla\CMS\Date\Date; - -$date = new Date('2025-12-1 15:20:00'); -$date->modify('+1 year'); -echo $date->toSQL(); // 2026-12-01 15:20:00 -``` - -There are also separate add() and sub() methods, for adding or subtracting time from a date object respectively. These -accept PHP-standard DateInterval objects: - -```php -use Joomla\CMS\Date\Date; - -$interval = new \DateInterval('P1Y1D'); // Interval represents 1 year and 1 day - -$date1 = new Date('2025-12-1 15:20:00'); -$date1->add($interval); -echo $date1->toSQL(); // 2026-12-02 15:20:00 - -$date2 = new Date('2025-12-1 15:20:00'); -$date2->sub($interval); -echo $date2->toSQL(); // 2024-11-30 15:20:00 -``` ## Related external References - [PHP DateTime](https://www.php.net/manual/en/class.datetime.php) -- [PHP Time Zones](https://www.php.net/manual/en/timezones.php) \ No newline at end of file +- [PHP Time Zones](https://www.php.net/manual/en/timezones.php) +- [PHP DateInterval::format](https://www.php.net/manual/en/dateinterval.format.php) \ No newline at end of file From 4a6420c9179f3ce2be6a27a120a3278414f1e53e Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 16:46:04 +0100 Subject: [PATCH 11/15] Improved Ordinal example Improved Dynamic Props example --- docs/general-concepts/classes/date.md | 119 +++++++++++++++++++++++++- 1 file changed, 117 insertions(+), 2 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index fa3d9884..9b1f4b59 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -62,7 +62,8 @@ used with the other methods as well. ### Adding and Subtracting from Dates -One of the easiest of these methods to modify a date is the modify() method, which accepts any relative modification string that +One of the easiest of these methods to modify a date is the modify() method, which accepts any relative modification +string that the PHP [strtotime()](https://www.php.net/manual/en/function.strtotime.php) method would accept as shown below. ```php @@ -139,7 +140,8 @@ echo $interval->format('%R%a days'); // +400 days :::info Nice to know The diff() method returns a DateInterval object, which can be used to get the difference between two dates in a -human-readable format. The format string used in the example above is a standard PHP format string, more examples can be found +human-readable format. The format string used in the example above is a standard PHP format string, more examples can be +found [here](https://www.php.net/manual/en/dateinterval.format.php). ::: @@ -239,6 +241,111 @@ See Outputting dates for more information on time zones. ::: +## Dynamic Properties + +The Date object has a number of dynamic properties that can be used to get information about the date. + +### Get count of Days in a Month + +```php +use Joomla\CMS\Factory; + +$date = new Date(); +// Factory example with different date for example purposes. +$dateFromFactory = Factory::getDate('now + 1 month'); + +echo $date->__get('daysinmonth'); // 30 +echo $dateFromFactory->__get('daysinmonth'); // 31 +``` + +### Get Day of Week + +```php +use Joomla\CMS\Factory; + +$date = new Date(); +// Factory example with different date for example purposes. +$dateFromFactory = Factory::getDate('now + 1 month'); + +echo $date->__get('dayofweek'); // 4 +echo $dateFromFactory->__get('dayofweek'); // 6 +``` + +### Get Day of Year + +```php +use Joomla\CMS\Factory; + +$date = new Date(); +// Factory example with different date for example purposes. +$dateFromFactory = Factory::getDate('now + 1 month'); + +echo $date->__get('dayofyear'); // 309 +echo $dateFromFactory->__get('dayofyear'); // 339 +``` + +### Checking for Leap Year + +```php +use Joomla\CMS\Factory; + +$date = new Date(); +// Factory example with different date for example purposes. +$dateFromFactory = Factory::getDate('now + 3 years'); + +// Note: Returns a boolean. +echo var_export($date->__get('isleapyear'), 1); // false +echo var_export($dateFromFactory->__get('isleapyear'), 1); // true +``` + +### Getting Year / Month / Week / Day / Hour / Minute / Second + +```php +use Joomla\CMS\Factory; + +$date = new Date(); +// Factory example with different date for example purposes. +$dateFromFactory = Factory::getDate('now + 2 months + 5 days + 1 hour + 12 minutes + 42 seconds'); + +echo $date->__get('year'); // 2025 +echo $dateFromFactory->__get('year'); // 2026 + +echo $date->__get('month'); // 11 +echo $dateFromFactory->__get('month'); // 02 + +echo $date->__get('week'); // 45 +echo $dateFromFactory->__get('week'); // 02 + +echo $date->__get('day'); // 06 +echo $dateFromFactory->__get('day'); // 11 + +echo $date->__get('hour'); // 13 +echo $dateFromFactory->__get('hour'); // 16 + +echo $date->__get('minute'); // 18 +echo $dateFromFactory->__get('minute'); // 31 + +echo $date->__get('second'); // 35 +echo $dateFromFactory->__get('second'); // 17 +``` + +### Get Ordinal Suffix + +The ordinal property returns the English suffix for ordinal numbers based on the day of the month. For example, if the +date is the 4th, it returns “th”, if it’s the 21st, it returns “st”. You can use this to format dates in English like +“Today is the 4th of November”. + +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate('01-01-2025'); +echo $date->__get('ordinal'); // st + +$date = Factory::getDate('02-01-2025'); +echo $date->__get('ordinal'); // nd + +``` + ## Outputting Dates One note of caution when outputting Date objects in a user context: do not simply print them to the screen. The Date @@ -449,7 +556,15 @@ $timezone = $user->getTimezone(); // Returns DateTimeZone object echo Factory::getDate()->setTimezone($timezone)->format(Text::_('DATE_FORMAT_LC6'), true); ``` +### Day to String +```php +use Joomla\CMS\Factory; + +$date = Factory::getDate('now'); +echo $date->dayToString(1, false); // Monday +echo $date->dayToString(2, true); // Tue +``` ## Related external References From 7e63b82289b60955c33cb44a9fccda84526a36dc Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 17:14:21 +0100 Subject: [PATCH 12/15] Switched Heading for Common Date Formats and added Common Date and Time Format Codes --- docs/general-concepts/classes/date.md | 39 ++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index 9b1f4b59..ed6be608 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -353,7 +353,37 @@ object's toString() method simply calls its parent's format() method, without co date formatting. This will not result in a good user experience and will lead to inconsistencies between the formatting in your extension, and elsewhere in Joomla. Instead, you should always output Dates using the methods shown below. -### Common Date Formats + +### Common Date and Time Format Codes + +```php +use Joomla\CMS\Factory; +$date = Factory::getDate('2025-01-06 15:05:05'); + +echo $date->format('D'); // Mo +echo $date->format('l'); // Monday +echo $date->format('M'); // Jan +echo $date->format('F'); // January +echo $date->format('d'); // 06 day of month +echo $date->format('H'); // 15 hour of day (24-hour clock) +echo $date->format('h'); // 03 hour of day (12-hour clock) +echo $date->format('a'); // pm or am based on the hour of day +echo $date->format('i'); // 05 minutes +echo $date->format('s'); // 05 seconds +echo $date->format('u'); // 000000 microseconds +echo $date->format('Y'); // 2025 year +echo $date->format('y'); // 25 year +echo $date->format('Y-m-d'); // 2025-01-06 +echo $date->format('Y-m-d H:i'); // 2025-01-06 15:05 +echo $date->format('Y-m-d H:i:s'); // 2025-01-06 15:05:05 +``` + +:::info +Check the official [PHP DateTime documentation](https://www.php.net/manual/en/datetime.format.php) +for a full list of available formats. +::: + +### Predefined Common Date Formats A number of date formats are predefined in Joomla as part of the base language packs. This is beneficial because it means date formats can be easily internationalised. A sample of the available format strings is below, from the en-GB @@ -393,8 +423,10 @@ echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6')); // date and time in Me use Joomla\CMS\Html\HtmlHelper; use Joomla\CMS\Language\Text; -$myDateString = '2025-12-1 15:20:00'; -echo HtmlHelper::date($myDateString, Text::_('DATE_FORMAT_LC6')); +$date = new Date(1764599400); +echo HtmlHelper::date($date, Text::_('DATE_FORMAT_LC6')); // 2025-12-1 15:30:00 +echo HtmlHelper::date($date, Text::_('D')); // Mon +echo HtmlHelper::date($date, Text::_('l')); // Monday ``` ### Using Date::format() Method @@ -569,5 +601,6 @@ echo $date->dayToString(2, true); // Tue ## Related external References - [PHP DateTime](https://www.php.net/manual/en/class.datetime.php) +- [PHP DateTime Formats](https://www.php.net/manual/en/datetime.format.php) - [PHP Time Zones](https://www.php.net/manual/en/timezones.php) - [PHP DateInterval::format](https://www.php.net/manual/en/dateinterval.format.php) \ No newline at end of file From 5815ae3d3e4b9c1b18e7f08c53a84aff3c082fc0 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 17:15:54 +0100 Subject: [PATCH 13/15] aligned indent --- docs/general-concepts/classes/date.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index ed6be608..bf01219d 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -550,7 +550,7 @@ try } catch (DateInvalidTimeZoneException $e) { - $siteTimezone = new DateTimeZone('UTC'); + $siteTimezone = new DateTimeZone('UTC'); } echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT_LC6'), true); From 408f41b4c33b7b83613c98264c073785c43f2383 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Thu, 6 Nov 2025 17:28:42 +0100 Subject: [PATCH 14/15] Typos and small text changes aswell as layouting. --- docs/general-concepts/classes/date.md | 47 ++++++++++++++------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index bf01219d..b1e45904 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -26,7 +26,8 @@ $date = new Date(); // Creates a new Date object equal to the current time. ### Factory::getDate() Method -There is no difference between these methods, as Date::getDate simply creates a new instance of Date exactly like +You may also create an instance using the `Factory::getDate()` method: +There is no difference between these methods, as `Factory::getDate()` simply creates a new instance of Date exactly like the first method shown. ```php @@ -57,8 +58,7 @@ The static method is only still available for backward compatibility and should Since the Joomla! Date class inherits directly from PHP's DateTime class, various native methods of the PHP class are available for modification. The full list of methods is available in the PHP documentation, which can be found [here](https://www.php.net/manual/en/class.datetime.php). The following is a list of the most commonly used -methods as well as some examples of their use in Joomla! in combination with the Factory::getDate() method but can be -used with the other methods as well. +methods as well as some examples of their use in Joomla!. ### Adding and Subtracting from Dates @@ -74,7 +74,8 @@ $date->modify('+1 year'); echo $date->toSQL(); // 2026-12-01 15:20:00 ``` -There are also separate add() and sub() methods, for adding or subtracting time from a date object respectively. These +There are also separate `add()` and `sub()` methods, for adding or subtracting time from a date object respectively. +These accept PHP-standard DateInterval objects: ```php @@ -139,7 +140,7 @@ echo $interval->format('%R%a days'); // +400 days :::info Nice to know -The diff() method returns a DateInterval object, which can be used to get the difference between two dates in a +The `diff()` method returns a DateInterval object, which can be used to get the difference between two dates in a human-readable format. The format string used in the example above is a standard PHP format string, more examples can be found [here](https://www.php.net/manual/en/dateinterval.format.php). @@ -348,11 +349,11 @@ echo $date->__get('ordinal'); // nd ## Outputting Dates -One note of caution when outputting Date objects in a user context: do not simply print them to the screen. The Date -object's toString() method simply calls its parent's format() method, without consideration for time zones or localized -date formatting. This will not result in a good user experience and will lead to inconsistencies between the formatting -in your extension, and elsewhere in Joomla. Instead, you should always output Dates using the methods shown below. - +One note of caution when outputting Date objects in a user context: do not print them to the screen. The Date +object's toString() method simply calls its parent's `format() method, without consideration for time zones or +localised date formatting. This will not result in a good user experience and will lead to inconsistencies between the +formatting in your extension, and elsewhere in Joomla!. Instead, you should always output Dates using the methods shown +below. ### Common Date and Time Format Codes @@ -379,17 +380,19 @@ echo $date->format('Y-m-d H:i:s'); // 2025-01-06 15:05:05 ``` :::info + Check the official [PHP DateTime documentation](https://www.php.net/manual/en/datetime.format.php) for a full list of available formats. + ::: ### Predefined Common Date Formats -A number of date formats are predefined in Joomla as part of the base language packs. This is beneficial because it -means date formats can be easily internationalised. A sample of the available format strings is below, from the en-GB -language pack. It is highly recommended to utilise these formatting strings when outputting dates so that your dates -will be automatically re-formatted according to a user's locale. They can be retrieved in the same way as any language -string. +A number of date formats are predefined in Joomla! as part of the base language packs. This is beneficial because it +means date formats can be easily internationalised. A sample of the available format strings from the +en-GB language pack is listed below. It is highly recommended to use these formatting strings when outputting dates so +that your dates will be automatically re-formatted according to a user's locale. They can be retrieved in the same way +as any language string. ```ini DATE_FORMAT_LC = "l, d F Y" @@ -408,7 +411,7 @@ DATE_FORMAT_FILTER_DATETIME = "Y-m-d H:i:s" ### Using HtmlHelper Method (Recommended) -HtmlHelper's date() method will take any date-time string that the Date constructor would accept, along with a +HtmlHelper's `date()` method will take any date-time string that the Date constructor would accept, along with a formatting string, and output the date appropriate for the current user's time zone settings. As such, this is the recommended method for outputting dates for the user. @@ -441,7 +444,7 @@ The Date::format() method accepts up to three parameters: 2. Boolean that indicating whether to use the configured time zone from the date object (default false) 3. Boolean to translate localised strings (default true). -The formatting string is the same as the one used by the HtmlHelper::date() method. +The formatting string is the same as the one used by the `HtmlHelper::date()` method. ```php use Joomla\CMS\Language\Text; @@ -497,8 +500,8 @@ $unixTime = $date->toUnix(); // 1764599400 There are two easy ways of doing this. -1. The HtmlHelper's date() method, if no date value is provided, will default to the current time. -2. Factory::getDate() gets the current date as a Date object, which we can then format. +1. The HtmlHelper's `date()` method, if no date value is provided, will default to the current time. +2. `Factory::getDate()` gets the current date as a Date object, which we can then format. ```php use Joomla\CMS\Factory; @@ -534,7 +537,7 @@ $timezoneString = $config->get('offset'); // e.g. 'Australia/Melbourne' echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); ``` -Or using the Factory::getDate() method, setting the time zone manually and formatting the date: +Or using the `Factory::getDate()` method, setting the time zone manually and formatting the date: ```php use Joomla\CMS\Factory; @@ -559,7 +562,7 @@ echo Factory::getDate()->setTimezone($siteTimezone)->format(Text::_('DATE_FORMAT ### Using the current User's Time Zone You can also use the time zone of the current user. By getting the user's time zone from the user object, you can then -use the time zone name as a string in the HtmlHelper::date method or the Date object's setTimezone() method to set the +use the time zone name as a string in the HtmlHelper::date method or the Date object's `setTimezone()` method to set the time zone of the Date object. ```php @@ -575,7 +578,7 @@ $timezoneString = $timezone->getName(); // e.g. 'Australia/Melbourne' echo HtmlHelper::date('now', Text::_('DATE_FORMAT_LC6'), $timezoneString); ``` -Using the Factory::getDate() method, setting the time zone manually and formatting the date: +Using the `Factory::getDate()` method, setting the time zone manually and formatting the date: ```php From ae8e12dbee0f91644f38535c992faba55124ac10 Mon Sep 17 00:00:00 2001 From: Marco Rensch Date: Fri, 7 Nov 2025 07:19:40 +0100 Subject: [PATCH 15/15] Fixed based on the github bot suggestions --- docs/general-concepts/classes/date.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/general-concepts/classes/date.md b/docs/general-concepts/classes/date.md index b1e45904..4404277c 100644 --- a/docs/general-concepts/classes/date.md +++ b/docs/general-concepts/classes/date.md @@ -166,7 +166,7 @@ $plus1YearTime = new Date('now +1 year'); // Current date and time, + 1 year. $plus1YearAnd1MonthTime = new Date('now +1 year +1 month'); // Current date and time, + 1 year and 1 month. $plusTimeToTime = new Date('now +1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds $plusTimeToTime = new Date('now -1 hour +30 minutes +3 seconds'); // Current date and time, + 1 hour, 30 minutes and 3 seconds -$combinedTimeToTime = new Date('now -1 hour -30 minutes 23 seconds'); // Current date and time, - 1 hour, +30 minutes and +23 seconds +$combinedTimeToTime = new Date('now -1 hour -30 minutes +23 seconds'); // Current date and time, - 1 hour, +30 minutes and +23 seconds $date = new Date('2025-12-1 15:20:00'); // 3:20 PM, December 1st, 2025 $dateFromTimestamp = new Date(1764599400); // 3:30 PM, December 1st, 2025 @@ -477,7 +477,7 @@ $isoDate = $date->toISO8601(); // 20251201T152000Z ```php $date = new Date('2025-12-1 15:20:00'); -$rfcDate = $date->toRFC822(); // Sat, 01 Dec 2025 15:20:00 +0000 +$rfcDate = $date->toRFC822(); // Mon, 01 Dec 2025 15:20:00 +0000 ``` ### Outputting Dates in SQL Date-Time Format