Normalizes a date value to a unix timestamp

@param string|\DateTime|int $date

@return int
@throws \InvalidArgumentException if the value cannot be converted to
a timestamp
511980
private function normalizeDateValue($date)
    {
        if (is_string($date)) {
            $date = strtotime($date);
        } elseif ($date instanceof \DateTime) {
            $date = $date->format('U');
        } elseif (!is_int($date)) {
            throw new \InvalidArgumentException('Date values must be a '
                . 'string, an int, or a DateTime object.');
        }

        return $date;
    }
929128
public function dateFormat($date, $format = '%e %b %Y')
    {
        if ($date instanceof \DateTime) {
            $ts = $date->getTimestamp();
        } elseif (is_numeric($date)) {
            $ts = $date;
        } elseif (preg_match('/^[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2}T[0-9]{2,2}:[0-9]{2,2}.*/', $date)) {
            // timestamp format 2013-01-01T00:00:00
            $ts = new \DateTime($date);
            $ts = $ts->getTimestamp();
        } else {
            throw new \InvalidArgumentException(sprintf("Cannot format %s as a date", $date));
        }

        return strftime($format, $ts);
    }
258743
public static function guessFrom($date)
    {
        if ($date instanceof DateTime) {
            return (new static())->setTimestamp($date->getTimestamp())->setTimezone($date->getTimezone());
        }

        if (is_numeric($date)) {
            return (new static())->setTimestamp((int) $date);
        }

        if (is_object($date) && method_exists($date, 'getTimestamp')) {
            return (new static())->setTimestamp($date->getTimestamp());
        }

        if (!Type::isStringLike($date)) {
            $typeName = Type::of($date);
            throw new InvalidArgumentException("No idea how to cast $typeName to DateTime");
        }

        $date = (string) $date;

        if ($dateTime = date_create($date)) {
            return $dateTime;
        }

        throw new InvalidArgumentException("No idea how to cast $date to DateTime");
    }
731372
private static function _utcZeroDateTime($date){

		$date = static::_nullValue($date);

		if($date === null){

			return $date;
		}

		$UTC = new DateTimeZone('UTC');
		$Date = new DateTime($date, $UTC);
		$Date->setTimezone($UTC);

		return $Date;
	}
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);
    }
968152
public function humanize($date, $config = array())
    {
        $config = new AnConfig($config);

        $config->append(array(
            'format' => '%B %d %Y',
            'relative' => false,
            'offset' => null,
        ));

        $format = $config->format;

        $diff = $this->_current_time->getDate(DATE_FORMAT_UNIXTIME) - $date->getDate(DATE_FORMAT_UNIXTIME);

        if ($config->relative) {
            $timeLeft = ($diff < 0) ? '-FUTURE' : '';

            $diff = abs($diff);

            if ($diff < 1) {
                return sprintf(AnTranslator::_('LIB-AN-DATE-MOMENT'), $diff);
            }

            if ($diff < 60) {
                return ($diff > 1) ? sprintf(AnTranslator::_('LIB-AN-DATE-SECONDS'.$timeLeft), $diff) : sprintf(AnTranslator::_('LIB-AN-DATE-SECOND'.$timeLeft), $diff);
            }

            $diff = round($diff / 60);

            if ($diff < 60) {
                return ($diff > 1) ? sprintf(AnTranslator::_('LIB-AN-DATE-MINUTES'.$timeLeft), $diff) : sprintf(AnTranslator::_('LIB-AN-DATE-MINUTE'.$timeLeft), $diff);
            }

            $diff = round($diff / 60);
            if ($diff < 24) {
                return ($diff > 1) ? sprintf(AnTranslator::_('LIB-AN-DATE-HOURS'.$timeLeft), $diff) : sprintf(AnTranslator::_('LIB-AN-DATE-HOUR'.$timeLeft), $diff);
            }

            $diff = round($diff / 24);
            if ($diff < 7) {
                return ($diff > 1) ? sprintf(AnTranslator::_('LIB-AN-DATE-DAYS'.$timeLeft), $diff) : sprintf(AnTranslator::_('LIB-AN-DATE-DAY'.$timeLeft), $diff);
            }

            $diff = round($diff / 7);

            if ($diff < 4) {
                return ($diff > 1) ? sprintf(AnTranslator::_('LIB-AN-DATE-WEEKS'.$timeLeft), $diff) : sprintf(AnTranslator::_('LIB-AN-DATE-WEEK'.$timeLeft), $diff);
            }
        } elseif ($config->offset) {
            $date->addHours($config->offset);
        }

        return $date->getDate($format);
    }
