Parse a string into a new DateTime object according to the specified format

@param string $format
@param string $time
@param null $timezone
@return DateTime|\DateTime
188569
public static function createFromFormat($format, $time, $timezone = null)
    {
        if ($timezone === null) {
            $timezone = new DateTimeZone(date_default_timezone_get());
        }

        $datetime = \DateTime::createFromFormat($format, $time, $timezone);

        return new self($datetime->format(DateTime::ISO8601), $timezone);
    }
1075641
public function getTime($format = null)
    {
        //argument 1 must be a string or null
        Argument::i()->test(1, 'string', 'null');

        $time = $this->time + $this->offset;

        if (!is_null($format)) {
            return date($format, $time);
        }

        return $time;
    }
326595
public function format(string $format, TimeZone $timeZone = null): string
    {
        if (null !== $timeZone) {
            return $timeZone->translate($this)->format($format);
        }

        return $this->dateTime->format($format);
    }
755842
public static Date parseDate(final String date, final String format, final TimeZone timeZone) {
        if (N.isNullOrEmpty(date) || (date.length() == 4 && "null".equalsIgnoreCase(date))) {
            return null;
        }

        return createDate(parse(date, format, timeZone));
    }
63171
public static function parse($datetime, $timezone = null) {
        $monthName = array_map(function ($value) {
            return ' ' . $value . ' ';
        }, self::$monthYear);
        $monthValue = array_map(function ($value) {
            return '-' . $value . '-';
        }, range(1,12));
        $datetime = str_replace($monthName, $monthValue, $datetime);

        $parse = date_parse($datetime);
        if($parse['error_count'] == 0){
            list($year, $month, $day) = self::getGregorian($parse['year'], $parse['month'], $parse['day']);
            list($hour,$minute, $second) = array($parse['hour'], $parse['minute'], $parse['second']);

            $timezone = self::createTimeZone($timezone);
            $datetime = new DateTime(sprintf('%04s-%02s-%02s %02s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $timezone);
            return new static($datetime);
        }
        else{
            throw new InvalidArgumentException(sprintf("Unknown datetime '%s'", $datetime));
        }
    }
939306
public static function createFromFormat($format, $datetime, $timezone = null)
    {
        $datepoint = parent::createFromFormat($format, $datetime, $timezone);
        if (false !== $datepoint) {
            return self::create($datepoint);
        }

        return $datepoint;
    }
19648
public function convertTo($zone, $format = null)
    {
        Argument::i()
            //argument 1 must be a string
            ->test(1, 'string')
            //argument 1 must be a timezone identifier
            ->test(1, 'location', 'utc', 'abbr')
            //argument 2 must be a string or null
            ->test(2, 'string', 'null');

        $time = $this->time + $this->calculateOffset($zone);

        if (!is_null($format)) {
            return date($format, $time);
        }

        return $time;
    }
1170396
public function format($dateTime, $format = null, $timezone = null)
	{
		// Do string replacements for date format options that can be translated.
		$format = preg_replace('/(^|[^\\\])D/', "\\1" . self::DAY_ABBR, $format);
		$format = preg_replace('/(^|[^\\\])l/', "\\1" . self::DAY_NAME, $format);
		$format = preg_replace('/(^|[^\\\])M/', "\\1" . self::MONTH_ABBR, $format);
		$format = preg_replace('/(^|[^\\\])F/', "\\1" . self::MONTH_NAME, $format);


		// Clone to allow timezone operations
		if ($dateTime instanceof \DateTime)
		{
			$dateTimeInZone = clone $dateTime;
		}
		// Or create from string
		else
		{
			$dateTimeInZone = new \DateTime($dateTime);
		}

		// Set timezone string argument
		if ($timezone instanceof \DateTimeZone)
		{
			$dateTimeInZone->setTimeZone($timezone);
		}
		// Set timezone DateTimeZone argument
		else if (is_string($timezone))
		{
			$dateTimeInZone->setTimeZone(new \DateTimeZone($timezone));
		}
		// Or global class option
		else
		{
			$dateTimeInZone->setTimeZone($this->timezone);
		}


		// Format to string
		$return = $dateTimeInZone->format($format ?: $this->format);


		// Manually modify the month and day strings in the formatted time.
		if (strpos($return, self::DAY_ABBR) !== false)
		{
			$return = str_replace(self::DAY_ABBR, $this->dayToString($dateTimeInZone->format('w'), true), $return);
		}

		if (strpos($return, self::DAY_NAME) !== false)
		{
			$return = str_replace(self::DAY_NAME, $this->dayToString($dateTimeInZone->format('w')), $return);
		}

		if (strpos($return, self::MONTH_ABBR) !== false)
		{
			$return = str_replace(self::MONTH_ABBR, $this->monthToString($dateTimeInZone->format('n'), true), $return);
		}

		if (strpos($return, self::MONTH_NAME) !== false)
		{
			$return = str_replace(self::MONTH_NAME, $this->monthToString($dateTimeInZone->format('n')), $return);
		}


		return $return;
	}
1087450
public function getTime(string $format = null)
    {
        $time = $this->time + $this->offset;

        if (!is_null($format)) {
            return date($format, $time);
        }

        return $time;
    }
657034
public static function timeFormat($timestamp = null, $format = 'Y-m-d H:i:s')
    {
        if ($timestamp === null) {
            $timestamp = time();
        } elseif (empty($timestamp)) {
            return null;
        } else {
            $timestamp = intval($timestamp);
        }
        if (strncmp($format, 'php:', 4) === 0) {
            $format = substr($format, 4);
        }
        
        return date($format, $timestamp);
    }
297790
public function getDeletedAtDatetime(string $timezone = null): ?DateTime
    {
        if (!$this->isDeleted()) {
            return null;
        }

        return new DateTime($this->getDeletedAt(), new DateTimeZone($timezone ?? 'UTC'));
    }
1161503
public function getDateTime($time = null)
    {
        if(null === $time)
        {
            $time = time();
        }
        return date($this->dateTimeFormat, $time);
    }
219998
private function parse($date, $timezone = null)
    {
        if (!($date instanceof \DateTime)) {
            if (false === strtotime($date)) {
                throw new \Exception('Invalid date not parsed.');
            }

            if (!$timezone) {
                $timezone = $this->timezoneSource;
            }

            $date = new \DateTime($date, new \DateTimeZone($timezone));
        }

        return $date;
    }
1328369
public function getUpdatedAtDatetime(string $timezone = null): ?DateTime
    {
        if (!$this->hasUpdatedAt()) {
            return null;
        }

        if ($timezone === null) {
            $timezone = 'UTC';
        }

        return new DateTime($this->getUpdatedAt(), new DateTimeZone($timezone));
    }
896519
public static function fromFormat(string $format, string $dateString, string $timeZoneId) : TimezonedDateTime
    {
        return new self(\DateTimeImmutable::createFromFormat('!' . $format, $dateString, new \DateTimeZone($timeZoneId)));
    }
1119756
public static function newPhpDateTime($time = 'now', \DateTimeZone $timeZone = null)
    {
        if (self::DATETIME_SYSTEM === self::$type) {
            return new \DateTime($time, $timeZone);
        }

        if (self::DATETIME_FIXED === self::$type) {
            return self::getFixedTimeFromConfiguredTimestamp($time, $timeZone);
        }

        $date = self::getFixedTimeFromConfiguredTimestamp($time, $timeZone);
        $timePassedSinceInstantiation = abs(
            (new \DateTime())->getTimestamp() - self::$offsetTimestamp
        );
        $date->modify("+{$timePassedSinceInstantiation} seconds");

        return $date;
    }
1205436
public static function factory($time = null, $timezone = null) {
        $timezone = static::timezone($timezone);

        if ($time instanceof DateTime) {
            return $time->setTimezone($timezone);
        }

        if (is_numeric($time)) {
            $time = '@' . $time;
        }

        $dt = new DateTime($time);
        $dt->setTimezone($timezone);

        return $dt;
    }