Date validation issue

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ndsoumah@gmail.com

    #1

    Date validation issue

    Hi guys

    I'm having a hard time trying to validate some dates.
    I have a form that accept dates from users in this format (YYYY-mm-dd).
    I've been looking at the available datetime functions and I'm still
    clueless as to how they can help me achieve my goals.

    what I need to do id check is:
    1- convert the input string into date first. At this point I couldn't
    find any srtTOdate type of functions. then I tried doing it the hard
    way...by using "checkdate" function. I could split the string fine
    (using "explode") but then I was stuck because I couldn't find any
    function to convert a string into Integer!

    2- I need to check if the date is valid


    Could somebody please help me out?

    Thanks

  • Manuel Lemos

    #2
    Re: Date validation issue

    Hello,

    on 06/27/2005 01:04 AM ndsoumah@gmail. com said the following:[color=blue]
    > I'm having a hard time trying to validate some dates.
    > I have a form that accept dates from users in this format (YYYY-mm-dd).
    > I've been looking at the available datetime functions and I'm still
    > clueless as to how they can help me achieve my goals.
    >
    > what I need to do id check is:
    > 1- convert the input string into date first. At this point I couldn't
    > find any srtTOdate type of functions. then I tried doing it the hard
    > way...by using "checkdate" function. I could split the string fine
    > (using "explode") but then I was stuck because I couldn't find any
    > function to convert a string into Integer!
    >
    > 2- I need to check if the date is valid[/color]

    You may want to take a look at this popular forms generation and
    validation class that comes with custom input plug-in that lets the user
    enter a date that is validated either on the client side with Javascript
    and the server side with the class PHP code.

    Not only it can validate the date, but also impose optional requirements
    to accept dates after a given start date or before a end date.

    http://www.phpclasses.org/formsgeneration

    Here is a screenshot of an example date form:

    http://www.phpclasses.org/browse/file/8245.html


    --

    Regards,
    Manuel Lemos

    PHP Classes - Free ready to use OOP components written in PHP
    http://www.phpclasses.org/

    PHP Reviews - Reviews of PHP books and other products
    http://www.phpclasses.org/reviews/

    Metastorage - Data object relational mapping layer generator

    Comment

    • chotiwallah

      #3
      Re: Date validation issue



      ndsoumah@gmail. com wrote:[color=blue]
      > Hi guys
      >
      > I'm having a hard time trying to validate some dates.
      > I have a form that accept dates from users in this format (YYYY-mm-dd).
      > I've been looking at the available datetime functions and I'm still
      > clueless as to how they can help me achieve my goals.
      >
      > what I need to do id check is:
      > 1- convert the input string into date first. At this point I couldn't
      > find any srtTOdate type of functions. then I tried doing it the hard
      > way...by using "checkdate" function. I could split the string fine
      > (using "explode") but then I was stuck because I couldn't find any
      > function to convert a string into Integer!
      >
      > 2- I need to check if the date is valid
      >
      >
      > Could somebody please help me out?
      >
      > Thanks[/color]

      using checkdate() sounds good to me. converting a string into an int
      works like that:

      $int = (int)$string;

      micha

      Comment

      • Alvaro G Vicario

        #4
        Re: Date validation issue

        *** ndsoumah@gmail. com wrote/escribió (26 Jun 2005 21:04:47 -0700):[color=blue]
        > 1- convert the input string into date first. At this point I couldn't
        > find any srtTOdate type of functions.[/color]

        In the "Date and Time Functions" chapter of the PHP manual you have all
        date and time functions, including strtotime()


        then I tried doing it the hard[color=blue]
        > way...by using "checkdate" function. I could split the string fine
        > (using "explode") but then I was stuck because I couldn't find any
        > function to convert a string into Integer!
        >
        > 2- I need to check if the date is valid[/color]

        As you already mentioned, checkdate sounds like a good option.


        You may also find useful this code for a previous additional client-side
        validation:

        /*
        * Returns true if valid date, false otherwise
        * Month range is [1,12]
        */
        function is_date(day, month, year // v2005-03-14
        var TheDate=new Date();
        TheDate.setFull Year(year, month-1, day);

        return (TheDate.getDat e()==day) && (TheDate.getMon th()==month-1) &&
        (TheDate.getFul lYear()==year);
        }


        --
        -- Álvaro G. Vicario - Burgos, Spain
        -- http://bits.demogracia.com - Mi sitio sobre programación web
        -- Don't e-mail me your questions, post them to the group
        --

        Comment

        • ndsoumah@gmail.com

          #5
          Re: Date validation issue

          Thanks for all the replys
          It's a lot of help.

          Comment

          • NC

            #6
            Re: Date validation issue

            ndsoumah@gmail. com wrote:[color=blue]
            >
            > I'm having a hard time trying to validate some dates.
            > I have a form that accept dates from users in this format
            > (YYYY-mm-dd). I've been looking at the available datetime
            > functions and I'm still clueless as to how they can help
            > me achieve my goals.[/color]

            function is_valid_date($ datestring) {
            if (date('Y-m-d', strtotime($date string)) == $datestring) {
            return true;
            } else {
            return false;
            }
            }

            Usage examples:

            is_valid_date(' 2005-05-22'); // returns true
            is_valid_date(' 2005-05-32'); // returns false

            The function assumes leading zeros are mandatory. If they
            are not, here's a revision that will fix it:

            function is_valid_date($ datestring) {
            list($y, $m, $d) = explode('-', $datestring);
            list($yy, $mm, $dd) = explode('-', date('Y-m-d',
            strtotime($date string)));
            if (($y == $yy) and ($m == $mm) and ($d == $dd)) {
            return true;
            } else {
            return false;
            }
            }


            Cheers,
            NC

            Comment

            Working...