38417
protected function _normalize($date)
    {
        $date = preg_replace('/[^0-9\-]/', '', $date);
        $parts = explode('-', $date);
        $year = date('Y');
        $month = 1;
        $day = 1;

        if (!empty($parts[0]) &&
            1 <= intval($parts[0]) &&
            intval($parts[0]) <= 32767
        ) {
            $year = intval($parts[0]);
        }

        if (!empty($parts[1]) &&
            1 <= intval($parts[1]) &&
            intval($parts[1]) <= 12
        ) {
            $month = intval($parts[1]);
        }

        if (!empty($parts[2]) &&
            1 <= intval($parts[2]) &&
            intval($parts[2]) <= 31
        ) {
            $day = intval($parts[2]);
        }

        return date('Y-m-d', strtotime("{$year}-{$month}-{$day}"));
    }
1199123
protected function normalize_date_time( $value ) {

		if ( $value === '' || $value === NULL ) {
			return $value;
		}
		if ( ! is_string( $value ) && ! is_int( $value ) && ! $value instanceof DateTime ) {
			return $value;
		}
		if ( is_int( $value ) ) {
			//timestamp
			$value = new \DateTime( '@' . $value );
		} elseif ( ! $value instanceof DateTime ) {
			$value = new \DateTime( $value );
		}

		return $value->format( $this->options[ 'format' ] );
	}
670409
protected function _convertDate($date)
    {
        // regexps to validate input
        $datePatterns = [
            "/^([0-9]{2})\.([0-9]{4})/" => "EUR2", // MM.YYYY
            "/^([0-9]{2})\.([0-9]{2})/" => "EUR1", // DD.MM
            "/^([0-9]{2})\/([0-9]{4})/" => "USA2", // MM.YYYY
            "/^([0-9]{2})\/([0-9]{2})/" => "USA1" // DD.MM
        ];

        // date/time formatting rules
        $dateFormats = [
            "EUR1" => [2, 1],
            "EUR2" => [2, 1],
            "USA1" => [1, 2],
            "USA2" => [2, 1]
        ];

        // looking for date field
        $dateMatches = [];
        $stringModifier = getStr();
        foreach ($datePatterns as $pattern => $type) {
            if ($stringModifier->preg_match($pattern, $date, $dateMatches)) {
                $date = $dateMatches[$dateFormats[$type][0]] . "-" . $dateMatches[$dateFormats[$type][1]];
                break;
            }
        }

        return $date;
    }
646222
public static function LocalDate2OLE($date = null)
	{
		if (!isset($date)) {
			return "\x00\x00\x00\x00\x00\x00\x00\x00";
		}

		// factor used for separating numbers into 4 bytes parts
		$factor = pow(2, 32);

		// days from 1-1-1601 until the beggining of UNIX era
		$days = 134774;
		// calculate seconds
		$big_date = $days*24*3600 + gmmktime(date("H",$date),date("i",$date),date("s",$date),
											 date("m",$date),date("d",$date),date("Y",$date));
		// multiply just to make MS happy
		$big_date *= 10000000;

		$high_part = floor($big_date / $factor);
		// lower 4 bytes
		$low_part = floor((($big_date / $factor) - $high_part) * $factor);

		// Make HEX string
		$res = '';

		for ($i = 0; $i < 4; ++$i) {
			$hex = $low_part % 0x100;
			$res .= pack('c', $hex);
			$low_part /= 0x100;
		}
		for ($i = 0; $i < 4; ++$i) {
			$hex = $high_part % 0x100;
			$res .= pack('c', $hex);
			$high_part /= 0x100;
		}
		return $res;
	}
