A little date help here...

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
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

A little date help here...

Post by seodevhead »

I have a form where users enter 3 fields

1) Month
2) Day
3) Year

in this format => mm / dd / yy

Keep in mind that each of the 3 are seperate text fields.

My question is... Once the user submits a date, how can I make sure that the date he entered is not in the future? Any date the user entered on my form should either be 'TODAY' or in the past. I am having difficulty coming up with some sort of solution to test whether the date they entered is in the future. Any help would be GREATLY appreciated! Thanks.
Serbaside
Forum Newbie
Posts: 5
Joined: Sat Dec 03, 2005 6:49 pm

Post by Serbaside »

Does the day, month, and year each have thier own variable?

Couldn't you use if statements to check against current time?
Look at this page http://us3.php.net/date
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

One solution would be to have some drop down menus dynamically generated to a point in the past. Although there is a chance that the user may run a post request to your page with whatever results he has in mind.. if thats the case try something like

Code: Select all

$pastDate = strtotime($month.'/'.$day.'/'.$year);

if ($pastDate > time()) {
   echo 'Your date is in the futur!';
}
else {
   //do whatever
}
I am assuming all the data entered has already been validated that they are the correct values, ie. months is not over 12, year is not 5 digits.. etc.

edit | after looking at the manual came up with this

Code: Select all

$pastDate = strtotime($month.'/'.$day.'/'.$year);

if (($pastDate != FALSE) && $pastDate < time()) {
   
	echo $pastDate;
	//do whatever
}
else {
   echo 'Your date is in the futur!';
}
Last edited by John Cartwright on Sat Dec 03, 2005 7:19 pm, edited 3 times in total.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Yes, the day, month and year are all seperate variables. And each variable is the two-digit representation. So when the form is submitted, the vars are:

$_POST['day'] ie. 03
$_POST['month'] ie. 12
$_POST['year'] ie. 05
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Re: A little date help here...

Post by foobar »

Code: Select all

/* Theoretical values */
$month = 12;
$day = 23;
$year = 2009; //<-- We better do something about this bad boy!

/* Create a timestamp from the date given */
$time = strtotime("{$year}-{$month}-{$day}");

if (time() < $time) { //Oopsie daisy...
  echo "Time in the future!";
}
else { //Everything worked fine...
  //Do some stuff...
}
[edit] Damn, beat me to it, JCart!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

fyi, I edited my last post with an improved version just incase you didn't notice.
Serbaside
Forum Newbie
Posts: 5
Joined: Sat Dec 03, 2005 6:49 pm

Post by Serbaside »

I think he might want it dynamic so it matches the current day, not a hardcoded value
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Serbaside wrote:I think he might want it dynamic so it matches the current day, not a hardcoded value
Which hardcoded value? If you're refering to my post, read the comment above the month, day, year variables...
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Serbaside wrote:I think he might want it dynamic so it matches the current day, not a hardcoded value
I believe we understood that, those were just example values.
User avatar
seodevhead
Forum Regular
Posts: 705
Joined: Sat Oct 08, 2005 8:18 pm
Location: Windermere, FL

Post by seodevhead »

Jcart and foobar,

BRAVO!!! AND THANKS A MILLION!!! I don't know what I would do if it weren't for helpful and knowledgeable folks like yourself. I used JCart's code and it works great. Thanks again so much!!
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Sorry I forgot to explain in my last post, the check for != FALSE will assure that $pastDate is infact a valid date.
Post Reply