PHP Class yii\validators\DateValidator

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.
Since: 2.0
Author: Qiang Xue ([email protected])
Author: Carsten Brandt ([email protected])
Inheritance: extends Validator
Datei anzeigen Open project: yiisoft/yii2 Class Usage Examples

Public Properties

Property Type Description
$format 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 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: php '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 [[min|minimum date]].
$locale 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 [[\yii\base\Application::formatter|formatter]] will be used. See also [[\yii\i18n\Formatter::locale]].
$max 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.
$maxString 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).
$min 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.
$minString 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).
$timeZone 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.
$timestampAttribute 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 UTC, so a conversion from the [[$timeZone|input time zone]] to UTC will be performed. When defining [[$timestampAttributeFormat]] you can control the conversion by setting [[$timestampAttributeTimeZone]] to a different value than 'UTC'.
$timestampAttributeFormat 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.
$timestampAttributeTimeZone the timezone to use when populating the [[timestampAttribute]]. 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.
$tooBig user-defined error message used when the value is bigger than [[max]].
$tooSmall user-defined error message used when the value is smaller than [[min]].
$type 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 Methods

Method Description
init ( )
validateAttribute ( $model, $attribute )

Protected Methods

Method Description
parseDateValue ( string $value ) : integer | false Parses date string into UNIX timestamp
validateValue ( $value )

Private Methods

Method Description
formatTimestamp ( integer $timestamp, string $format ) : string Formats a timestamp using the specified format
parseDateValueFormat ( string $value, string $format ) : integer | false Parses date string into UNIX timestamp
parseDateValueIntl ( string $value, string $format ) : integer | boolean Parses a date value using the IntlDateFormatter::parse()
parseDateValuePHP ( string $value, string $format ) : integer | boolean Parses a date value using the DateTime::createFromFormat()

Method Details

init() public method

public init ( )

parseDateValue() protected method

Parses date string into UNIX timestamp
protected parseDateValue ( string $value ) : integer | false
$value string string representing date
return integer | false a UNIX timestamp or `false` on failure.

validateAttribute() public method

public validateAttribute ( $model, $attribute )

validateValue() protected method

protected validateValue ( $value )

Property Details

$format public_oe 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 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: php '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 [[min|minimum date]].
public $format

$locale public_oe 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 [[\yii\base\Application::formatter|formatter]] will be used. See also [[\yii\i18n\Formatter::locale]].
public $locale

$max public_oe property

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.
Since: 2.0.4
public $max

$maxString public_oe property

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).
Since: 2.0.4
public $maxString

$min public_oe property

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.
Since: 2.0.4
public $min

$minString public_oe property

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).
Since: 2.0.4
public $minString

$timeZone public_oe 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 $timeZone

$timestampAttribute public_oe 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 UTC, so a conversion from the [[$timeZone|input time zone]] to UTC will be performed. When defining [[$timestampAttributeFormat]] you can control the conversion by setting [[$timestampAttributeTimeZone]] to a different value than 'UTC'.
See also: timestampAttributeFormat
See also: timestampAttributeTimeZone
public $timestampAttribute

$timestampAttributeFormat public_oe property

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
See also: timestampAttribute
Since: 2.0.4
public $timestampAttributeFormat

$timestampAttributeTimeZone public_oe property

the timezone to use when populating the [[timestampAttribute]]. 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
Since: 2.0.4
public $timestampAttributeTimeZone

$tooBig public_oe property

user-defined error message used when the value is bigger than [[max]].
Since: 2.0.4
public $tooBig

$tooSmall public_oe property

user-defined error message used when the value is smaller than [[min]].
Since: 2.0.4
public $tooSmall

$type public_oe property

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.
Since: 2.0.8
public $type