Page 1 of 1
A little date help here...
Posted: Sat Dec 03, 2005 7:05 pm
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.
Posted: Sat Dec 03, 2005 7:07 pm
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
Posted: Sat Dec 03, 2005 7:09 pm
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!';
}
Posted: Sat Dec 03, 2005 7:10 pm
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
Re: A little date help here...
Posted: Sat Dec 03, 2005 7:12 pm
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!
Posted: Sat Dec 03, 2005 7:15 pm
by John Cartwright
fyi, I edited my last post with an improved version just incase you didn't notice.
Posted: Sat Dec 03, 2005 7:16 pm
by Serbaside
I think he might want it dynamic so it matches the current day, not a hardcoded value
Posted: Sat Dec 03, 2005 7:19 pm
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...
Posted: Sat Dec 03, 2005 7:19 pm
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.
Posted: Sat Dec 03, 2005 7:29 pm
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!!
Posted: Sat Dec 03, 2005 7:31 pm
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.