Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

Core date and time related code.

Author: Petr Skoda <petr.skoda@totaralms.com>
Copyright: 2015 Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 926 lines (38 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: core_date  - X-Ref

Core date and time related code.

get_list_of_timezones($currentvalue = null, $include99 = false)   X-Ref
Returns a localised list of timezones.

param: string $currentvalue
param: bool $include99 should the server timezone info be included?
return: array

get_localised_timezone($tz)   X-Ref
Returns localised timezone name.

param: string $tz
return: string

normalise_timezone($tz)   X-Ref
Normalise the timezone name. If timezone not supported
this method falls back to server timezone (if valid)
or default PHP timezone.

param: int|string|float|DateTimeZone $tz
return: string timezone compatible with PHP

get_server_timezone()   X-Ref
Returns server timezone.

return: string normalised timezone name compatible with PHP

get_server_timezone_object()   X-Ref
Returns server timezone.

return: DateTimeZone

set_default_server_timezone()   X-Ref
Set PHP default timezone to $CFG->timezone.


get_user_timezone($userorforcedtz = null)   X-Ref
Returns user timezone.

Ideally the parameter should be a real user record,
unfortunately the legacy code is using 99 for both server
and default value.

Example of using legacy API:
// Date for other user via legacy API.
$datestr = userdate($time, core_date::get_user_timezone($user));

The coding style rules in Moodle are moronic,
why cannot the parameter names have underscores in them?

param: mixed $userorforcedtz user object or legacy forced timezone string or tz object
return: string normalised timezone name compatible with PHP

get_user_timezone_object($userorforcedtz = null)   X-Ref
Return user timezone object.

param: mixed $userorforcedtz
return: DateTimeZone

get_default_php_timezone()   X-Ref
Return default timezone set in php.ini or config.php.

return: string normalised timezone compatible with PHP

store_default_php_timezone()   X-Ref
To be called from lib/setup.php only!


phpunit_override_default_php_timezone($tz)   X-Ref
Do not use directly - use $this->setTimezone('xx', $tz) instead in your test case.

param: string $tz valid timezone name

phpunit_reset()   X-Ref
To be called from phpunit reset only, after restoring $CFG.


init_zones()   X-Ref
Initialise timezone arrays, call before use.


strftime(string $format, $timestamp = null, ?string $locale = null)   X-Ref
Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)
This provides a cross-platform alternative to strftime() for when it will be removed from PHP.
Note that output can be slightly different between libc sprintf and this function as it is using ICU.

From:
https://github.com/alphp/strftime

param: string $format Date format
param: int|string|DateTime $timestamp Timestamp
param: string|null $locale
author: BohwaZ <https://bohwaz.net/>
return: string