date validation in php – Date Validation as Text Format in PHP

date validation in php – How to Validate Date String in PHP also The checkdate() function is used to validate a Gregorian date. The checkDateFormat() function checks whether the given string is a valid date or not using PHP code with php check if date is bigger than today.

date validation in php

Checking a valid date entered by user in PHP. The checkDateFormat() function checks whether the given string is a valid date using PHP and php check if date is bigger than today.

PHP | checkdate() Function

function checkDateFormat($date, $format = 'Y-m-d'){
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) === $date;
}

Don’t miss : laravel date validation

Call the checkDateFormat() function

// Returns false
var_dump(checkDateFormat('2022-14-01')); 
var_dump(checkDateFormat('2022-14-01'));
var_dump(checkDateFormat('2022-10-32'));
var_dump(checkDateFormat('2017-5-25'));

// Returns true
var_dump(checkDateFormat('2022-12-01'));
var_dump(checkDateFormat('1970-11-28'));

using format (Y-n-j).

// Returns true
var_dump(checkDateFormat('2022-2-5', 'Y-n-j'));

php check if date is bigger than today

 $last_dt) {
    echo 'greater than';
}else{
    echo 'Less than';
}

php check if date is bigger than today

 '2022-01-02') {
    echo 'greater than';
}else{
    echo 'Less than';
}

I hope you get an idea about date validation in php.
I would like to have feedback on my infinityknow.com.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.

Leave a Comment