Checking Date vaildidation Problem

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
User avatar
Slyvampy
Forum Newbie
Posts: 23
Joined: Thu Nov 28, 2002 2:03 am
Location: Yorkshire, England
Contact:

Checking Date vaildidation Problem

Post by Slyvampy »

After sucessfull sorting my previous problem, I must admit I want to improve my code.

At the moment I have 4 if statements checking to see if a date is vaild, I am trying to get it down to one if possible, im sure it is but once again its 05:23am and I have another brain freeze.

This is my previous code:

Code: Select all

<?php
$string_length = strlen($takings_date);

if($string_length == 10)
{
	$format_of_date0 = $takings_date{0};
	$format_of_date1 = $takings_date{1};
	$format_of_date2 = $takings_date{2};
	$format_of_date3 = $takings_date{3};
	$format_of_date4 = $takings_date{4};
	$format_of_date5 = $takings_date{5};
	$format_of_date6 = $takings_date{6};
	$format_of_date7 = $takings_date{7};
	$format_of_date8 = $takings_date{8};
	$format_of_date9 = $takings_date{9};

	$takings_date_day_tmp = "$format_of_date0" . "$format_of_date1";

	$takings_date_month_tmp = "$format_of_date3" . "$format_of_date4";

	if($takings_date_day_tmp > 31)
	{
		  include("error_with_date.php");
	}
	else if($takings_date_day_tmp < 01)
	{
		  include("error_with_date.php");
	}
	else if($takings_date_month_tmp < 01)
	{
		  include("error_with_date.php");
	}
	else if($takings_date_month_tmp > 12)
	{
		  include("error_with_date.php");
	}
	else
	{

	$format_of_date_line = "-";

	if(strpos($format_of_date2, $format_of_date_line) === 0)
	{
		if(strpos($format_of_date5, $format_of_date_line) === 0)
		{

		echo " You have entered the daily takings as the following: - <BR><BR> Amount Taken: <B>£$formatted_amount_taken</B><BR><BR>On the date of : <B>$takings_date</B>";

		}
	}
}

?>
And im trying to use this code, however I know that its a boolean statement.

Code: Select all

<?php
	if($takings_date_day !> 31 && $takings_date_day !< 01 || $takings_date_month !> 12 && $takings_date_month !< 01 || $takings_date_year !>2037 && $takings_date_year !< 1970)
	{

		$date_is_correct = checkdate ($takings_date_month, $takings_date_day, $takings_date_year);

		if($date_is_correct == true)
		{

			$takings_date_timestamp = mktime(0,0,0,$takings_date_month,$takings_date_day,$takings_date_year);
		}
	}

?>
But that still gives me script errors, can anyone shed some light on this situation for me please ?

Cheers,

SteJ.
User avatar
Slyvampy
Forum Newbie
Posts: 23
Joined: Thu Nov 28, 2002 2:03 am
Location: Yorkshire, England
Contact:

Checking Date vaildidation Problem

Post by Slyvampy »

I think i have it, take a look at this:

Code: Select all

<?php
if($takings_date_day < 31 && $takings_date_day > 01 && $takings_date_month < 12 && $takings_date_month > 01 && $takings_date_year < 2037 && $takings_date_year > 1970)
?>
I'm guessing thats right, it seems to work so far, however if anyone has a better idea, please let me know.

Cheers,

SteJ.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you tried the checkdate() function? You then only need to do a little more validation to check that the year is within a valid range.

Mac
Post Reply