php check if format is date

Solutions on MaxInterview for php check if format is date by the best coders in the world

showing results for - "php check if format is date"
Gaynor
25 Jan 2021
1function validateDate($date, $format = 'Y-m-d')
2{
3    $d = DateTime::createFromFormat($format, $date);
4    // The Y ( 4 digits year ) returns TRUE for any integer with any number of digits so changing the comparison from == to === fixes the issue.
5    return $d && $d->format($format) === $date;
6}
7
8Test cases:
9var_dump(validateDate('2013-13-01'));  // false
10var_dump(validateDate('20132-13-01')); // false
11var_dump(validateDate('2013-11-32'));  // false
12var_dump(validateDate('2012-2-25'));   // false
13var_dump(validateDate('2013-12-01'));  // true
14var_dump(validateDate('1970-12-01'));  // true
15var_dump(validateDate('2012-02-29'));  // true
16var_dump(validateDate('2012', 'Y'));   // true
17var_dump(validateDate('12012', 'Y'));  // false
Michele
18 Nov 2018
1if (preg_match("/\d{4}\-\d{2}-\d{2}/", $date)) {
2    echo 'true';
3} else {
4    echo 'false';
5}
Matthias
26 Feb 2019
1function isRealDate($date) { 
2    if (false === strtotime($date)) { 
3        return false;
4    } 
5    list($year, $month, $day) = explode('-', $date); 
6    return checkdate($month, $day, $year);
7}
8
queries leading to this page
php check valid date formatinput entered is a valid date or not in phpcheck provide string is valid date format in phpphp regex date php date format string regexphp check format dateregex date format phpcheck the date format in php verify date format phpphp check if all date existsdateformat validation php regular expressiondate checking in phpphp check if valid datecheck date format is valid in phphow to check format of date in phphow to check user enter a date or not text using phphow to check entered value is date or not in phpphp check if date is in right formatvalid date format phphow to check if valid date format in phpcheck format date string phpverify dates phpphp check if datephp isvaliddatehow to check if string is valid date in phpcheck if valid date format phpcheck if string is valid date phpphp determine date formatphp date ifcheck if date is valid in phpdate in a form and validate in phppresent date in php input fieldphp date check formatchecking format date phpcheck if string is date format phphow to i checked if inputted date is correct phpcheck format date phpphp test preg match online yymmddnumberphp check if is datephp if is datephp check date is validcheck date format in php regexcheck date is in format phpvalidate date phpphp check date format from stringcheck if valid date phpphp time check formatphp check if date typedate format validation in phpphp check date with regexcheck if date is date phpphp validate dateformatcheck if string can be date phphow to verify date in phpcheck if input is date phphow to check date format using phpdate regex in php how to validate date in phpcheck if date matches format phpcheck if valid date and time in phphow to check valid date format in phpregex replace date c 23php dateformat check filter varcheck is date phpphp check if date matches formatcheck 3c and 3e date in phpcheck if string is date phpphp check if is date is validphp check the date in correct fromat or notif data type date format in phpphp regex for datephp check if its a valid datephp check if is datetimecheck if the return type is date format in phpphp if string is datephp check if date is this monthphp check if date format is correctphp check input date formattest if string is date phpregex validate datetime format phpcheck date value phpphp check date string formatcheck date format php d 2fm 2fyvalidate dates formats phpdate check in phpphp check if value is date or notcheck date format in phpchecking if input is date in phpphp check date formathow to check user input is date or not in phpphp check if date formathow to check the date in phpphp check if date filed is validdate checker validation php functionphp verify datephp regex check date formatcheck if a input is a date in phpphp if datepattern for date for pattern check phpchecking if input is dat in phpdate backards regex phpdisplay if valid date phpcheck input is date phpphp check if value is a datecheck date format phpphp check datetime formatcheck if date in valid phpphp check if is date validcheck if php date formatphp check value isdatephp check if date respects formatfunction check date format regex to check if string is date phpphp check format of datephp dateformmat checkphp check if value is formatted for datetimephp detect date formatphp validate datephp check for date formatregex date phphow to check the entered input is a date or not in phpcheck the date format frim string phpcheck date time in correct format in phpphp check if date is in pastregex for valid month in phpcheck if date is correct format phpphp check if is date formatverify if date time parameter is correct phpcheck date format in excel phpphp check if date is validregex date y m dhow to check format in phpphp regex match datephp format date but check fi date is correct formatphp regex validate date dd 2fmm 2fyyyycheck if datetime format phpchk date format in if phpphp test if string is datephp check if a string is a valid datedate php format checkcheck if is date phpphp check string date formatphp datetime check formatcheck if valid date in phpphp check if date from form is correctphp check datecheck date input phpregex format date phpwrite a php function to check for date formatphp check if value is time formatphp is datephp check date if it matches formathow to check if a value is a date format with phpdata format validatedate phphow to check user input date or not in phpphp check if format is datephp check if is a datephp detect a date formatphp check if date is in correct formatcheck if date is in a particular format phpregex check date format phpphp check date format validvalidate year phpverify date string phpregex to check date format phphow to check 2f or in date format using phpregex validate date time format phpphp check if date validphp check date formatephp check if date has ita formatcheck if date format phpphp how to check date formatphp check if input is datephp check valid dateis my date valid phpcheck valid date format in phpphp check to see if date formatphp check if format is date