Back to Date class

Method __construct

public
__construct
(mixed $date = 'now', mixed $tz = null)
Constructor.
Parameters
  • string $date String in a format accepted by strtotime(), defaults to "now".
  • mixed $tz Time zone to be used for the date. Might be a string or a DateTimeZone object.
Since
  • 1.7.0
Class: Date
Project: Joomla

Method __construct - Source code

/**
 * Constructor.
 *
 * @param   string  $date  String in a format accepted by strtotime(), defaults to "now".
 * @param   mixed   $tz    Time zone to be used for the date. Might be a string or a DateTimeZone object.
 *
 * @since   1.7.0
 */
public function __construct($date = 'now', $tz = null)
{
    // Create the base GMT and server time zone objects.
    if (empty(self::$gmt) || empty(self::$stz)) {
        // @TODO: This code block stays here only for B/C, can be removed in 5.0
        self::$gmt = new \DateTimeZone('GMT');
        self::$stz = new \DateTimeZone(@date_default_timezone_get());
    }
    // If the time zone object is not set, attempt to build it.
    if (!$tz instanceof \DateTimeZone) {
        if (\is_string($tz)) {
            $tz = new \DateTimeZone($tz);
        } else {
            $tz = new \DateTimeZone('UTC');
        }
    }
    // Backup active time zone
    $activeTZ = date_default_timezone_get();
    // Force UTC timezone for correct time handling
    date_default_timezone_set('UTC');
    // If the date is numeric assume a unix timestamp and convert it.
    $date = is_numeric($date) ? date('c', $date) : $date;
    // Call the DateTime constructor.
    parent::__construct($date, $tz);
    // Restore previously active timezone
    date_default_timezone_set($activeTZ);
    // Set the timezone object for access later.
    $this->tz = $tz;
}