Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 39 and 400] [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

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: 695 lines (29 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.

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

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

return: string
param: string $tz

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.

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

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?

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

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

return: DateTimeZone
param: mixed $userorforcedtz

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.