782869
public function normalize($object, $format = null, array $context = [])
    {
        if (!$object instanceof \DateInterval) {
            throw new InvalidArgumentException('The object must be an instance of "\DateInterval".');
        }

        return $object->format($context[self::FORMAT_KEY] ?? $this->defaultContext[self::FORMAT_KEY]);
    }
833682
public function setDateFrom($date = null)
    {
        $date = $this->resolveCarbon($date);

        return $this->setDate($date->year, $date->month, $date->day);
    }
1169083
private function parseRequestDate($date)
    {
        $timezone = new \DateTimeZone("UTC");
        $granularity = null;

        if (preg_match('#^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z$#', $date)) {
            $parsedDate = date_create_from_format('Y-m-d\TH:i:s\Z', $date, $timezone);
            $granularity = Identity::GRANULARITY_YYYY_MM_DDTHH_MM_SSZ;
        } elseif (preg_match('#^\d{4}-\d{2}-\d{2}$#', $date)) {
            // Add ! to format to set time to 00:00:00
            $parsedDate = date_create_from_format('!Y-m-d', $date, $timezone);
            $granularity = Identity::GRANULARITY_YYYY_MM_DD;
        } else {
            throw new BadArgumentException("Expected a data in one of the following formats: " .
                Identity::GRANULARITY_YYYY_MM_DDTHH_MM_SSZ . " OR " .
                Identity::GRANULARITY_YYYY_MM_DD ." FOUND " . $date);
        }

        $parseResult = date_get_last_errors();
        if (!$parsedDate || ($parseResult['error_count'] > 0) || ($parseResult['warning_count'] > 0)) {
            throw new BadArgumentException("$date is not a valid date");
        }

        return [$parsedDate, $granularity];
    }
734661
public static function formatDate($date)
    {
        $format = DateTime::ATOM;
        if ($date instanceof DateTime) {
            $d = $date->format($format);
        } elseif (is_numeric($date)) {
            $d = date($format, $date);
        } else {
            $d = (string) $date;
        }
        return $d;
    }
53770
private function parseTimestamp($date, $locale = null) {
        // try time default formats
        foreach ($this->formats as $timeFormat) {
            // try date default formats
            foreach($this->formats as $dateFormat) {
                $dateFormater = \IntlDateFormatter::create($locale ?: \Locale::getDefault(), $dateFormat, $timeFormat, date_default_timezone_get());
                $timestamp = $dateFormater->parse($date);

                if ($dateFormater->getErrorCode() == 0) {
                    return $timestamp;
                }
            }
        }

        // try other custom formats
        $formats = array(
            'MMMM yyyy', // november 2011 - nov. 2011
        );
        foreach($formats as $format) {
            $dateFormater = \IntlDateFormatter::create($locale ?: \Locale::getDefault(), $this->formats['none'], $this->formats['none'],  date_default_timezone_get(), \IntlDateFormatter::GREGORIAN, $format);
            $timestamp = $dateFormater->parse($date);

            if ($dateFormater->getErrorCode() == 0) {
                return $timestamp;
            }
        }

        throw new \Exception('"'.$date.'" could not be converted to \DateTime');
    }
302984
public function timeAgo($date)
    {
        $interval = $this->getInterval(time(), $date);
        if ($interval->invert) {
            return $this->convert(time(), $date);
        }

        return $this->translation->translate();
    }
729921
public function convertTimestamp($date, $format)
    {
        if (! is_numeric($date)) {
            $date = strtotime($date);
        }
        
        return $this->getDt()
            ->createFromTimestamp($date, $this->getTz())
            ->format($format);
    }