Convert DateTime instance into strings.

@param string|int|\DateTime|\DateTimeImmutable $value The value to convert.
@param \Cake\Database\Driver $driver The driver instance to convert with.
@return string|null
555733
public function toDatabase($value, Driver $driver)
    {
        if ($value === null || is_string($value)) {
            return $value;
        }
        if (is_int($value)) {
            $class = $this->_className;
            $value = new $class('@' . $value);
        }

        $format = (array)$this->_format;

        if ($this->dbTimezone !== null
            && $this->dbTimezone->getName() !== $value->getTimezone()->getName()
        ) {
            if (!$value instanceof DateTimeImmutable) {
                $value = clone $value;
            }
            $value = $value->setTimezone($this->dbTimezone);
        }

        return $value->format(array_shift($format));
    }
523749
public function castToDateTime($value)
    {
        if (is_int($value)) {
            $dt = new \DateTime();
            $dt->setTimestamp($value);
        } else {
            $dt = new \DateTime($value);
        }

        return $dt;
    }
990753
public function processModifier($type, $value)
	{
		switch (gettype($value)) {
			case 'string':
				switch ($type) {
					case 'b':
						if (in_array($value, ['TRUE', 'FALSE'])) {
							return $value === 'TRUE';
						}
						break;
					case 're':
						return $this->driver->convertToDriver($value, IDriver::TYPE_REGEXP);
				}
			case 'integer':
			case 'double':
			case 'boolean':
				switch ($type) {
					case 'any':
					case 's':
						return (string) $value;
					case 'i':
						return (int) ($value + 0);
					case 'f':
						return (float) $value;
					case 'dt':
						return $this->driver->convertToDriver(is_numeric($value) ? (int) $value : strtotime((string) $value), IDriver::TYPE_DATETIME);
					case 'ts':
						return $this->driver->convertToDriver(is_numeric($value) ? (int) $value : strtotime((string) $value), IDriver::TYPE_TIMESTAMP);
					case 'b':
						return (bool) $value;
					case 'oid':
						return $this->driver->convertToDriver((string) $value, IDriver::TYPE_OID);
				}
				break;

			case 'NULL':
				return NULL;

			case 'object':
				if ($value instanceof \DateTimeInterface) {
					switch ($type) {
						case 'dt':
							return $this->driver->convertToDriver((int) $value->format('U'), IDriver::TYPE_DATETIME);
						case 'ts':
							return $this->driver->convertToDriver((int) $value->format('U'), IDriver::TYPE_TIMESTAMP);
						case 'any':
						case 's':
							return $value->format('Y-m-d H:i:s');
						case 'i':
							return (int) $value->format('U');
						case 'f':
							return (float) $value->format('U');
					}
				}

				if (($return = $this->driver->convertToDriver($value, $type)) !== $value) {
					return $return;
				}

				if (method_exists($value, '__toString')) {
					$str_value = (string) $value;
					switch ($type) {
						case 'any':
						case 's':
							return $str_value;
						case 'i':
							return (int) ($str_value + 0);
						case 'f':
							return (float) $str_value;
						case 'b':
							return (bool) $str_value;
					}
				}
				break;

			case 'array':
				if (substr($type, -1) === ']' || $type === 'any') {
					$this->processArray(trim($type, '[]'), $value);
					return $value;
				}
				break;

			default:
				return $value;
		}
	}
1218274
public function toStatement($value, Driver $driver)
    {
        if ($value === null) {
            return PDO::PARAM_NULL;
        }

        return PDO::PARAM_LOB;
    }
1282555
public function quote($value)
    {
        if ($value === null) {
            return 'NULL';
        }
        if ($value instanceof DateTimeInterface) {
            return $this->pdo->quote($value->format('Y-m-d H:i:s'));
        }
        if (is_array($value)) {
            return implode(',', array_map([$this, 'quote'], $value));
        }
        if (is_bool($value)) {
            return $this->pdo->quote($value ? 1 : 0);
        }

        return $this->pdo->quote($value);
    }
503506
protected function interpretAsDate($value)
    {
        if (null === $value || 0 === $value || '' === $value) {
            return null;
        }

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

        if (is_string($value)) {
            return new Carbon($value);
        }

        if (is_numeric($value)) {
            return Carbon::createFromTimestamp($value);
        }

        throw new UnexpectedValueException("Expecting null or date");
    }
570158
public function stringify($value): string
    {
        if ($value instanceof \DateTimeInterface) {
            return $value->format('Y-m-d');
        }

        if (is_array($value)) {
            return self::ARRAY_VALUE;
        }

        if (is_object($value) && !method_exists($value, '__tostring')) {
            return self::OBJECT_VALUE;
        }

        return (string)$value;
    }
