Take a \DateTime value and convert it to the string date format that the API expects.

@param \DateTime|mixed $dateTime
@return string
765149
public static function getStringValue($dateTime): string
    {
        // do not return anything if no value was specified
        if (!$dateTime) {
            return '';
        }

        // until a mixed type is supported for type-hints, check the value here
        if (!$dateTime instanceof \DateTime) {
            return (new \DateTime($dateTime))->format(static::DATE_FORMAT);
        }

        return $dateTime->format(static::DATE_FORMAT);
    }
188018
public static function getDateSQLToForm($datetime)
    { 
        //YYYY-mm-dd
        $t = $this->splitDate($datetime, false);

        return $t['date'] . '/' . $t['month'] . '/' . $t['year'];
    }
411810
protected static function convertDateTime($date)
    {
        if (!$date) {
            return;
        }

        $date = new \DateTime($date);
        $date->setTimezone(new \DateTimeZone(date_default_timezone_get()));

        return $date->format(\DateTime::ISO8601);
    }
410526
public function getStringByDate(DateTime $date)
    {
        $dateNow = new DateTime();

        if ($dateNow < $date) {
            return 'Future date not implemented.';
        }

        if ($date->format('Ymd') === $dateNow->format('Ymd')) {
            return $this->getTranslator()->trans('time.today');
        }

        if ($date->format('Ymd') === ((new DateTime('yesterday'))->format('Ymd'))) {
            return $this->getTranslator()->trans('time.yesterday');
        }

        $diffDays = ceil(($dateNow->getTimestamp() - $date->getTimestamp()) / 86400);

        if (7 > $diffDays) {
            return $this->getTranslator()->trans('time.days_ago', [
                '{days}' => $diffDays,
            ]);
        }

        if ($date->format('W') === (new DateTime('-1 Week'))->format('W')) {
            return $this->getTranslator()->trans('time.last_week');
        }

        if (28 > $diffDays) {
            return $this->getTranslator()->trans('time.weeks_ago', [
                '{weeks}' => floor($diffDays / 7),
            ]);
        }

        if ($date->format('Ym') === (new DateTime('-1 month'))->format('Ym')) {
            return $this->getTranslator()->trans('time.last_month');
        }

        if ($date->format('Y') === ($dateNow->format('Y') - 1)) {
            return $this->getTranslator()->trans('time.last_year');
        }

        return $this->getTranslator()->trans('time.years_ago', [
            '{years}' => $dateNow->format('Y') - $date->format('Y'),
        ]);
    }
1321171
public function setDateTime($dateTime)
	{
		$this->registry()->register(self::REGISTRY_DATETIME, $dateTime, true);
		return $this;
	}
197107
public static function getTimestamp($dateTime = null)
    {
        return $dateTime ? is_numeric($dateTime) ? $dateTime : strtotime($dateTime) : time();
    }
1205368
protected function getDateTime($dateTime)
    {
        if ($dateTime instanceof \DateTime) {
            return $dateTime;
        }

        if ((is_string($dateTime) && (int) $dateTime > 0) || is_numeric($dateTime)) {
            return (new \DateTime())->setTimestamp($dateTime);

        }

        throw new \common_Exception(__('A date has to be a Datetime or timestamp'));
    }
214697
public static function convertDateTimeObject(\DateTime $dateTime, $includeTimezone = true)
    {
        $formatString = 'Y-m-d\TH:i:s'.(true === $includeTimezone ? 'P' : '\Z');
        $string = $dateTime->format($formatString);

        return $string;
    }
543314
protected function encodeDateTime(DateTime $value = null) {
        return null === $value ? "" : $value->format(self::DATETIME_FORMAT);
    }
234086
public static function isValidDateTime($dateTime)
    {
        $t = explode(' ', $dateTime);
        if ($this->isValidDate($t[0]) && $this->isValidDate($t[1])) {
            return true;
        } else {
            return false;
        }
    }
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);
    }
1120273
public function getDateString(DateTime $time)
	{
		if ($this->getOption('type') === 'date')
		{
			return $time->format('Y-m-d');
		}
		else if ($this->getOption('type') === 'datetime')
		{
			return $time->format('Y-m-d H:i:s');
		}
		else
		{
			return $time->format('H:i:s');
		}
	}
1119910
public function buildDateTimeRawValue(\DateTime $dateTime = null) {
		if (null === $dateTime) return null;
		return DateUtils::formatDateTime($dateTime, MssqlDateTimeColumn::generateFormatBuildRawValue(
				MssqlDateTimeColumn::FORMAT_BUILD_TYPE_RAW_VALUE, true, true, self::COLUMN_TYPE_PRECISION, false));
	}
1372076
public static function shortDateTime($datetime)
    {
        if (self::isDateTime($datetime)) {
            $str = self::dateTimeToTimeStamp($datetime);
            $string = date("d.m.Y H:i", $str);
            return $string;
        } else {
            return '';
        }
    }
442564
private static function validateDateTime($datetime)
	    {
	        if ($datetime instanceof DateTimeInterface || $datetime instanceof DateTime) {
	            return $datetime;
	        }

	        return new DateTime((string) $datetime);
	    }
136950
public static function fromDateTime(DateTime $dateTime)
    {
        $dateTime = clone $dateTime;

        return new static($dateTime->getTimestamp() - $dateTime->setTime(0, 0, 0)->getTimestamp());
    }
533883
public static function getRelTime($datetime, $datetimeFrom = null)
    {
        $ret = '';
        $dateTime = self::getDateTime($datetime);
        $dateTimeFrom = self::getDateTime($datetimeFrom);
        $dateTimeSameDay = clone $dateTimeFrom;
        $dateTimeSameDay->setTime(0, 0, 0);
        $diffSec = $dateTimeFrom->getTimestamp() - $dateTime->getTimestamp();
        if ($dateTime > $dateTimeFrom) {
            // Future
            $dateTimeTest = clone $dateTimeSameDay;
            if ($dateTime < $dateTimeTest->modify('tomorrow')) {
                $ret = 'Today at '.$dateTime->format('g:ia');
            } elseif ($dateTime < $dateTimeTest->modify('+2 days')) {
                $ret = 'Tomorrow at '.$dateTime->format('g:ia');
            } else {
                $ret = $dateTime->format('F jS');
            }
        } elseif ($diffSec < 60) {
            // within a min
            $ret = 'Just now';
        } elseif ($diffSec < 3600) {
            // within an hour
            $ret = \round(($diffSec)/60) .' min ago';
        } elseif ($diffSec < 3600*6) {
            // within 6 hours
            $hrs = \round(($diffSec)/3600);
            $ret = $hrs.' '.Str::plural($hrs, 'hour').' ago';
        } elseif ($dateTime > $dateTimeSameDay) {             // since midnight so it's today
            $ret = 'Today at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400) {
            $ret = 'Yesterday at '.$dateTime->format('g:ia');
        } elseif ($dateTime->getTimestamp() >= $dateTimeSameDay->getTimestamp() - 86400*7) {
            $ret = $dateTime->format('l \a\t g:ia');
        } elseif ($dateTime->getTimestamp() > \mktime(0, 0, 0, 1, 1)) {// since 1st Jan so it's this year
            $ret = $dateTime->format('F jS');
        } else {                                // last year..
            $ret = $dateTime->format('F jS, Y');
        }
        $ret = \str_replace(' at 12:00am', '', $ret);
        return $ret;
    }