Form text validation

Hi there,

Can you please help me with a php script to compare text entered into a web form to the current date? Date will only be year and month.

Any help would be greatly appreciated...
finconAsked:
Who is Participating?
 
AlexanderREnterprise Web DeveloperCommented:
steelseth12 has a good answer, however fincon unfortunatly is still in php4 where there is no str_split

This should work across all versions and it counts it in months
$str = "203MSZT010853";

//get todays date
$Today = mktime(0,0,0,date("m"),0,date("Y"));

//extract and computer manufacture's date
$ManufacturerYear = substr($str,0,1);
$ManufacturerMonth = substr($str,1,2);
$ManufacturerDate = mktime(0,0,0,$ManufacturerMonth,0,$ManufacturerYear);

//Check difference
//3 years = 36 months * 30 days * 24 hours * 60 min * 60 sec = 93312000 seconds
if(($Today-$ManufacturerDate)>93312000){
      echo "Warranty expired";
} else {
      echo "Under warranty";
}
0
 
RoonaanCommented:
What are the comparison requirements?
0
 
finconAuthor Commented:
Hi Roonan...

I need a user to enter a number like 703MSZT010853 (this is a product serial number), the first three digits is the manufacturing date (1st = year, 2nd = month) and the item has a 3 year warranty. I need to check these numbers with the current date to validate warranty on the unit and return with a message.

Thanks
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

 
finconAuthor Commented:
Sorry - The 2nd AND 3rd digit is the month..
0
 
steelseth12Commented:
$str = "703MSZT010853";

$temp = "";

for($i=0;$i<strlen($str);$i++) {


      if(is_numeric($str[$i])) {
      
      
            $temp .=$str[$i];      
      
      }else{
      
            break;
            
      }
      
}

$temp =  strrev($temp);

$arr = str_split($temp,2);

$month = strrev($arr[0]);

$year = strrev($arr[1]);


$post_date = mktime(0,0,0,$month,1,$year+3);

$difference = intval(($post_date - time())/86400)/356;


if($difference > 0 ) {

      echo "Under Warranty";
      
}else{

      echo "Warranty Expired";

}


It may seem a bit long ... its because you represent year as 7 and not 07. This will also work when you get to 2010
0
 
finconAuthor Commented:
steelseth,

Thank you for your input, however, I get the following error:

PHP Fatal error: Call to undefined function: str_split() in C:\Domains\testcrt.co.za\wwwroot\lgsn.php on line 24
0
 
finconAuthor Commented:
Is there possibly a way of checking this in normal HTML?
0
 
finconAuthor Commented:
Also, can it be validated in months instead of years?
0
 
finconAuthor Commented:
Thank you guys! As alwasy, expert advice for a beginner like me! I appreciate all your time and effort! Thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.