How to check whether a variable $date
contains a date in the form of 2011-09-20 ? This is to know if the $date
contains any letters in it because I want to use the date()
and gives me error.
try this:
$date = '2011-09-20';
list($y, $m, $d) = explode("-", $date);
if(checkdate($m, $d, $y)){
echo "OK Date";
} else {
echo "BAD Date";
}
demo here: http://codepad.org/3iqrQrey
One approch would be this though it's not perfect.
if (false === strtotime($date)) {
echo 'invalid date';
else {
echo 'valid date';
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With