Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a variable contains a date or a date and text?

Tags:

php

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.

like image 297
EnexoOnoma Avatar asked Oct 04 '11 08:10

EnexoOnoma


2 Answers

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

like image 109
JellyBelly Avatar answered Oct 26 '22 23:10

JellyBelly


One approch would be this though it's not perfect.

if (false === strtotime($date)) {
    echo 'invalid date';
else {
    echo 'valid date';
}
like image 37
Alfwed Avatar answered Oct 27 '22 01:10

Alfwed