checkdate() doesn't work

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
agibsonsw
Forum Newbie
Posts: 8
Joined: Sun Apr 11, 2010 1:58 pm

checkdate() doesn't work

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: checkdate() doesn't work

Post by requinix »

Doesn't accept for me.

What's the HTML and exactly what did you type into the fields?
agibsonsw
Forum Newbie
Posts: 8
Joined: Sun Apr 11, 2010 1:58 pm

Re: checkdate() doesn't work

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: checkdate() doesn't work

Post 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.
agibsonsw
Forum Newbie
Posts: 8
Joined: Sun Apr 11, 2010 1:58 pm

Re: checkdate() doesn't work

Post 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>
agibsonsw
Forum Newbie
Posts: 8
Joined: Sun Apr 11, 2010 1:58 pm

Re: checkdate() doesn't work

Post 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.
Post Reply