Creates a DateTime object from the supplied date string

@param string $dateTimeString
@param string $timeZoneId

@return TimezonedDateTime
186052
public static function fromString(string $dateTimeString, string $timeZoneId) : TimezonedDateTime
    {
        return new self(new \DateTimeImmutable($dateTimeString, new \DateTimeZone($timeZoneId)));
    }
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;
    }
669458
public static function string2dateTime($string, \DateTimeZone $zone = null)
    {
        if (!$zone) {
            $zone = new \DateTimeZone('UTC');
        }

        $dt = new \DateTime('now', $zone);
        $dt->setTimestamp(self::string2timestamp($string));

        return $dt;
    }
575449
public function fromSqlToIso8601Time(string $string, ?DateTimeZone $displayedTimeZone = null): string
    {
        $dateTime = DateTime::createFromFormat(
            "Y-m-d H:i:s",
            $string,
            $displayedTimeZone
        );

        if ($dateTime === false) {
            return "";
        }

        return $dateTime->format(DateTime::ATOM);
    }
123716
private SimpleDateFormat createDateTimeTimeZoneFormat() {
		SimpleDateFormat dateTimeTimeZoneFormat = new SimpleDateFormat( DATE_TIME_TIMEZONE_FORMAT );
		dateTimeTimeZoneFormat.setLenient( false );
		return dateTimeTimeZoneFormat;
	}
1134925
public static function toDateTime($value, DateTimeZone $dtz) : DateTime
    {
        // TODO : Figure out where to move. Can vary for different source objects
        $format = 'Y-m-d H:i:s';

        if (is_string($value)) {
            $val = strtotime($value);
            if (false === $val) {
                throw new InvalidArgumentException(sprintf('Unsupported datetime string provided: %s', $value));
            }

            return new DateTime(date($format, $val), $dtz);
        }

        if ($value instanceof Time) {
            $value = $value->format($format);

            return new DateTime($value, $dtz);
        }

        if ($value instanceof DateTime) {
            return $value;
        }

        throw new InvalidArgumentException("Type [" . gettype($value) . "] is not supported for date/time");
    }
682148
public static function createFromString($string)
    {
        if (!preg_match(self::REGEX, strtoupper($string), $matches)) {
            return null;
        }

        $dateAndTime = $matches[1];
        $microseconds = $matches[2] ?: '.000000';
        $timeZone = 'Z' !== $matches[3] ? $matches[4] . ':' . $matches[5] : '+00:00';
        $dateFormat = strpos($dateAndTime, 'T') === false ? 'Y-m-d H:i:s.uP' : 'Y-m-d\TH:i:s.uP';
        $dateTime = \DateTime::createFromFormat($dateFormat, $dateAndTime . $microseconds . $timeZone, new \DateTimeZone('UTC'));

        return $dateTime ?: null;
    }
342603
public static DateTime iso8601DateTimeFromString(String dateTimeString) {
        try {
            return DateTime.parse(dateTimeString, ISO8601_DATE_TIME_FORMATTER);
        } catch (Exception e) {
            return null;
        }
    }
533377
public static DateTime toDate(String str, TimeZone tz) throws PageException {
	return DateCaster.toDateAdvanced(str, tz);
    }
71538
public static function of(string $id) : TimeZoneRegion
    {
        $id = (string) $id;

        if ($id === '' || $id === 'Z' || $id === 'z' || $id[0] === '+' || $id[0] === '-') {
            // DateTimeZone would accept offsets, but TimeZoneRegion targets regions only.
            throw DateTimeException::unknownTimeZoneRegion($id);
        }

        try {
            return new TimeZoneRegion(new \DateTimeZone($id));
        } catch (\Exception $e) {
            throw DateTimeException::unknownTimeZoneRegion($id);
        }
    }
750489
public static function fromString($timeString)
    {
        try {
            return static::fromDateTime(new DateTime($timeString));
        } catch (Exception $e) {
            throw new InvalidArgumentValue('$timeString', $timeString, __CLASS__, $e);
        }
    }
1152735
public static function timezone($timezone = null): \DateTimeZone
    {
        if ($timezone instanceof DateTimeZone) {
            return $timezone;
        }

        $timezone = $timezone ?: date_default_timezone_get();

        return new DateTimeZone($timezone);
    }
863329
public static function createFromFormat($format, $string, $timezone = null)
    {
        $timezone = $timezone ?: new DateTimeZone(date_default_timezone_get());
        $other = DateTime::createFromFormat($format, $string, $timezone);

        return (new static())->setTimestamp($other->getTimestamp())
                           ->setTimezone($other->getTimezone());
    }
1343461
public static DateTime fromStringWithMinutes( String dateTimeString ) {
        DateTime dt = withMinutesformatter.parseDateTime(dateTimeString);
        return dt;
    }
612672
protected function createDateFromFormat($dateString)
	{
		foreach($this->allowedDateFormats as $df) {
			if(($dt = \DateTime::createFromFormat($df, $dateString)) === false)
				continue;
			
			$lastErrors = $dt->getLastErrors();

			if($lastErrors['warning_count'] > 0 || $lastErrors['error_count'] > 0) {
				return false;
			}
			
			return $dt;
		}

		return false;
	}
1358325
public static String changeFormatAndTimeZoneToOffsetDateTime(
            String dateFormat, String dateString) {
        return getOffsetDateTime(changeTimestampToLong(dateFormat, dateString))
                .toString();
    }
1119091
public function inTimezone(string $timeZoneId) : TimezonedDateTime
    {
        return new TimezonedDateTime(
                \DateTimeImmutable::createFromFormat(
                        self::DISPLAY_FORMAT,
                        $this->format(self::DISPLAY_FORMAT),
                        new \DateTimeZone($timeZoneId)
                )
        );
    }