src/Koldy/Validator.php
protected function validateDate($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        $format = $args[0] ?? null;

        if ($format === '') {
            throw new ValidatorConfigException("Invalid format specified in 'date' validator for parameter {$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if ($format === null) {
            // no format, use simple strtotime()
            if (strtotime($value) === false) {
                return Message::getMessage(Message::DATE, [
                    'param' => $parameter,
                    'value' => $value
                ]);
            }
        } else {
            $dateTime = \DateTime::createFromFormat($format, $value);
            if ($dateTime === false || $dateTime->format($format) !== $value) {
                return Message::getMessage(Message::DATE, [
                    'param' => $parameter,
                    'value' => $value
                ]);
            }
        }

        return null;
    }
protected function validateDate($value, string $parameter, array $args = [], array $rules = null, array $context = null): ?string
    {
        $format = $args[0] ?? null;

        if ($format === '') {
            throw new ValidatorConfigException("Invalid format specified in 'date' validator for parameter {$parameter}");
        }

        //$value = $this->getValue($parameter);

        if ($value === null) {
            return null;
        }

        if (!is_scalar($value)) {
            return Message::getMessage(Message::PRIMITIVE, [
                'param' => $parameter
            ]);
        }

        $value = trim((string)$value);

        if ($value === '') {
            return null;
        }

        if ($format === null) {
            // no format, use simple strtotime()
            if (strtotime($value) === false) {
                return Message::getMessage(Message::DATE, [
                    'param' => $parameter,
                    'value' => $value
                ]);
            }
        } else {
            $dateTime = \DateTime::createFromFormat($format, $value);
            if ($dateTime === false || $dateTime->format($format) !== $value) {
                return Message::getMessage(Message::DATE, [
                    'param' => $parameter,
                    'value' => $value
                ]);
            }
        }

        return null;
    }
protected function validateDate ( $ value , string $ parameter , array $ args = [ ] , array $ rules = null , array $ context = null ) : ? string { $ format = $ args [ 0 ] ?? null ; if ( $ format === '' ) { throw new ValidatorConfigException ( "Invalid format specified in 'date' validator for parameter {$parameter}" ) ; } //$value = $this->getValue($parameter); if ( $ value === null ) { return null ; } if ( ! is_scalar ( $ value ) ) { return Message :: getMessage ( Message :: PRIMITIVE , [ 'param' => $ parameter ] ) ; } $ value = trim ( ( string ) $ value ) ; if ( $ value === '' ) { return null ; } if ( $ format === null ) { // no format, use simple strtotime() if ( strtotime ( $ value ) === false ) { return Message :: getMessage ( Message :: DATE , [ 'param' => $ parameter , 'value' => $ value ] ) ; } } else { $ dateTime = \ DateTime :: createFromFormat ( $ format , $ value ) ; if ( $ dateTime === false || $ dateTime -> format ( $ format ) !== $ value ) { return Message :: getMessage ( Message :: DATE , [ 'param' => $ parameter , 'value' => $ value ] ) ; } } return null ; }
Validate if given value is properly formatted date

@param mixed $value
@param string $parameter
@param array $args
@param array $rules other rules

@return null|string
@throws ValidatorConfigException
@example 'param' => 'date'
@example 'param' => 'date:Y-m-d'
Validate if given value is properly formatted date