A little date help here...
Moderator: General Moderators
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
A little date help here...
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.
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.
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
Couldn't you use if statements to check against current time?
Look at this page http://us3.php.net/date
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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
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 > time()) {
echo 'Your date is in the futur!';
}
else {
//do whatever
}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.
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
Re: A little date help here...
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...
}- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- seodevhead
- Forum Regular
- Posts: 705
- Joined: Sat Oct 08, 2005 8:18 pm
- Location: Windermere, FL
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact: