Build a DateTime of a String

@param string $stringDate
@param mixed $base
@return \DateTimeInterface
@throws Exception
192871
protected function buildDateTimebyString($stringDate, $base = null)
    {
        if (is_null($base)) {
            $base = time();
        }

        try {
            $base = $base instanceof \DateTimeInterface ? $base->format('U') : strtotime((MathUtility::canBeInterpretedAsInteger($base) ? '@' : '') . $base);
            $dateTimestamp = strtotime((MathUtility::canBeInterpretedAsInteger($date) ? '@' : '') . $date, $base);
            $date = new \DateTime('@' . $dateTimestamp);
            $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));
        } catch (\Exception $exception) {
            throw new Exception('"' . $date . '" could not be parsed by \DateTime constructor: ' . $exception->getMessage(), 1241722579);
        }
    }
1342484
public function toDateTime($date)
    {
        if ($date instanceof \DateTimeInterface) {
            return $date;
        }

        if (preg_match('/^(\d{4})-(\d{1,2})-(\d{1,2})$/', $date)) {
            return \DateTime::createFromFormat('Y-m-d', $date);
        }

        return \DateTime::createFromFormat($this->db->getQuery(true)->getDateFormat(), $date);
    }
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;
    }
573257
public function date($date = null)
    {
        if (is_string($date)) {
            $date = new DateTime($date);
        }

        $this->date = $date;

        return $this;
    }
346335
public function hydrate($value)
    {
        if ($this->nullable && $value === null) {
            return null;
        }

        if (is_string($value) || is_int($value)) {
            if ($dateTime = DateTime::createFromFormat($this->getFormat(), $value)) {
                return $dateTime;
            }

            throw new Exception\InvalidArgumentException(sprintf(
                'Invalid format or value: format must be a string representing a valid \DateTime format, "%s" given;' .
                'value must be a string representing the time according to \DateTime::createFromFormat(), "%s" given.',
                $this->getFormat(),
                $value
            ));
        }

        throw new Exception\InvalidArgumentException(sprintf(
            'Invalid value: must be a string or integer representing the time according to the format, "%s" given',
            is_object($value) ? get_class($value) : gettype($value)
        ));
    }
457932
public function formatDate($format, \DateTime $date = null)
    {
        if (null === $date) {
            return null;
        }

        return $date->format($format);
    }
785961
public static function filterDate($inputDate = '')
    {
        if ($inputDate != '') {
            // Get date
            if ($inputDate == 'back_1_day') {
                try {
                    $dateTime = new \DateTime("-1 day");
                    $result   = [
                        'date'       => $dateTime->format('Y-m-d'),
                        'day'        => $dateTime->format('Ymd'),
                        'month'      => $dateTime->format('Y-m'),
                        'monthTable' => $dateTime->format('Y_m')
                    ];
                }
                catch (\Exception $e) {
                    if (function_exists('log_message')) {
                        $message = 'Error Code: ' . $e->getCode() . ' - File: ' . $e->getFile() . ' - Line: ' . $e->getLine() . ' - Message: ' . $e->getMessage();
                        log_message('error', $message);
                    }
                    $result = [
                        'date'       => date('Y-m-d', strtotime("-1 day", strtotime($inputDate))),
                        'day'        => date('Ymd', strtotime("-1 day", strtotime($inputDate))),
                        'month'      => date('Y-m', strtotime("-1 day", strtotime($inputDate))),
                        'monthTable' => date('Y_m', strtotime("-1 day", strtotime($inputDate)))
                    ];
                }
            } else {
                $result = [
                    'date'       => date('Y-m-d', strtotime($inputDate)),
                    'day'        => date('Ymd', strtotime($inputDate)),
                    'month'      => date('Y-m', strtotime($inputDate)),
                    'monthTable' => date('Y_m', strtotime($inputDate))
                ];
            }
        } else {
            $result = [
                'date'       => date('Y-m-d'),
                'day'        => date('Ymd'),
                'month'      => date('Y-m'),
                'monthTable' => date('Y_m')
            ];
        }

        return $result;
    }
417520
static function Parse($format, $dateTimeString)
     {
         $dt = \DateTime::createFromFormat($format, $dateTimeString);
         if (!$dt)
             return null;
         
         return self::FromDateTime($dt);
     }
457780
public function setDateTimeFrom($date = null)
    {
        $date = $this->resolveCarbon($date);

        return $this->modify($date->rawFormat('Y-m-d H:i:s.u'));
    }
1300624
private function _processDateTime($dateTime)
    {
        if (is_numeric($dateTime)) {
            $timestamp = $dateTime;
            $dateTime = new \DateTime();
            $dateTime->setTimestamp($timestamp);
        } elseif (empty($dateTime)) {
            throw new \InvalidArgumentException('Date/time is empty');
        } elseif (is_string($dateTime)) {
            $dateTime = new \DateTime($dateTime);
        }

        if (!($dateTime instanceof \DateTime)) {
            throw new \InvalidArgumentException('Incorrect date/time type');
        }
        return $dateTime;
    }
1070023
public function writeDate($date)
    {
        if ($this->writeObjectReference($date)) {
            return $this;
        }

        if ($date instanceof DateTime) {
            $dateString = $date->format('U') * 1000;
        } elseif ($date instanceof Zend_Date) {
            $dateString = $date->toString('U') * 1000;
        } else {
            require_once 'Zend/Amf/Exception.php';
            throw new Zend_Amf_Exception('Invalid date specified; must be a string DateTime or Zend_Date object');
        }

        $this->writeInteger(0x01);
        // write time to stream minus milliseconds
        $this->_stream->writeDouble($dateString);
        return $this;
    }
1264714
protected static function prepareDate($date)
    {
        if (!$date instanceof DateTimeBr) {
            $_date = DateTimeBr::createDate($date);
            if (!$_date) {
                throw new \DomainException("Tipo '$date' inváldo. Utilize DateBr.");
            }
            $date = $_date;
        }

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

		return $date;
	}
253880
public static function date2timestamp($date, $format = null)
    {
        $config = Grav::instance()['config'];
        $dateformat = $format ?: $config->get('system.pages.dateformat.default');

        // try to use DateTime and default format
        if ($dateformat) {
            $datetime = \DateTime::createFromFormat($dateformat, $date);
        } else {
            $datetime = new \DateTime($date);
        }

        // fallback to strtotime() if DateTime approach failed
        if ($datetime !== false) {
            return $datetime->getTimestamp();
        }

        return strtotime($date);
    }
166149
protected function date_format($date)
    {
        if (empty($date)) {
            return null;
        }

        if (!is_object($date) || !is_a($date, 'DateTime')) {
            try {
                $timestamp = rcube_utils::strtotime($date);
                $date      = new DateTime("@".$timestamp);
            }
            catch (Exception $e) {
                return null;
            }
        }

        return $date->format('d-M-Y H:i:s O');
    }
323899
public function getDate($date = null, $format = 'Y-m-d H:i:s')
    {
        if (strlen($date) < 0) {
            $date = date('d-m-Y H:i:s');
        }

        $timeStamp = new DateTime($date);
        return $timeStamp->format($format);
    }
1095704
public function date($date, $format = null)
	{
		$datetime = DateTimeUtil::getDateTime($date);
        if (empty($format)) {
			$format = $this->cfg['date_format'];
		}
		if ($format == 'rel') {
			$date = DateTimeUtil::getRelTime($datetime);
		} else {
			$date = $datetime->format($format);
		}
		return $date;
	}