Hello everyone i thought that this was simple for myself but i need and advice .

I have a form with three select menu for day-month-year that i can select when someone fill a form to subscribe.

and i'm wondering which technique to use to stock the data in my sql table.

should i have three row of data one for my date the other for the month and the last one for the year.

also when i do a query from my database how does it goes to get the data so the result display the age of the person in real time.

if someone can show me and example code of that that would be really good for me

Let me know.

    If I were you, I would put the data into one database table in the format: YYYY-MM-DD

    To do this with your data, just append the day, month, and year together:

    $bday = $year . "-" . $month . "-" . $day;
    

    Then put the variable $bday into your database.

    Now, in order to get a person's age from their birthday, just run a simple subtraction algorithm against the date. Off the top of my head, this should work:

    $currentYear = date("Y");
    $currentMonth = date("m");
    $currentDay = date("d");
    
    $year = $currentYear - substr($userQueryRow['UserBirthday'], 0, 4);
    $mon = substr($userQueryRow['UserBirthday'], 5, 2);
    $day = substr($userQueryRow['UserBirthday'], 8, 2);
    
    if ($mon >= $currentMonth) {
    	if ($day > $currentDay)
    		$year--;
    }
    
    echo "You are $year years old.";
    

    What this does is, takes the value of the current year minus the user's birth year, and puts that into $year. Then to be right down to the day, it calculates whether or not the user's birth month and birth day is greater than the current month and current day.

    HTH,

    insectis

      This look like the field name in my form will be for example year month day

      the value $bday will be the value that i will concatenate from thoose three value.

      how do i format my answer to look like YYYY-MM-DD and be place in my database do you have and example on the top of your head?

      also do i put the type of my field as date in phpmyadmin for the field YYYY-MM-DD
      or varchar?

      let me know.

      i like the idea for the calculation for the anniversary date.

        I already showed you how to format the data for input into your database:

        $bday = $year . "-" . $month . "-" . $day;

        All you have to do is insert $bday into your database field.

        I would personally use DATE instead of VARCHAR.

        insectis

          Ok nice i get it this would be great i think i ll try

            a year later

            I have birthdate in MySQL in form of YYYY-MM-DD. I used function suggested by insectis, but it doesnt work properly.

            Date 1969-04-10 returns 35 years which is correct
            but date
            1969-10-10 also returns 35 years instead of 34. Any ideas?

              Write a Reply...