checkdate and int-conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Erik T.

    checkdate and int-conversion

    Hi,

    I have a form that contains amongst others a text-field where users
    should enter their year of birth.

    <input type="text" name="year_of_b irth">

    Within the php script that parses the info, I try to check if a valid
    birth-date was given, using the checkdate function. This function
    expects integers as parameters, so I tought converting the text to an
    integer value would work.

    checkdate (1, 1, (int)$_POST["year_of_bi rth"])

    However, this doesn't work, and I cannot seem to find out why... Any clues?

    cheers,
    E.T.
  • Janwillem Borleffs

    #2
    Re: checkdate and int-conversion

    Erik T. wrote:[color=blue]
    > Within the php script that parses the info, I try to check if a valid
    > birth-date was given, using the checkdate function. This function
    > expects integers as parameters, so I tought converting the text to an
    > integer value would work.
    >
    > checkdate (1, 1, (int)$_POST["year_of_bi rth"])
    >[/color]

    No explicit casting required, PHP will take care of this automatically.
    [color=blue]
    > However, this doesn't work, and I cannot seem to find out why... Any
    > clues?[/color]

    It doesn't work as it always returns 0/1 or throws an error?

    More info required, including your OS.


    JW



    Comment

    • Erik T.

      #3
      Re: checkdate and int-conversion

      Janwillem Borleffs wrote:[color=blue]
      > Erik T. wrote:
      >[color=green]
      >>Within the php script that parses the info, I try to check if a valid
      >>birth-date was given, using the checkdate function. This function
      >>expects integers as parameters, so I tought converting the text to an
      >>integer value would work.
      >>
      >>checkdate (1, 1, (int)$_POST["year_of_bi rth"])
      >>[/color]
      >
      >
      > No explicit casting required, PHP will take care of this automatically.[/color]

      Without the casting, I get :

      Warning: checkdate() expects parameter 3 to be long, string given in
      ledenform.php on line 49

      Line 49 looks like this:

      if ( checkdate ( $_POST["geboortema and"] , $_POST["geboorteda g"],
      $_POST["geboorteja ar"] ) )

      With the typecast, I don't get an error, but checkdate always results as
      FALSE.
      [color=blue]
      >
      >[color=green]
      >>However, this doesn't work, and I cannot seem to find out why... Any
      >>clues?[/color]
      >
      >
      > It doesn't work as it always returns 0/1 or throws an error?
      >
      > More info required, including your OS.[/color]

      I'm hosting this at my provider. php_info result:

      Comment

      • Janwillem Borleffs

        #4
        Re: checkdate and int-conversion

        Erik T. wrote:[color=blue]
        > Without the casting, I get :
        >
        > Warning: checkdate() expects parameter 3 to be long, string given in
        > ledenform.php on line 49
        >[/color]

        This means that $_POST["geboorteja ar"] does not start with a digit, but
        probably with a white space character.
        [color=blue]
        > With the typecast, I don't get an error, but checkdate always results
        > as FALSE.
        >[/color]

        When your try to cast a string to an integer and the string doesn't start
        with a digit, the result will be 0, which is why checkdate() always returns
        false.

        Try to apply trim() to $_POST["geboorteja ar"] before using it and have a
        close look at its value when the problem persists.


        JW



        Comment

        • R. Rajesh Jeba Anbiah

          #5
          Re: checkdate and int-conversion

          Erik T. wrote:
          <snip>[color=blue]
          > checkdate (1, 1, (int)$_POST["year_of_bi rth"])
          >
          > However, this doesn't work, and I cannot seem to find out why... Any[/color]
          clues?

          You may want to understand the casting basics in PHP:
          1. http://in2.php.net/set_type#50255
          2. http://in2.php.net/manual/en/languag...e-juggling.php

          --
          <?php echo 'Just another PHP saint'; ?>
          Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

          Comment

          Working...