Jump to content

[SOLVED] Leap year


Luodeni

Recommended Posts

I created afunction which works with 3 dropdown menu's to get the date but no matter whether a date is a leapyear or not it always says it's an invalid date

 

here's the code

 

<?php
function validDateCheck( $dobMonth, $dobDay, $dobYear )
{
	$message = "";
	if ( $dobMonth == 2 || $dobMonth == 4 || $dobMonth == 6 || $dobMonth == 9 || $dobMonth == 11)
	{
		if ( $dobDay > 30)
		{
			$message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />";
		}
		elseif ( $dobMonth == 2 && $dobDay > 28 )
		{
			if ( ($dobMonth == 2) && ($dobDay == 29) && ($dobYear % 4 != 0) )
			{
				$message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />";
			}
			elseif ( ($dobMonth == 2) && ($dobDay >= 29) )
			{
				$message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " is not a valid date. Please check and correct it! <br />";
			}
		}
	}
	return $message;
}
?>

Link to comment
Share on other sites

Now, this will most likely not affect your particular application as you are using it for data of birth, but just for educational purposes:

 

That leap year logic is not correct. Leap years do not occur every 4 years - exactly. There are exceptions to that.

 

A year is a leap year if it is divisiable by 4 AND if is NOT divisiable by 100, UNLESS it is also divisable by 400.

 

So every '00 year is not a leap year unless it is also divisable by 400. The year 2000 was a leap year, but hte year 2100 will not be. But, since you are using this for DOBs, the logic of just using divisable by four would cover everything from 1901 to 2099.

Link to comment
Share on other sites

Now, this will most likely not affect your particular application as you are using it for data of birth, but just for educational purposes:

 

That leap year logic is not correct. Leap years do not occur every 4 years - exactly. There are exceptions to that.

 

A year is a leap year if it is divisiable by 4 AND if is NOT divisiable by 100, UNLESS it is also divisable by 400.

 

So every '00 year is not a leap year unless it is also divisable by 400. The year 2000 was a leap year, but hte year 2100 will not be. But, since you are using this for DOBs, the logic of just using divisable by four would cover everything from 1901 to 2099.

 

yes I was actually aware of that but as you already said it's only for birthdays and I don't know anyone who's 109 years old and still working hahah :)

 

thanks to PFMaBiSmAd for the PHP checkdate() function. I ammended the codeto this now. which is much neater

 

<?php
function validDateCheck( $dobMonth, $dobDay, $dobYear )
{
	if (checkdate( $dobMonth, $dobDay, $dobYear) == true )
	{
		$message .= "";
	}
	else 
	{
		$message .= $dobMonth . "-" . $dobDay . "-" . $dobYear . " Is not a valid date, please correct it!";
	}
	return $message;
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.