Creates a new date in the desired timezone

@param string $desiredTZ The desired timezone in which to return the date
@param mixed $date       An initialization string used to define the date.

@return Date

@throws \DateException
331578
protected function newDate($desiredTZ, $date = false)
    {
        if ($date === false) {
            $date = 'now';
        }

        try {
            $newDate = new Date(is_numeric('' . $date) ? '@' . $date : $date, $desiredTZ);

            // if the string TZ was not the desired TZ, we must convert it
            //$temp = new DateTime(is_numeric(''.$date)?'@'.$date:$date);
            //if(intVal($temp->format('Z')) != $desiredTZ->getOffset(new DateTime()))
            $newDate->setTimezone($desiredTZ);
        } catch (Exception $e) {
            throw new DateException($e);
        }

        unset($date);
        unset($desiredTZ);

        return $newDate;
    }
170882
public function setEndDate($date)
    {
        $date = AnDomainAttributeDate::getInstance()->setDate($date);
        $this->set('endDate', $date);

        return $this;
    }
1000014
public static function anytodatetime($date, $timezone = null)
    {
        if ($date instanceof DateTime) {
            return $date;
        }

        $dt   = false;
        $date = self::clean_datestr($date);

        // try to parse string with DateTime first
        if (!empty($date)) {
            try {
                $_date = preg_match('/^[0-9]+$/', $date) ? "@$date" : $date;
                $dt    = $timezone ? new DateTime($_date, $timezone) : new DateTime($_date);
            }
            catch (Exception $e) {
                // ignore
            }
        }

        // try our advanced strtotime() method
        if (!$dt && ($timestamp = self::strtotime($date, $timezone))) {
            try {
                $dt = new DateTime("@".$timestamp);
                if ($timezone) {
                    $dt->setTimezone($timezone);
                }
            }
            catch (Exception $e) {
                // ignore
            }
        }

        return $dt;
    }
295888
protected static function makeDate($date = null)
    {
        if (is_string($date)) {
            $date = new DateTime($date);
        } elseif (is_numeric($date)) {
            $date = new DateTime('@'.$date);
        }

        return self::isDate($date) ? $date : null;
    }
241906
public function set_date($date = null)
    {
        if (is_null($date)) {
            return $this->set_timestamp(time());
        }
        elseif (is_numeric($date)) {
            return $this->set_timestamp($date);
        }
        elseif (is_a($date, self::class)) {
            /** @var Datetime $date */
            return $this->set_date($date->format());
        }
        elseif (preg_match(self::REGEX_SQL, $date, $dateSplitted)) {
            if (\checkdate($dateSplitted['M'], $dateSplitted['D'], $dateSplitted['Y'])) {
                $h = (array_key_exists('h', $dateSplitted) && is_numeric($dateSplitted['h'])) ? $dateSplitted['h'] : 0;
                $m = (array_key_exists('m', $dateSplitted) && is_numeric($dateSplitted['m'])) ? $dateSplitted['m'] : 0;
                $s = (array_key_exists('s', $dateSplitted) && is_numeric($dateSplitted['s'])) ? $dateSplitted['s'] : 0;
    
                $mkt = mktime($h, $m, $s, $dateSplitted['M'], $dateSplitted['D'], $dateSplitted['Y']);
                $this->set_date($mkt);
                return $this;
            }
            else {
                return false;
            }
        }
        elseif (preg_match(self::REGEX_STR, $date, $dateSplitted)) {
            if (\checkdate($dateSplitted['M'], $dateSplitted['D'], $dateSplitted['Y'])) {
                $h = (array_key_exists('h', $dateSplitted) && is_numeric($dateSplitted['h'])) ? $dateSplitted['h'] : 0;
                $m = (array_key_exists('m', $dateSplitted) && is_numeric($dateSplitted['m'])) ? $dateSplitted['m'] : 0;
                $s = (array_key_exists('s', $dateSplitted) && is_numeric($dateSplitted['s'])) ? $dateSplitted['s'] : 0;
                
                $mkt = mktime($h, $m, $s, $dateSplitted['M'], $dateSplitted['D'], $dateSplitted['Y']);
                $this->set_date($mkt);
                
                return $this;
            }
            else {
                return false;
            }
        }
        elseif (preg_match(self::REGEX_SEM, $date, $dateSplitted)) {
            $baseDt = new \DateTime();
            $baseDt->setISOdate($dateSplitted['Y'], $dateSplitted['S']);
            return $this->set_date($baseDt->getTimestamp());
        }
        
        return false;
    }
10661
public static function makeFromDate($year = null, $month = null, $day = null, $timezone = null)
	{
		return static::makeFromDateTime($year, $month, $day, null, null, null, $timezone);
	}
1305615
public static function createDate($year = null, $month = null, $day = null, $timezone = null)
    {
        return static::create($year, $month, $day, null, null, null, $timezone);
    }
1229941
public function setDateByTimezone($timezone) {
    if (is_string($timezone)) {
      $timezone = new DateTimeZone($timezone);
    }
    $this->convertedTimezone = $timezone;
    $new_date_utc = $this->date->getTimeStamp() - $timezone->getOffset($this->date);
    $new_date = new DateTime();
    $new_date->setTimestamp($new_date_utc);
    $this->date = $new_date;
  }
516079
public static function safeCreateDateTimeZone($timezone)
    {
        if ($timezone === null) {
            // Don't return null...
            return new DateTimeZone(date_default_timezone_get());
        }

        if ($timezone instanceof DateTimeZone) {
            return $timezone;
        }

        /** @noinspection PhpUsageOfSilenceOperatorInspection */
        $tz = @timezone_open((string) $timezone);

        if ($tz === false) {
            throw new Exception\InvalidArgumentException(
                sprintf(
                    'Unknown or bad timezone (%s)', is_scalar($timezone) ? $timezone : gettype($timezone)
                )
            );
        }

        return $tz;
    }
1338813
private static function cast(Date $date = null)
	{
		if (!is_null($date))
		{
			$date = new DateTime($date);
		}

		return $date;
	}
435669
public function setCreated( $date, $format = null )
    {
        $this->created = $this->inputDate( $date, $format );
        return $this;
    }
1356612
public function date(string $format, \DateTimeZone $timeZone = null) : DateFieldBuilder
    {
        return $this->dateTypeBuilder(new DateType($format, $timeZone));
    }
832242
public static function createFromFormat($format, $date, $tz = null) {
        
        if ($tz !== null) {
            $tz = static::safeCreateDateTimeZone($tz);
        }
        
        $instance = new static($tz);
        
        $instance->parseFormat($format, $date);
        
        return $instance;
    }
1204307
public static function fromDate(\DateTime $date)
    {
        return new self($date->format('H'), $date->format('i'), $date->format('s'));
    }
1025564
public static function formatDate($date, $format = 'Y-m-d')
    {
        if (!$date) {
            return null;
        }
        $date = new DateTime($date);
        return $date->format($format);
    }
802584
public function date($date)
    {
        if ($date instanceof \DateTime) {
            $date = $date->format('Y-m-d H:i:s');
        }
        $this->arguments[] = '--date=' . $date;
        return $this;
    }
1152735
public static function timezone($timezone = null): \DateTimeZone
    {
        if ($timezone instanceof DateTimeZone) {
            return $timezone;
        }

        $timezone = $timezone ?: date_default_timezone_get();

        return new DateTimeZone($timezone);
    }