Re: Function to check a valid date
| From: | Ben Ramsey | Date: | Sat, 20 Mar 2004 22:36:31 +0000 |
| Subject: | Re: Function to check a valid date | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-181050@lists.php.net to get a copy of this message | ||
Your likely to find many ways to do this and many functions with many lines of code that do many variations of what you want. Can I use the word "many" many more times?
Such is the way with programming.
Here's my take on it. Pull all your values into one long string:
$date_string = "{$_POST["week_day"]}, {$_POST["month"]} {$_POST["day"]}, {$_POST["year"]}";
Now, you have a string in the format "Saturday, March 20, 2004".
Now, use strtotime() to figure out whether it's a valid date:
if (($timestamp = strtotime($date_string)) === -1) {
// Not a valid date; do some error reporting here} else {
// It is a valid date, $timestamp is now a valid Unix
// timestamp of $date_string; use it like below:
echo date("m/d/Y", $timestamp);
}
See also http://us4.php.net/strtotime
Chris Bruce wrote:
Hello all, I was going to build a function to check whether a selected date exists, but thought that I would see if anyone has done this type of thing already which would indeed save me some time. I have a form where the users select the day of the week, month, date and year from 4 pulldowns. (Saturday, March 20, 2004). I need to have a function that will check to see if that is indeed a real date. Does this sound familiar to anyone? Thanks a bunch :) -- Chris Bruce chris@idextrus.com Idextrus E-Business Architects http://www.idextrus.com 3282 Wilmar Cres. Mississauga, ON L5L4B2 CA 905.828.9189 **************************************************************** This e-mail and its contents are privileged, confidential and subject to copyright. If you are not the intended recipient, please delete this e-mail immediately. Any unauthorized use or disclosure of the information herein is prohibited.-- Regards, Ben Ramsey http://benramsey.com http://www.phpcommunity.org/wiki/People/BenRamsey