Php проверить валидность даты
- PHP Tutorial
- PHP | Introduction
- PHP Full Form
- How to set PHP development environment in windows ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | Coding Standards
- PHP | Basic Syntax
- PHP | Variables
- PHP echo and print
- PHP | Data Types
- PHP | Strings
- PHP | Constants
- PHP | Magic Constants
- PHP | Decision Making
- PHP | Loops
- PHP | Superglobals
- PHP | Regular Expressions
- PHP Examples
- Describe PHP Include and Require
- PHP | Basics of File Handling
- PHP | fopen( ) (Function open file or URL)
- PHP fread( ) Function
- PHP fclose( ) Function
- PHP | fwrite( ) Function
- PHP | Uploading File
- PHP Cookies
- PHP | Sessions
- PHP Filter and Filter Constant
- Implementing callback in PHP
- Exception Handling in PHP
- PHP | Arrays
- PHP array() Function
- PHP | array_change_key_case() Function
- PHP array_chunk() Function
- PHP | array_column() Function
- PHP array_combine() Function
- PHP array_count_values() Function
- PHP array_diff() function
- PHP array_diff_assoc() Function
- PHP array_diff_key() Function
- PHP array_diff_uassoc() Function
- PHP array_diff_ukey() Function
- PHP array_fill() function
- PHP array_fill_keys() Function
- PHP Array Functions Complete Reference
- PHP Date and Time
- PHP | checkdate() Function
- PHP | date_create(), date_format(), date_add() Functions
- PHP | date_create_from_format() Function
- PHP | date_date_set() Function
- PHP | date_default_timezone_get() Function
- PHP | date_default_timezone_set() Function
- PHP | date_diff() Function
- PHP | date_get_last_errors() Function
- PHP | date_isodate_set() Function
- PHP | date_modify() Function
- PHP | date_offset_get() Function
- PHP | date_parse_from_format() Function
- PHP | date_parse() Function
- PHP | date_sub() Function
- PHP basename( ) Function
- PHP chgrp( ) Function
- PHP chmod( ) Function
- PHP chown( ) Function
- PHP copy( ) Function
- PHP dirname( ) Function
- PHP disk_free_space( ) Function
- PHP disk_total_space( ) Function
- PHP feof( ) Function
- PHP fflush( ) Function
- PHP | fgetc( ) Function
- PHP | fgets( ) Function
- PHP | fgetss( ) Function
- PHP | file_exists( ) Function
- PHP file_get_contents() Function
- PHP Filesystem Functions Complete Reference
- PHP | ftp_alloc() function
- PHP | ftp_chdir() function
- PHP | ftp_close() function
- PHP | ftp_connect() function
- PHP | ftp_delete() function
- PHP | ftp_exec() function
- PHP | ftp_get() function
- PHP | ftp_put() function
- PHP | ftp_get_option() function
- PHP | ftp_login() function
- PHP | ftp_mdtm() Function
- PHP | ftp_mkdir() function
- PHP | Ds\Map allocate() Function
- PHP | Ds\Map apply() Function
- PHP | Ds\Map capacity() Function
- PHP | DS\Map clear() Function
- PHP | Ds\Map __construct() Function
- PHP Ds\Map copy() Function
- PHP | Ds\Map count() Function
- PHP | Ds\Map diff() Function
- PHP | Ds\Map filter() Function
- PHP | Ds\Map first() Function
- PHP Ds\Map Functions Complete Reference
- SplDoublyLinkedList in PHP
- PHP SplDoublyLinkedList add() Function
- PHP SplDoublyLinkedList bottom() Function
- PHP SplDoublyLinkedList count() function
- PHP SplFixedArray count() Function
- PHP SplFixedArray current() Function
- PHP SplFixedArray getSize() Function
- PHP SplFixedArray key() Function
- PHP SplObjectStorage addAll() Function
- PHP SplObjectStorage attach() Function
- PHP SplObjectStorage contains() Function
- PHP SplObjectStorage count() Function
- PHP SplQueue::__construct() Function
- PHP SPL Data structures Complete Reference
- PHP Tutorial
- PHP | Introduction
- PHP Full Form
- How to set PHP development environment in windows ?
- LAMP installation and important PHP configurations on Ubuntu
- PHP | Coding Standards
- PHP | Basic Syntax
- PHP | Variables
- PHP echo and print
- PHP | Data Types
- PHP | Strings
- PHP | Constants
- PHP | Magic Constants
- PHP | Decision Making
- PHP | Loops
- PHP | Superglobals
- PHP | Regular Expressions
- PHP Examples
- Describe PHP Include and Require
- PHP | Basics of File Handling
- PHP | fopen( ) (Function open file or URL)
- PHP fread( ) Function
- PHP fclose( ) Function
- PHP | fwrite( ) Function
- PHP | Uploading File
- PHP Cookies
- PHP | Sessions
- PHP Filter and Filter Constant
- Implementing callback in PHP
- Exception Handling in PHP
- PHP | Arrays
- PHP array() Function
- PHP | array_change_key_case() Function
- PHP array_chunk() Function
- PHP | array_column() Function
- PHP array_combine() Function
- PHP array_count_values() Function
- PHP array_diff() function
- PHP array_diff_assoc() Function
- PHP array_diff_key() Function
- PHP array_diff_uassoc() Function
- PHP array_diff_ukey() Function
- PHP array_fill() function
- PHP array_fill_keys() Function
- PHP Array Functions Complete Reference
- PHP Date and Time
- PHP | checkdate() Function
- PHP | date_create(), date_format(), date_add() Functions
- PHP | date_create_from_format() Function
- PHP | date_date_set() Function
- PHP | date_default_timezone_get() Function
- PHP | date_default_timezone_set() Function
- PHP | date_diff() Function
- PHP | date_get_last_errors() Function
- PHP | date_isodate_set() Function
- PHP | date_modify() Function
- PHP | date_offset_get() Function
- PHP | date_parse_from_format() Function
- PHP | date_parse() Function
- PHP | date_sub() Function
- PHP basename( ) Function
- PHP chgrp( ) Function
- PHP chmod( ) Function
- PHP chown( ) Function
- PHP copy( ) Function
- PHP dirname( ) Function
- PHP disk_free_space( ) Function
- PHP disk_total_space( ) Function
- PHP feof( ) Function
- PHP fflush( ) Function
- PHP | fgetc( ) Function
- PHP | fgets( ) Function
- PHP | fgetss( ) Function
- PHP | file_exists( ) Function
- PHP file_get_contents() Function
- PHP Filesystem Functions Complete Reference
- PHP | ftp_alloc() function
- PHP | ftp_chdir() function
- PHP | ftp_close() function
- PHP | ftp_connect() function
- PHP | ftp_delete() function
- PHP | ftp_exec() function
- PHP | ftp_get() function
- PHP | ftp_put() function
- PHP | ftp_get_option() function
- PHP | ftp_login() function
- PHP | ftp_mdtm() Function
- PHP | ftp_mkdir() function
- PHP | Ds\Map allocate() Function
- PHP | Ds\Map apply() Function
- PHP | Ds\Map capacity() Function
- PHP | DS\Map clear() Function
- PHP | Ds\Map __construct() Function
- PHP Ds\Map copy() Function
- PHP | Ds\Map count() Function
- PHP | Ds\Map diff() Function
- PHP | Ds\Map filter() Function
- PHP | Ds\Map first() Function
- PHP Ds\Map Functions Complete Reference
- SplDoublyLinkedList in PHP
- PHP SplDoublyLinkedList add() Function
- PHP SplDoublyLinkedList bottom() Function
- PHP SplDoublyLinkedList count() function
- PHP SplFixedArray count() Function
- PHP SplFixedArray current() Function
- PHP SplFixedArray getSize() Function
- PHP SplFixedArray key() Function
- PHP SplObjectStorage addAll() Function
- PHP SplObjectStorage attach() Function
- PHP SplObjectStorage contains() Function
- PHP SplObjectStorage count() Function
- PHP SplQueue::__construct() Function
- PHP SPL Data structures Complete Reference
checkdate
Проверяет корректность даты по переданным аргументам. Дата считается корректной, если все параметры принимают допустимые значения.
Список параметров
Месяц. Принимает значения от 1 до 12 включительно.
День. Принимает значения, допустимые для указанного месяца month . При этом учитывается, является ли год year високосным.
Год. Принимает значения от 1 до 32767 включительно.
Возвращаемые значения
Возвращает true , если проверяемая дата является корректной, или false в противном случае.
Примеры
Пример #1 Пример использования функции checkdate()
Результат выполнения данного примера:
Смотрите также
- mktime() — Возвращает метку времени Unix для заданной даты
- strtotime() — Преобразует текстовое представление даты на английском языке в метку времени Unix
User Contributed Notes 1 note
With DateTime you can make the shortest date&time validator for all formats.
function validateDate ( $date , $format = ‘Y-m-d H:i:s’ )
$d = DateTime :: createFromFormat ( $format , $date );
return $d && $d -> format ( $format ) == $date ;
>
var_dump ( validateDate ( ‘2012-02-28 12:12:12’ )); # true
var_dump ( validateDate ( ‘2012-02-30 12:12:12’ )); # false
var_dump ( validateDate ( ‘2012-02-28’ , ‘Y-m-d’ )); # true
var_dump ( validateDate ( ’28/02/2012′ , ‘d/m/Y’ )); # true
var_dump ( validateDate ( ’30/02/2012′ , ‘d/m/Y’ )); # false
var_dump ( validateDate ( ’14:50′ , ‘H:i’ )); # true
var_dump ( validateDate ( ’14:77′ , ‘H:i’ )); # false
var_dump ( validateDate ( 14 , ‘H’ )); # true
var_dump ( validateDate ( ’14’ , ‘H’ )); # true
var_dump ( validateDate ( ‘2012-02-28T12:12:12+02:00’ , ‘Y-m-d\TH:i:sP’ )); # true
# or
var_dump ( validateDate ( ‘2012-02-28T12:12:12+02:00’ , DateTime :: ATOM )); # true
var_dump ( validateDate ( ‘Tue, 28 Feb 2012 12:12:12 +0200’ , ‘D, d M Y H:i:s O’ )); # true
# or
var_dump ( validateDate ( ‘Tue, 28 Feb 2012 12:12:12 +0200’ , DateTime :: RSS )); # true
var_dump ( validateDate ( ‘Tue, 27 Feb 2012 12:12:12 +0200’ , DateTime :: RSS )); # false
# .
- Функции даты и времени
- checkdate
- date_add
- date_create_from_format
- date_create_immutable_from_format
- date_create_immutable
- date_create
- date_date_set
- date_default_timezone_get
- date_default_timezone_set
- date_diff
- date_format
- date_get_last_errors
- date_interval_create_from_date_string
- date_interval_format
- date_isodate_set
- date_modify
- date_offset_get
- date_parse_from_format
- date_parse
- date_sub
- date_sun_info
- date_sunrise
- date_sunset
- date_time_set
- date_timestamp_get
- date_timestamp_set
- date_timezone_get
- date_timezone_set
- date
- getdate
- gettimeofday
- gmdate
- gmmktime
- gmstrftime
- idate
- localtime
- microtime
- mktime
- strftime
- strptime
- strtotime
- time
- timezone_abbreviations_list
- timezone_identifiers_list
- timezone_location_get
- timezone_name_from_abbr
- timezone_name_get
- timezone_offset_get
- timezone_open
- timezone_transitions_get
- timezone_version_get
How to Validate Date String in PHP
Before taking any action with date input, it always a great idea to validate the date string. Date validation helps to check whether the provided string is a valid date format. Using the DateTime class you can easily check if the date string is valid in PHP.
In the example code snippet, we will show you how to validate a date string in PHP. It is very useful for server-side validation of the date input using PHP.
The validateDate() function checks whether the given string is a valid date using PHP. It uses PHP DateTime class to validate date based on the specified format. This function returns TRUE if date string is valid, otherwise FALSE.
- $date – Required. The date string to validate.
- $format – Optional. The format of the date string.
function validateDate($date, $format = 'Y-m-d') $d = DateTime::createFromFormat($format, $date);
return $d && $d->format($format) === $date;
>Call the validateDate() function and pass the date string in the first parameter.
// Returns false
var_dump(validateDate('2018-14-01'));
var_dump(validateDate('20122-14-01'));
var_dump(validateDate('2018-10-32'));
var_dump(validateDate('2017-5-25'));
// Returns true
var_dump(validateDate('2018-12-01'));
var_dump(validateDate('1970-11-28'));By default, the format is set to Y-m-d . If you want to allow day and month without leading zeroes, specify the respective format ( Y-n-j ).
// Returns true
var_dump(validateDate('2018-2-5', 'Y-n-j'));PHP checkdate() Function
The checkdate() function is used to validate a Gregorian date.
Syntax
Parameter Values
Parameter Description month Required. Specifies the month as a number between 1 and 12 day Required. Specifies the day as a number between 1 and 31 year Required. Specifies the year as a number between 1 and 32767 Technical Details
COLOR PICKER
Report Error
If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:
Thank You For Helping Us!
Your message has been sent to W3Schools.
Top Tutorials
Top References
Top Examples
Get Certified
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.