Hi

I get the user to enter their date of birth by selecting the day, month and year from generated drop-down boxes.

I then want to put this into the database as either DD-MM-YYYY or the default YYYY-MM-DD but each time it just enters: 0000-00-00.

What am I doing wrong with the following code:

<!-- DAY -->
              <TD><SELECT NAME="day" class="formselect" />
                  <OPTION SELECTED VALUE="">Select</OPTION>
              	<?php
    		for ($day = 1; $day <= 31; $day++) {  
if ($day<10) { $day = "0".$day; } echo "<option value=\"".$day."\">".$day."</option>\n"; } ?> </SELECT> <!-- MONTH --> <SELECT NAME="month" class="formselect" /> <OPTION SELECTED VALUE="">Select</OPTION> <?php $today = getdate(); $thismonth = 1; for ($x=$thismonth;$x<13;$x++) { echo "<option value=\"".date("m", mktime(0,0,0,$x,1,0))."\">".date("F", mktime(0,0,0,$x,1,0))."</option>\n"; } ?> </SELECT> <!-- YEAR --> <SELECT NAME="year" class="formselect" alt="select" /> <OPTION SELECTED VALUE="">Select</OPTION> <?php $todayyear = $today['year']; for ($year = 1927; $year <= $todayyear-18; $year++) { echo "<option value=\"".$year."\">".$year."</option>\n"; } ?> </SELECT> <?php $day = $_POST["day"]; $month = $_POST["month"]; $year = $_POST["year"]; $DOB = $_POST["year"] . $_POST["month"] . $_POST["day"];
$mDOB = date('Ymd', $DOB); ?>

To test I try and echo $DOB or mDOB but nothing is displayed. However, if I echo the individual day, month or year, those are outputted.

Any ideas on what I am doing wrong?

Help!!

Thanks.

Mak 🙂

    This looks more complicated than it needs to be. I do it like so:

    $date= date("M-D-Y");

      Hi

      Can you explain a bit more please?

      It appears that the concatenation is not working somewhere here:

      <?php
      $day = $_POST["day"]; 
      $month = $_POST["month"]; 
      $year = $_POST["year"];
      
      $DOB = $_POST["year"] . $_POST["month"] . $_POST["day"];             
      $mDOB = date('Ymd', $DOB); ?>

      Thanks.

      Mak 🙂

        Ok, try this:

        Your row in a table:

        <TR>
        <TD><font face="Arial,Helv,Helvetica,Geneva" size="-1">Birthdate</font></td>
        <TD> <font face="Arial,Helv,Helvetica,Geneva" size="-1">
        <select name="month1">
        <option value="01">January
        <option value="02">February
        <option value="03">March
        <option value="04">April
        <option value="05">May
        <option value="06">June
        <option value="07">July
        <option value="08">August
        <option value="09">September
        <option value="10">October
        <option value="11">November
        <option value="12">December
        </select>
        
        <select name="day1">
        <option>01
        <option>02
        <option>03
        <option>04
        <option>05
        <option>06
        <option>07
        <option>08
        <option>09
        <option>10
        <option>11
        <option>12
        <option>13
        <option>14
        <option>15
        <option>16
        <option>17
        <option>18
        <option>19
        <option>20
        <option>21
        <option>22
        <option>23
        <option>24
        <option>25
        <option>26
        <option>27
        <option>28
        <option>29
        <option>30
        <option>31
        </select>
        
        19<input type="text" name="year1" size=4 maxwidth=4>
        </font>
         </td>
        </tr>
        

        And then upon submission of that form:

        	$year=date("Y");
        	$month=date("m");
        	$day=date("d");
        	$today=date("Y-m-d");
        
        $birthdate = "19".$year1."-".$month1."-".$day1;
        
        //echo $birthdate;
        
        # Set the sql variable and insert into database
        $sql="INSERT INTO email 
        (site_id,first_name,last_name,address,city,state,zip,phone,email,birthdate,sex,creation_date)
        VALUES ('$site_id','$first_name','$last_name','$address','$city','$state','$zip','$phone','$email','$birthdate','$sex','$today')";
        

          Hi

          I have modified mine as yours, but still no luck.

          I can echo the individual day, month or year but not the concatenation before I enter it into the DB, thus:

          mDOB = '".$_POST['mDOB']."', 

          Any ideas? 🙁

          Thanks.

          Mak 🙂

            Did you figure this out yet?

            If not, I was thinking... I haven't put POST commands into variables before. Perhaps you could string together your necessary variables, as in the example below of

            $birthdate = "19".$year1."-".$month1."-".$day1;

            and then just POST $birthdate into the database through your INSERT statement?

              Looking at your code again, perhaps this is more what you're looking for.

              Assuming your form fields are named day, month and year:

              $DOB = $year."/".$month."/".$day;

              Then in your INSERT statement, you can just put $DOB in your appropriate row.

                Hi mak-uk. The problem is that the php date() function takes a unix timestamp as the second parameter. In case you don't know a unix time stamp defines a date by the number of seconds since January 1 1970. Thus your concatonation of dates is in the wrong format for the date() function. To convert dates you do have to the unix timestamp format you would use the mktime() function. Here is some example code:

                $day = $_POST['day']; 
                
                $month = $_POST['month']; 
                
                $year = $_POST['year'];
                
                $birthdate=date("Y-m-d",mktime(0,0,0,$month,$day,$year)); 
                

                $brithdate should then contain the proper date in YYYY-MM-DD format for inserting into the database.

                  Hi

                  Great, I have gotten it working.

                  I think it was a combination of me doing the date code wrong as well as not realising that I can do $_POST after the form has been submitted and and not before!

                  What I was also wondering is that the date is entered into the database in the yyyy-mm-dd format. Is there any way to change the code/database default so that it is dd-mm-yyyy?

                  Thanks.

                  Mak 🙂

                    I'm assuming your using mysql as your database. To the best of my knowledge you can't change the input format on a date field. You can use the mysql function FROM_UNIXTIME() to insert straight from a unix timestamp. I know thats not what you asked but it lets you skip using date() on the php side. Another option is when it comes time to pull the dates out of the database you can use the mysql DATE_FORMAT() function to format the output in whatever way you desire.

                    Here is the location of the mysql manual page that deals with dates: http://www.mysql.com/doc/en/Date_and_time_functions.html

                      Write a Reply...