On an HTML form that my users fill in, I have a section for for them to enter a date. This is done through three listboxes, one for day (from 01 to 31), one for the month (01 to 12) and another for the year (2005 to 2010).

Of course the user could select an invalid date, such as 30th february 2006, so I wanted to use checkdate to check that the date is valid before the date is stored in the mysql database.

My theory was to convert the date to a timestamp, and use a while loop to increment the date by 86400 seconds until a valid date was found.

However, I was playing around with some code and found that when converting an invalid date (like 30th feb 2006) to a time stamp, it actually comes out as 2nd march 2006, so my above theory for the code is not going to work.

if(!checkdate($_POST['productStockDateM'], $_POST['productStockDateD'], $_POST['productStockDateY']))
{
	$productStockDate = strtotime($_POST['productStockDateM'].'/'.$_POST['productStockDateD'].'/'.$_POST['productStockDateY']); 
	$productStockDate = $productStockDate + 86400;
	echo strftime ( "%d-%m-%Y", $productStockDate );
}

Can someone suggest a method for checking a date is valid, and if not valid, setting the date as the next valid date?

    I've solved this with the following code, if anyone can think of a better method or see any problems, then let me know:

    function check_format_date($yyyy, $mm, $dd)
    {
    	while(!checkdate($mm, $dd, $yyyy))
    	{
    		if($dd!= 31) 
    		{
    			$dd = $dd +1;
    		}
    		elseif($mm != 12) 
    		{
    			$dd = "01";
    			$mm = $mm +1;
    		}
    		else
    		{
    			$dd = "01";
    			$mm= "01";
    			$yyyy = $yyyy +1;
    		}
    	}
    	$result = $yyyy.'-'.$mm.'-'.$dd ;
    	return $result;
    }
    
      Write a Reply...