Convert a DateTime to a storable string.

@param  \DateTime|int  $value
@return string
762491
public function fromDateTime($value)
    {
        $format = $this->getDateFormat();

        // If the value is already a DateTime instance, we will just skip the rest of
        // these checks since they will be a waste of time, and hinder performance
        // when checking the field. We will just return the DateTime right away.
        if ($value instanceof DateTime)
        {
            //
        }

        // If the value is totally numeric, we will assume it is a UNIX timestamp and
        // format the date as such. Once we have the date in DateTime form we will
        // format it according to the proper format for the database connection.
        elseif (is_numeric($value))
        {
            $value = Carbon::createFromTimestamp($value);
        }

        // If the value is in simple year, month, day format, we will format it using
        // that setup. This is for simple "date" fields which do not have hours on
        // the field. This conveniently picks up those dates and format correct.
        elseif (preg_match('/^(\d{4})-(\d{2})-(\d{2})$/', $value))
        {
            $value = Carbon::createFromFormat('Y-m-d', $value)->startOfDay();
        }

        // If this value is some other type of string, we'll create the DateTime with
        // the format used by the database connection. Once we get the instance we
        // can return back the finally formatted DateTime instances to the devs.
        elseif ( ! $value instanceof DateTime)
        {
            $value = Carbon::createFromFormat($format, $value);
        }

        return $value->format($format);
    }
668444
private static function stringify($value)
    {
        if (is_bool($value)) {
            return $value ? '<TRUE>' : '<FALSE>';
        }

        if (is_scalar($value)) {
            $val = (string)$value;

            if (strlen($val) > 100) {
                $val = substr($val, 0, 97) . '...';
            }

            return $val;
        }

        if (is_array($value)) {
            return '<ARRAY>';
        }

        if (is_object($value)) {
            return get_class($value);
        }

        if (is_resource($value)) {
            return '<RESOURCE>';
        }

        if ($value === NULL) {
            return '<NULL>';
        }

        return 'unknown';
    }
1128373
public static function stringify($value)
    {
        $string = '';
        if (is_scalar($value)) {
            // integer, float, string, boolean
            $string = (string)$value;
        } elseif (is_resource($value)) {
            // resource
            $string = '[resource]';
        } else {
            // array, object, null, callable
            $string = json_encode($value);
        }
        return $string;
    }
309515
public function fromDateTime($value)
    {
        return empty($value) ? $value : $this->asDateTime($value)
            ->timezone($this->getTimezone())
            ->format($this->getFormat());
    }
1297534
public function marshal($value)
    {
        $date = parent::marshal($value);
        if ($date instanceof DateTime) {
            $date->setTime(0, 0, 0);
        }

        return $date;
    }
396144
public function dateTime($value, $type = NULL)
    {
        $apend = "";
        switch ($type) {
            case 'time':
                    $apend = 'time';
                break;
            case 'timestamp':
                    $apend = 'timestamp';
                break;
            case 'date':
                    $apend = 'date';
                break;
            case 'datetime':
                    $apend = 'datetime';
                break;
            case 'year':
                    $apend = 'year(4)';
                break;
            default:
                    $apend = 'timestamp';
                break;
        }
        return $value . " " . $apend . " NOT NULL";
    }
265743
public static function getStringValueAsCode($value): string
    {
        if ($value instanceof \DateTime) {
            return "new \DateTime('".$value->format(self::DEFAULT_DATETIME_FORMAT)."')";
        } elseif (is_bool($value)) {
            return $value ? 'true' : 'false';
        } elseif (is_array($value)) {
            return '['.implode(', ', array_map('self::getStringValueAsCode', array_filter($value))).']';
        } elseif (is_null($value)) {
            return 'null';
        } elseif (is_int($value) || is_float($value)) {
            return (string) $value;
        }

        return "'".(string) $value."'";
    }
616902
public function filter($value)
    {
        try {
            $result = $this->normalizeDateTime($value);
        } catch (\Exception $e) {
            // DateTime threw an exception, an invalid date string was provided
            throw new Exception\InvalidArgumentException('Invalid date string provided', $e->getCode(), $e);
        }

        if ($result === false) {
            return $value;
        }

        return $result;
    }
513511
public function transform($value)
    {
        if ( $value ) {
            $from = '';
            if ( $value->getFrom() ) {
                $from = $value->getFrom()->format('Y-m-d');
            }
            $to = '';
            if ( $value->getTo() ) {
                $to = $value->getTo()->format('Y-m-d');
            }
            return sprintf(
                '%s%s%s',
                $from,
                $this->dateSeparator,
                $to
            );
        }
    }
878912
public function buildTime($value)
    {
        if ($value instanceof \DateTime) {
            return $value->format(static::TIME_FORMAT);
        } elseif (is_int($value)) {
            $date = new \DateTime();
            $date->setTimestamp($value);

            return $date->format(static::TIME_FORMAT);
        } elseif (is_string($value)) {
            $date = new \DateTime($value);

            return $date->format(static::TIME_FORMAT);
        }
        throw new \InvalidArgumentException('Time value should be int, string or \DateTime');
    }
636271
private function stringEscaped($value)
    {
        $value = $this->string($value);
        $value = str_replace(self::$strReplaceFrom, self::$strReplaceTo, $value);

        return $value;
    }
856002
public static function datetime($value): bool
    {
        if ($value instanceof \DateTime) {
            return true;
        } elseif (strtotime($value) !== false) {
            return true;
        }

        return false;
    }
588720
public static function castToDateTime($value)
    {
        if ($value instanceof \DateTime) {
            return clone $value;
        }
        if (is_numeric($value)) {
            $date = new \DateTime();
            $date->setTimestamp((int)$value);
        } elseif (is_string($value) || (is_object($value) && method_exists($value, '__toString'))) {
            $date = date_create((string)$value);
            if ($date === false) {
                $date = null;
            }
        } else {
            $date = null;
        }
        return $date;
    }
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;
    }
283766
private function stringifyValue($value)
    {
        switch (true) {
            case $value === null:
                $value = 'NULL';
                break;
            case ($value instanceof \DateTime):
                $value = $value->format(DATE_RFC822);
                break;
            case ($value instanceof StringableInterface):
                $value = $value->toString();
                break;
            case ($value instanceof ArrayableInterface):
                $value = sprintf('Arrayable::%s (%s)', (new \ReflectionObject($value))->getShortName(),
                    $this->stringifyValue($value->toArray()));
                break;
            case is_scalar($value):
            case method_exists($value, '__toString'):
                $value = (string) $value;
                break;
            case is_object($value):
                $value = sprintf('Object (%s)', get_class($value));
                break;
            case is_array($value):
                $value = sprintf('Array (%s)', implode(', ', array_map([$this, 'stringifyValue'], $value)));
                break;
        }
        
        return $value;
    }
1366510
private function argValue($value) {
        if (is_bool($value)) {
            return (int)$value;
        } elseif ($value instanceof \DateTimeInterface) {
            return $value->format(self::MYSQL_DATE_FORMAT);
        } else {
            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;
    }