Page 1 of 1

checkdate() doesn't work

Posted: Sun Apr 11, 2010 2:06 pm
by agibsonsw
Hello.
I've set the default time zone to 'Europe/London' and am attempting to validate form entry for a date of birth:

if (!checkdate((int) $_POST['Month'],(int) $_POST['Day'],(int) $_POST['Year'])) {
$errors['Year'] = "That is not a valid date.";
But this seems to accept any date, including 29 Feb 2010, 31 June 2010, etc.

Any idea why this is not working as it should? Thanks, Andy.

Re: checkdate() doesn't work

Posted: Sun Apr 11, 2010 3:42 pm
by requinix
Doesn't accept for me.

What's the HTML and exactly what did you type into the fields?

Re: checkdate() doesn't work

Posted: Sun Apr 11, 2010 3:58 pm
by agibsonsw
The Day, Month and Year are three select options. Andy.

Actually I've discovered I have a problem with the indexes for these select options. I'm using range(1,31) to populate a days array but $_POST['Day'] returns the
index of item (which is 0 for 1, etc.). I have a similar problem with the years.

I shall re-phrase my question. How do I retrieve the value from a select option rather than it's index? Thanks, Andy.

Re: checkdate() doesn't work

Posted: Sun Apr 11, 2010 4:19 pm
by requinix
Again, would help to have the HTML and PHP.

But the month works?
Look at what you did for the month and copy it for the day and year.

Re: checkdate() doesn't work

Posted: Sun Apr 11, 2010 4:34 pm
by agibsonsw
I've got it to read the values (rather than the keys) so that the day and year work but now the $_POST['Month'] returns 'April' as text.

<?php
date_default_timezone_set('Europe/London'); // required when using date/time functions.
include ('includes/header.html');
include ('includes/form_ctls.php');
$days = range(1,31);
$months = array(1=>'January','February','March','April','May','June','July','August','September','October','November','December');
$years = range(1920,(int) date("Y"));

Is there a way that I can refer to my $months array to convert 'April' to 4? Thanks, Andy.

Here is the HTML from my header.html if it helps:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>My Form</title>
<style type="text/css">
.Error {color:red; font-size:smaller;}
</style>
</head>
<body>

Re: checkdate() doesn't work

Posted: Sun Apr 11, 2010 4:52 pm
by agibsonsw
Sorry to waste time but I've solved it!

Code: Select all

(int) array_search($_POST['Month'],$months) // converts 'April' to 4 and now checkdate() is working!
I'm still studying, learning :)

Thanks again, Andy.