Doctrine\ODM\MongoDB\Mapping\Types\DateType::convertToDatabaseValue PHP Method

convertToDatabaseValue() public method

public convertToDatabaseValue ( $value )
    public function convertToDatabaseValue($value)
    {
        if ($value === null) {
            return null;
        }
        $timestamp = false;
        if ($value instanceof \DateTime) {
            $timestamp = $value->getTimestamp();
        } elseif (is_numeric($value)) {
            $timestamp = $value;
        } elseif (is_string($value)) {
            $timestamp = strtotime($value);
        }
        // Could not convert date to timestamp so store ISO 8601 formatted date instead
        if ($timestamp === false) {
            $date = new \DateTime($value);
            return $date->format('c');
        } else {
            $value = $timestamp;
        }
        return new \MongoDate($value);
    }

Usage Example

 public function convertToDatabaseValue($value)
 {
     if ($value === null) {
         return null;
     }
     if (!is_array($value)) {
         throw new CustomTypeException('Array expected.');
     }
     $converter = new DateType();
     $value = array_map(function ($date) use($converter) {
         return $converter->convertToDatabaseValue($date);
     }, array_values($value));
     return $value;
 }