PHP Класс 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.
С версии: 2.0
Автор: Qiang Xue ([email protected])
Автор: Carsten Brandt ([email protected])
Наследование: extends Validator
Показать файл Открыть проект Примеры использования класса

Открытые свойства

Свойство Тип Описание
$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.

Открытые методы

Метод Описание
init ( )
validateAttribute ( $model, $attribute )

Защищенные методы

Метод Описание
parseDateValue ( string $value ) : integer | false Parses date string into UNIX timestamp
validateValue ( $value )

Приватные методы

Метод Описание
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()

Описание методов

init() публичный Метод

public init ( )

parseDateValue() защищенный Метод

Parses date string into UNIX timestamp
protected parseDateValue ( string $value ) : integer | false
$value string string representing date
Результат integer | false a UNIX timestamp or `false` on failure.

validateAttribute() публичный Метод

public validateAttribute ( $model, $attribute )

validateValue() защищенный Метод

protected validateValue ( $value )

Описание свойств

$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]].
public $format

$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]].
public $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.
См. также: tooBig for the customized message used when the date is too big.
С версии: 2.0.4
public $max

$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).
С версии: 2.0.4
public $maxString

$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.
См. также: tooSmall for the customized message used when the date is too small.
С версии: 2.0.4
public $min

$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).
С версии: 2.0.4
public $minString

$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.
public $timeZone

$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
См. также: timestampAttributeTimeZone
public $timestampAttribute

$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.
См. также: format
См. также: timestampAttribute
С версии: 2.0.4
public $timestampAttributeFormat

$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.
См. также: timestampAttributeFormat
С версии: 2.0.4
public $timestampAttributeTimeZone

$tooBig публичное свойство

user-defined error message used when the value is bigger than [[max]].
С версии: 2.0.4
public $tooBig

$tooSmall публичное свойство

user-defined error message used when the value is smaller than [[min]].
С версии: 2.0.4
public $tooSmall

$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.
С версии: 2.0.8
public $type