# Class yii\validators\DateValidator

Inheritance | yii\validators\DateValidator » yii\validators\Validator » yii\base\Component » yii\base\BaseObject
Implements | yii\base\Configurable
Subclasses | yii\mongodb\validators\MongoDateValidator
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/validators/DateValidator.php

DateValidator verifies if the attribute represents a date, time or datetime in a proper $format.

It can also parse internationalized dates in a specific $locale like e.g. `12 мая 2014` when $format is configured to use a time pattern in ICU format.

It is further possible to limit the date within a certain range using $min and $max.

Additional to validating the date it can also export the parsed timestamp as a machine readable format which can be configured using $timestampAttribute. For values that include time information (not date-only values) also the time zone will be adjusted. The time zone of the input value is assumed to be the one specified by the $timeZone property and the target timeZone will be UTC when $timestampAttributeFormat is `null` (exporting as UNIX timestamp) or $timestampAttributeTimeZone otherwise. If you want to avoid the time zone conversion, make sure that $timeZone and $timestampAttributeTimeZone are the same.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$attributeNames | array | Attribute names. This property is read-only. | yii\validators\Validator
$attributes | array|string | Attributes to be validated by this validator. | yii\validators\Validator
$behaviors | yii\base\Behavior[] | List of behaviors attached to this component. This property is read-only. | yii\base\Component
$builtInValidators | array | List of built-in validators (name => class or configuration) | yii\validators\Validator
$defaultTimeZone | string | The default timezone used for parsing when no time parts are provided in the format. | yii\validators\DateValidator
$enableClientValidation | boolean | Whether to enable client-side validation for this validator. | yii\validators\Validator
$except | array|string | Scenarios that the validator should not be applied to. | yii\validators\Validator
$format | string | The date format that the value being validated should follow. | yii\validators\DateValidator
$isEmpty | callable | A PHP callable that replaces the default implementation of isEmpty(). | yii\validators\Validator
$locale | string | The locale ID that is used to localize the date parsing. | yii\validators\DateValidator
$max | integer|string | Upper limit of the date. | yii\validators\DateValidator
$maxString | string | User friendly value of upper limit to display in the error message. | yii\validators\DateValidator
$message | string | The user-defined error message. | yii\validators\Validator
$min | integer|string | Lower limit of the date. | yii\validators\DateValidator
$minString | string | User friendly value of lower limit to display in the error message. | yii\validators\DateValidator
$on | array|string | Scenarios that the validator can be applied to. | yii\validators\Validator
$skipOnEmpty | boolean | Whether this validation rule should be skipped if the attribute value is null or an empty string. | yii\validators\Validator
$skipOnError | boolean | Whether this validation rule should be skipped if the attribute being validated already has some validation error according to some previous rules. | yii\validators\Validator
$strictDateFormat | boolean | Set this parameter to true if you need strict date format validation (e.g. only such dates pass validation for the following format 'yyyy-MM-dd': '0011-03-25', '2019-04-30' etc. | yii\validators\DateValidator
$timeZone | string | The timezone to use for parsing date and time values. | yii\validators\DateValidator
$timestampAttribute | string | The name of the attribute to receive the parsing result. | yii\validators\DateValidator
$timestampAttributeFormat | string | The format to use when populating the $timestampAttribute. | yii\validators\DateValidator
$timestampAttributeTimeZone | string | The timezone to use when populating the $timestampAttribute with $timestampAttributeFormat. | yii\validators\DateValidator
$tooBig | string | User-defined error message used when the value is bigger than $max. | yii\validators\DateValidator
$tooSmall | string | User-defined error message used when the value is smaller than $min. | yii\validators\DateValidator
$type | string | The type of the validator. | yii\validators\DateValidator
$validationAttributes | array | List of attribute names. This property is read-only. | yii\validators\Validator
$when | callable | A PHP callable whose return value determines whether this validator should be applied. | yii\validators\Validator
$whenClient | string | A JavaScript function name whose return value determines whether this validator should be applied on the client-side. | yii\validators\Validator

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
__call() | Calls the named method which is not a class method. | yii\base\Component
__clone() | This method is called after the object is created by cloning an existing one. | yii\base\Component
__construct() | Constructor. | yii\base\BaseObject
__get() | Returns the value of a component property. | yii\base\Component
__isset() | Checks if a property is set, i.e. defined and not null. | yii\base\Component
__set() | Sets the value of a component property. | yii\base\Component
__unset() | Sets a component property to be null. | yii\base\Component
addError() | Adds an error about the specified attribute to the model object. | yii\validators\Validator
attachBehavior() | Attaches a behavior to this component. | yii\base\Component
attachBehaviors() | Attaches a list of behaviors to the component. | yii\base\Component
behaviors() | Returns a list of behaviors that this component should behave as. | yii\base\Component
canGetProperty() | Returns a value indicating whether a property can be read. | yii\base\Component
canSetProperty() | Returns a value indicating whether a property can be set. | yii\base\Component
className() | Returns the fully qualified name of this class. | yii\base\BaseObject
clientValidateAttribute() | Returns the JavaScript needed for performing client-side validation. | yii\validators\Validator
createValidator() | Creates a validator object. | yii\validators\Validator
detachBehavior() | Detaches a behavior from the component. | yii\base\Component
detachBehaviors() | Detaches all behaviors from the component. | yii\base\Component
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\base\Component
getAttributeNames() | Returns cleaned attribute names without the `!` character at the beginning. | yii\validators\Validator
getBehavior() | Returns the named behavior object. | yii\base\Component
getBehaviors() | Returns all behaviors attached to this component. | yii\base\Component
getClientOptions() | Returns the client-side validation options. | yii\validators\Validator
getValidationAttributes() | Returns a list of attributes this validator applies to. | yii\validators\Validator
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\base\Component
hasMethod() | Returns a value indicating whether a method is defined. | yii\base\Component
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\base\Component
init() | Initializes the object. | yii\validators\DateValidator
isActive() | Returns a value indicating whether the validator is active for the given scenario and attribute. | yii\validators\Validator
isEmpty() | Checks if the given value is empty. | yii\validators\Validator
off() | Detaches an existing event handler from this component. | yii\base\Component
on() | Attaches an event handler to an event. | yii\base\Component
trigger() | Triggers an event. | yii\base\Component
validate() | Validates a given value. | yii\validators\Validator
validateAttribute() | Validates a single attribute. | yii\validators\DateValidator
validateAttributes() | Validates the specified object. | yii\validators\Validator

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
formatMessage() | Formats a mesage using the I18N, or simple strtr if `\Yii::$app` is not available. | yii\validators\Validator
parseDateValue() | Parses date string into UNIX timestamp. | yii\validators\DateValidator
validateValue() | Validates a value. | yii\validators\DateValidator

## Constants

Hide inherited constants

Constant | Value | Description | Defined By
--- | --- | --- | ---
TYPE_DATE | 'date' | Constant for specifying the validation $type as a date value, used for validation with intl short format. | yii\validators\DateValidator
TYPE_DATETIME | 'datetime' | Constant for specifying the validation $type as a datetime value, used for validation with intl short format. | yii\validators\DateValidator
TYPE_TIME | 'time' | Constant for specifying the validation $type as a time value, used for validation with intl short format. | yii\validators\DateValidator

## Property Details

$defaultTimeZone public property (available since version 2.0.39)

The default timezone used for parsing when no time parts are provided in the format. See $timestampAttributeTimeZone for more description.

public string $defaultTimeZone = 'UTC'
$format public property

The date format that the value being validated should follow. This can be a date time pattern as described in the ICU manual.

Alternatively this can be a string prefixed with `php:` representing a format that can be recognized by the PHP Datetime class. Please refer to https://secure.php.net/manual/en/datetime.createfromformat.php on supported formats.

If this property is not set, the default value will be obtained from `Yii::$app->formatter->dateFormat`, see yii\i18n\Formatter::$dateFormat for details. Since version 2.0.8 the default value will be determined from different formats of the formatter class, dependent on the value of $type:

- if type is TYPE_DATE, the default value will be taken from yii\i18n\Formatter::$dateFormat,
- if type is TYPE_DATETIME, it will be taken from yii\i18n\Formatter::$datetimeFormat,
- and if type is TYPE_TIME, it will be yii\i18n\Formatter::$timeFormat.

Here are some example values:

    'MM/dd/yyyy' // date in ICU format
    'php:m/d/Y' // the same date in PHP format
    'MM/dd/yyyy HH:mm' // not only dates but also times can be validated

Note: the underlying date parsers being used vary dependent on the format. If you use the ICU format and the PHP intl extension is installed, the IntlDateFormatter is used to parse the input value. In all other cases the PHP DateTime class is used. The IntlDateFormatter has the advantage that it can parse international dates like `12. Mai 2015` or `12 мая 2014`, while the PHP parser is limited to English only. The PHP parser however is more strict about the input format as it will not accept `12.05.05` for the format `php:d.m.Y`, but the IntlDateFormatter will accept it for the format `dd.MM.yyyy`. If you need to use the IntlDateFormatter you can avoid this problem by specifying a minimum date.

public string $format = null
$locale public property

The locale ID that is used to localize the date parsing. This is only effective when the PHP intl extension is installed. If not set, the locale of the formatter will be used. See also yii\i18n\Formatter::$locale.

public string $locale = null
$max public property (available since version 2.0.4)

Upper limit of the date. Defaults to null, meaning no upper limit. This can be a unix timestamp or a string representing a date time value. If this property is a string, $format will be used to parse it.

See also $tooBig for the customized message used when the date is too big.

public integer|string $max = null
$maxString public property (available since version 2.0.4)

User friendly value of upper limit to display in the error message. If this property is null, the value of $max will be used (before parsing).

public string $maxString = null
$min public property (available since version 2.0.4)

Lower limit of the date. Defaults to null, meaning no lower limit. This can be a unix timestamp or a string representing a date time value. If this property is a string, $format will be used to parse it.

See also $tooSmall for the customized message used when the date is too small.

public integer|string $min = null
$minString public property (available since version 2.0.4)

User friendly value of lower limit to display in the error message. If this property is null, the value of $min will be used (before parsing).

public string $minString = null
$strictDateFormat public property (available since version 2.0.22)

Set this parameter to true if you need strict date format validation (e.g. only such dates pass validation for the following format 'yyyy-MM-dd': '0011-03-25', '2019-04-30' etc. and not '18-05-15', '2017-Mar-14' etc. which pass validation if this parameter is set to false)

public boolean $strictDateFormat = false
$timeZone public property

The timezone to use for parsing date and time values. This can be any value that may be passed to date_default_timezone_set() e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. Refer to the php manual for available timezones. If this property is not set, yii\base\Application::$timeZone will be used.

public string $timeZone = null
$timestampAttribute public property

The name of the attribute to receive the parsing result. When this property is not null and the validation is successful, the named attribute will receive the parsing result.

This can be the same attribute as the one being validated. If this is the case, the original value will be overwritten with the timestamp value after successful validation.

Note, that when using this property, the input value will be converted to a unix timestamp, which by definition is in default UTC time zone, so a conversion from the input time zone to the default one will be performed. If you want to change the default time zone, set the $defaultTimeZone property. When defining $timestampAttributeFormat you can further control the conversion by setting $timestampAttributeTimeZone to a different value than `'UTC'`.

See also:

- $timestampAttributeFormat
- $timestampAttributeTimeZone

public string $timestampAttribute = null
$timestampAttributeFormat public property (available since version 2.0.4)

The format to use when populating the $timestampAttribute. The format can be specified in the same way as for $format.

If not set, $timestampAttribute will receive a UNIX timestamp. If $timestampAttribute is not set, this property will be ignored.

See also:

- $format
- $timestampAttribute

public string $timestampAttributeFormat = null
$timestampAttributeTimeZone public property (available since version 2.0.4)

The timezone to use when populating the $timestampAttribute with $timestampAttributeFormat. Defaults to `UTC`.

This can be any value that may be passed to date_default_timezone_set() e.g. `UTC`, `Europe/Berlin` or `America/Chicago`. Refer to the php manual for available timezones.

If $timestampAttributeFormat is not set, this property will be ignored.

See also $timestampAttributeFormat.

public string $timestampAttributeTimeZone = 'UTC'
$tooBig public property (available since version 2.0.4)

User-defined error message used when the value is bigger than $max.

public string $tooBig = null
$tooSmall public property (available since version 2.0.4)

User-defined error message used when the value is smaller than $min.

public string $tooSmall = null
$type public property (available since version 2.0.8)

The type of the validator. Indicates, whether a date, time or datetime value should be validated. This property influences the default value of $format and also sets the correct behavior when $format is one of the intl short formats, `short`, `medium`, `long`, or `full`.

This is only effective when the PHP intl extension is installed.

This property can be set to the following values:

- TYPE_DATE - (default) for validating date values only, that means only values that do not include a time range are valid.
- TYPE_DATETIME - for validating datetime values, that contain a date part as well as a time part.
- TYPE_TIME - for validating time values, that contain no date information.

public string $type = self::TYPE_DATE

## Method Details

init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

parseDateValue() protected method

Parses date string into UNIX timestamp.

protected integer|false parseDateValue ( $value )
$value | mixed | String representing date
return | integer|false | A UNIX timestamp or `false` on failure.

validateAttribute() public method

Validates a single attribute.

Child classes must implement this method to provide the actual validation logic.

public void validateAttribute ( $model, $attribute )
$model | yii\base\Model | The data model to be validated
$attribute | string | The name of the attribute to be validated.

validateValue() protected method

Validates a value.

A validator class can implement this method to support data validation out of the context of a data model.

protected array|null validateValue ( $value )
$value | mixed | The data value to be validated.
return | array|null | The error message and the array of parameters to be inserted into the error message. ```php if (!$valid) {
    return [$this->message, [
        'param1' => $this->param1,
        'formattedLimit' => Yii::$app->formatter->asShortSize($this->getSizeLimit()),
        'mimeTypes' => implode(', ', $this->mimeTypes),
        'param4' => 'etc...',
    ]];
} return null; ``` for this example `message` template can contain `{param1}`, `{formattedLimit}`, `{mimeTypes}`, `{param4}` Null should be returned if the data is valid.
throws | yii\base\NotSupportedException | if the validator does not supporting data validation without a model