Jump to content

[SOLVED] validate date input,


gazever

Recommended Posts

Really simple question,

 

someone is using my webpage to book villas, they are entering dates as date from, etc.

 

my GET looks like this,

 

availability_booking.php?villa=newvilla1&date_from=2007-12-14

 

however as input cannot be trusted, I get all villa codes out of sql and put in an array and then check the GET[villa] is inarray before checking the availability.

 

after this I then check for booked dates,

 

id there a simple way of checking to see if the date from day is in the future or not before checking the database,

 

many Thanks

Link to comment
https://forums.phpfreaks.com/topic/73213-solved-validate-date-input/
Share on other sites

 

try this

 

*untested

 

<?php
if ($date($_GET['date_from']))
{
echo "valid";
}else{
echo "invalid";
}

function validDate($date)
{
if (preg_match('%((?:19|20)[0-9]{2})[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])%s', $date, $regs)) {
return checkdate($regs[2], $regs[3], $regs[1]);
} else {
return false;
}
}

?>

Hi

 

I have a function which supposedly checks if its a valid date or not,

 

Sorry guess I didn't make the original question very clear,

 

What I'm trying to decide is if the date is in the past or not, i.e. yesterday, 3 weeks ago etc.

 

here is my date checking function:

function date_checker($date) {

$bits_of_date = explode("-", $date);

 

$dd = $bits_of_date[2];

$mm = $bits_of_date[1];

$yy = $bits_of_date[0];

 

    if (is_numeric($yy) && is_numeric($mm) && is_numeric($dd))

    {

$tocheck = date('Y-n-j', mktime(0, 0, 0, $mm, $dd, $yy));

$tocheck = explode("-", $tocheck);

return checkdate($tocheck[1],$tocheck[2],$tocheck[0]);

 

    }

    return false;         

}


<?php
if (validDate($_GET['date_from']))
{
echo "valid";
}else{
echo "invalid";
}

function validDate($date)
{
if (preg_match('%((?:19|20)[0-9]{2})[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])%s', $date, $reg
s))
{
	if(checkdate($regs[2], $regs[3], $regs[1]))
	{
		$tTime = mktime(0, 0, 0, $regs[2], $regs[3], $regs[1]);
		return ($tTime < time());
	}
}
return false;
}

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.