One more date question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
snicolas
Forum Commoner
Posts: 97
Joined: Tue Nov 09, 2004 8:32 am

One more date question

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply