Get Target from Date

@param string $date The date to convert to a target.
@return string The target
174504
public function getTargetFromDate($date)
    {
        if (!preg_match('/^\d{4,14}$/', $date)) {
            throw new \InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
        }

        // what we need to append to the date according to the possible date string lengths
        $dateStrlenToAppend = [
            14 => '',
            12 => '00',
            10 => '0000',
            8 => '000000',
            6 => '01000000',
            4 => '0101000000',
        ];

        if (!isset($dateStrlenToAppend[strlen($date)])) {
            throw new \InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
        }

        $target = $date . $dateStrlenToAppend[strlen($date)];

        $dateTime = \DateTime::createFromFormat('YmdHis', $target);

        if ($dateTime === false) {
            throw new \InvalidArgumentException('Invalid date. Format is YYYY[MM[DD[HH[II[SS]]]]].');
        }

        return $dateTime->format('YmdHis');
    }
896520
public static function parseDate(string $date): ?\DateTimeImmutable
    {
        if (preg_match('/^\d{4}$/', $date)) {
            return \DateTimeImmutable::createFromFormat('!Y-m-d', $date.'-01-01', new \DateTimeZone('UTC'));
        }

        if (preg_match('/(\w{3}), (\d{4})/', $date, $matches)) {
            return \DateTimeImmutable::createFromFormat(
                '!M d, Y',
                "{$matches[1]} 01, {$matches[2]}",
                new \DateTimeZone('UTC')
            );
        }

        try {
            return new \DateTimeImmutable($date, new \DateTimeZone('UTC'));
        } catch (\Exception $e) {
            return null;
        }
    }
1041823
protected function toSql($date)
    {
        $tz = new DateTimeZone(JFactory::getApplication()->get('offset'));
        $date = new JDate($date);
        $date->setTimezone($tz);
        return $date->toSql(true);
    }
1017853
public function getStatementForDate($date)
    {
        if (is_string($date)) {
            $date = new \DateTime($date);
        }

        foreach ($this->statements as $stmt) {
            if ($stmt->getDate() == $date) {
                return $stmt;
            }
        }

        return null;
    }
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;
    }
798039
public function transform($date)
    {
        if (!$date instanceof DateTime) {
            $date = new DateTime($date);
        }

        $current = new DateTime('now');

        if ($this->isToday($date)) {
            return $this->trans('Today');
        }

        if ($this->isYesterday($date)) {
            return $this->trans('Yesterday');
        }

        if ($this->isTomorrow($date)) {
            return $this->trans('Tomorrow');
        }

        if ($this->isNextWeek($date)) {
            return $this->trans('Next %weekday%', [ '%weekday%' =>  $date->format('l') ]);
        }

        if ($this->isLastWeek($date)) {
            return $this->trans('Last %weekday%', [ '%weekday%' => $date->format('l') ]);
        }

        if ($this->isThisYear($date)) {
            return $date->format('F j');
        }

        return $date->format('F j, Y');
    }
971102
protected function getFormatedDate($date)
    {
        if (! is_null($date)) {
            if (! ($date instanceof Carbon)) {
                $date = Carbon::parse($date);
            }

            $date = $date->timezone($this->getTimezone())->format($this->getFormat());
        }

        return $date;
    }
299720
private function getTimeDrift(string $date) :? int
    {
        $now = new \DateTime();
        $now->format(\DateTime::RFC1123);

        try {
            $realDate = new DateTime($date);
        } catch (\Exception $e) {
            return null;
        }

        return (int)\abs($now->getTimestamp() - $realDate->getTimestamp());
    }
681718
public static function isDate($date): bool
    {
        if (!\preg_match('/^([\d]{4})-((?:0?[\d])|(?:1[0-2]))-((?:0?[\d])|(?:[1-2][\d])|(?:3[01]))( [\d]{2}:[\d]{2}:[\d]{2})?$/',
            $date, $matches)) {
            return false;
        }

        return \checkdate((int)$matches[2], (int)$matches[3], (int)$matches[1]);
    }
1057442
protected function normalizeDate($date)
    {
        $date = str_replace(array('/', '|'), '-', $date);

        if (empty($date)) {
            return date('Y-m-d H:i:s');
        } else if (ctype_digit($date)) {
            return date('Y-m-d H:i:s', $date);
        }

        return $date;
    }
179025
public static function parseForumDate(string $date): ?\DateTimeImmutable
    {
        if (!preg_match('/\d{4}/', $date)) {
            $date .= ', '.date('Y');
        }

        return self::parseDate($date);
    }
457780
public function setDateTimeFrom($date = null)
    {
        $date = $this->resolveCarbon($date);

        return $this->modify($date->rawFormat('Y-m-d H:i:s.u'));
    }
1303430
final public static function cloneDateTime($date)
    {
        try {
            $result = new \DateTimeImmutable();
            return $result->setTimezone($date->getTimezone())->setTimestamp($date->getTimestamp());
        } catch (\Exception $e) {
            return null;
        }
    }
1341257
public function parseDate($date)
    {
        $date = str_replace(['-', ':', '/', '.'], ' ', $date);
        $date_yyyy = $date_mm = $date_dd = $date_hh = $date_ii = $date_ss = 0;

        switch (sscanf($date, '%d %d %d %d %d %d', $date_yyyy, $date_mm, $date_dd, $date_hh, $date_ii, $date_ss)) {
            case 1:
                $date = sprintf('%04d/00/00 00:00:00', $date_yyyy);
                break;
            case 2:
                $date = sprintf('%04d/%02d/00 00:00:00', $date_yyyy, $date_mm);
                break;
            case 3:
                $date = sprintf('%04d/%02d/%02d 00:00:00', $date_yyyy, $date_mm, $date_dd);
                break;
            case 4:
                $date = sprintf('%04d/%02d/%02d %02d:00:00', $date_yyyy, $date_mm, $date_dd, $date_hh);
                break;
            case 5:
                $date = sprintf('%04d/%02d/%02d %02d:%02d:00', $date_yyyy, $date_mm, $date_dd, $date_hh, $date_ii);
                break;
            case 6:
                $date = sprintf('%04d/%02d/%02d %02d:%02d:%02d', $date_yyyy, $date_mm, $date_dd, $date_hh, $date_ii, $date_ss);
                break;
            default:
                $date = '0000/00/00 00:00:00';
        }

        return $date;
    }
26240
public function date_strtotime($date)
	{
		$strtotime = strtotime($date);
		if ($strtotime === -1 || $strtotime === false)
		{
			return false;
		}
		else
		{
			return $strtotime;
		}
	}
768347
private function getLogOrFail($date)
    {
        try {
            return $this->viewer->get($date);
        } catch (Exception $e) {
            return abort(404, $e->getMessage());
        }
    }
670964
public function getPostsByDate($date)
    {
        if (false === array_key_exists($date, $this->dateIndex)) {
            return [];
        }

        $postNames = $this->dateIndex[$date];

        return $this->getPostsByName($postNames);
    }