1233346
private function convertValue($value)
    {
        if (empty($value)) {
            return null;
        }

        if (\is_array($value)) {
            return $value;
        }

        switch ($this->fieldType) {
            case 'radio':
                return $value;
            case 'checkbox':
                $delimiter = (false !== \stripos($value, "\t")) ? "\t" : ',';

                return StringUtil::trimsplit($delimiter, $value);
            default:
        }
        throw new \RuntimeException('Unknown field type encountered: ' . $this->fieldType);
    }
927064
public function createDateTime($value)
    {
        $date = null;

        if(is_string($value)) {
            $date = new \DateTime($value);
        }

        if(is_int($value)) {
            $date = new \DateTime();
            $date->setTimestamp($value);
        }

        return $date;
    }
371462
protected function parseAsDateTime($value)
    {
        if (empty($value) && !is_numeric($value)) {
            return null;
        }

        if (is_string($value)) {
            try {
                $value = new DateTime($value);
            } catch (Exception $e) {
                throw new InvalidArgumentException(sprintf(
                    'Invalid date/time value: %s',
                    $e->getMessage()
                ), $e->getCode(), $e);
            }
        }

        if (!$value instanceof DateTimeInterface) {
            throw new InvalidArgumentException(
                'Invalid date/time value. Must be a date/time string or an implementation of DateTimeInterface.'
            );
        }

        return $value;
    }
1105914
static public function datetime($value=null, $format=null)
    {
        $value = static::toUnixtime($value);
        if (!$format) {
            $value = strftime('%Y-%m-%d %H:%M:%S', $value);
        } else {
            if (strpos($format, '%') === false) {
                $format = preg_replace('/([a-z])/i', '%\\1', $format);
            }
            $value = strftime($format, $value);
        }
        return $value;
    }
1213614
public static function stringToDateTime($string, DateTimeZone $timezone = null, $null = null)
    {
        return ValueHelper::isDateTime($string) ? new \DateTime($string, $timezone) : $null;
    }
215527
private static function tostring($value) : string
    {
        if (!isset($value)) {
            return 'NULL';
        }
        if ($value === true) {
            return 'true';
        }
        if ($value === false) {
            return 'false';
        }
        if (is_numeric($value)) {
            return $value;
        }
        if (is_string($value)) {
            return "'$value'";
        }
        if (is_array($value)) {
            $out = '[';
            $i = 0;
            foreach ($value as $key => $entry) {
                if ($i) {
                    $out .= ', ';
                }
                $out .= $key.' => '.self::tostring($entry);
                $i++;
            }
            $out .= ']';
            return $out;
        }
        if (is_object($value)) {
            if (method_exists($value, '__toString')) {
                return "$value";
            } else {
                return get_class($value);
            }
        }
    }
373443
public function serialize($value): string
    {
        if ($value instanceof Carbon) {
            return $value->toDateTimeString();
        }

        return $this
            ->tryParsingDateTime($value, InvariantViolation::class)
            ->toDateTimeString();
    }
613105
public static function parseValue($value)
    {
        if ($value instanceof DateTimeInterface) {
            $value = $value->format('Y-m-d H:i:s');
        } elseif ($value instanceof DateTimeProperty) {
            $value = $value->storageVal($value->val());
        } elseif (is_string($value)) {
            $str = strtolower($value);
            if ($str === 'true') {
                $value = true;
            } elseif ($str === 'false') {
                $value = false;
            }
        }

        return $value;
    }
251886
public function datetime($name = null, $value = null) 
	{
		Argument::i()
			->test(1, 'string', 'null')
			->test(2, 'scalar', 'null');
			
		$field = Datetime::i();
		
		if(!is_null($name)) {
			$field->setName($name);
		}
		
		if(!is_null($value)) {
			$field->setValue($value);
		}
		
		return $field;
	}
476636
protected static function formatValue($value)
    {
        if ($value instanceof \DateTime) {
            $value = $value->format(\DateTime::ISO8601);
        } elseif (\is_bool($value)) {
            $value = $value ? 'True' : 'False';
        } elseif (\is_array($value) || $value instanceof \IteratorAggregate) {
            $itValue = $value instanceof \IteratorAggregate ? $value->getIterator() : $value;
            $value = [];
            foreach ($itValue as $key => $arrayValue) {
                $value[$key] = static::formatValue($arrayValue);
            }
            $value = implode("\n  ", $value);
        }

        return (string) $value;
    }