Form text validation

Posted on 2007-10-21
Last Modified: 2013-12-13
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...
Question by:fincon
    LVL 49

    Expert Comment

    What are the comparison requirements?

    Author Comment

    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.


    Author Comment

    Sorry - The 2nd AND 3rd digit is the month..
    LVL 20

    Assisted Solution

    $str = "703MSZT010853";

    $temp = "";

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

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

    $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";

          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

    Author Comment


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

    PHP Fatal error: Call to undefined function: str_split() in C:\Domains\\wwwroot\lgsn.php on line 24

    Author Comment

    Is there possibly a way of checking this in normal HTML?

    Author Comment

    Also, can it be validated in months instead of years?
    LVL 11

    Accepted Solution

    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
          echo "Warranty expired";
    } else {
          echo "Under warranty";

    Author Comment

    Thank you guys! As alwasy, expert advice for a beginner like me! I appreciate all your time and effort! Thanks

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
    Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to count occurrences of each item in an array.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now