Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

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

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.

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.