Page 1 of 1

One more date question

Posted: Mon Mar 14, 2005 11:48 am
by snicolas
Hi,

I need to check if a date entered in a <input type="text"> is:
1/ Valid
2/ In the Future (compared from today().

The validation is ok, but I am bit stuck on checking if it's in the future.
My date format is dd/mm/year.

Tx

S.

Posted: Mon Mar 14, 2005 12:32 pm
by Chris Corbyn
I'm sure somebody may correct me but I think you'll need to convert your date to mm/dd/YYYY to be U.S. style.

Next, use strtotime() to turn that into the unix timesatmp as PHP uses this.

The unix timestamp is in seconds so just check it's value is greater than the one date() returns :-)

Sorry I had to use some RegExp's.

Code: Select all

$userDate = "03/05/1983"; //My D.o.B 

$USdate = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$2/$1/$3', $userDate); //Rearrange to US style

$unixDate = strtotime($USdate); //Convert to Unix timestamp in seconds

$timeNow = date(); //Now

if ($timeNow < $unixDate) {
    echo 'OK'; //This is in the future
} else {
    echo 'Not OK'; //This is in the past
}
EDIT: Removed second argument from strtotime() after double checking it's usage in the manual

Posted: Tue Mar 15, 2005 1:01 am
by feyd
checkdate() works well too :) be aware that a user may enter the date in almost any format if it's just a text field. So some special processing is required most often. Drop-downs, or a calendar chooser may be a better idea.