WORKING WITH DATE FIELDS IN THE MEMBER SIGNUP AND PROFILE UPDATE FORMS - Jan 20th, 2011


BACKGROUND
Along with other information, my client needed to list the dates of birth for her students in their records in the
accounts database.

She wanted to include date fields for 3 students. Student 1, 2 and 3.

This meant that the parents would have to be able to enter the dates into date fields in the database record they were
creating, and also be able to update those date fields when required.

Here's the code we used to accomplish these tasks, (with a lot of help from Jason Sauchuk from Interactive Tools).

THE MEMBER APPLICATION FORM
In the original application form, in the PHP code at the top of the page, in the //add user section:


$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));


and in the mysql_query("INSERT INTO `{$TABLE_PREFIX}accounts` SET section:


student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',


Then in the form, the following code (in the appropriate place for your particular form):
NOTE: You only have to define the variables $lowestYear and $highestYear once per viewer.

<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1920;
$highestYear = 2006;
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>"><?php echo date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>"><?php echo $day_1;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>"><?php echo $year_1;?></option>
<?php endforeach?>
</select>

</tr>




<tr>
<td>Date of Birth - Student 2</td>
<td>Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>"><?php echo date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>"><?php echo $day_2;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>"><?php echo $year_2;?></option>
<?php endforeach?>
</select>

</tr>


And,

<tr>
<td>Date of Birth - Student 3</td>
<td>Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>"><?php echo date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>"><?php echo $day_3;?></option>
<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>"><?php echo $year_3;?></option>
<?php endforeach?>
</select>

</tr>



THE PROFILE UPDATE FORM
For the profile update page, the code at the top of the page, in the //update user section is:


$student_1_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_1'],@$_REQUEST['day_1'],@$_REQUEST['year_1']));
$student_2_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_2'],@$_REQUEST['day_2'],@$_REQUEST['year_2']));
$student_3_dob = date("Y-m-d",mktime(0,0,0,@$_REQUEST['month_3'],@$_REQUEST['day_3'],@$_REQUEST['year_3']));



And the code in the $query = "UPDATE `{$TABLE_PREFIX}accounts` SET section is:


student_1_dob = '".mysql_escape($student_1_dob)."',
student_2_dob = '".mysql_escape($student_2_dob)."',
student_3_dob = '".mysql_escape($student_3_dob)."',


In the form, in order to pre-populate the individual parts of the date field, Jason came up with the following code:

<tr>
<td>Date of Birth - Student 1</td>
<td><?php
$lowestYear = 1935;
$highestYear = 2008;

//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();

$_REQUEST['month_1']=date("n",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['day_1']=date("j",strtotime($CURRENT_USER['student_1_dob']));
$_REQUEST['year_1']=date("Y",strtotime($CURRENT_USER['student_1_dob']));
?>
Month:
<select name="month_1">
<?php foreach(range(1,12) as $month_1): ?>
<option value="<?php echo $month_1;?>" <?php selectedIf($month_1,@$_REQUEST['month_1']);?>><?php echo
date("F",strtotime("0000-$month_1"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_1">
<?php foreach(range(1,31)as $day_1): ?>
<option value="<?php echo $day_1;?>" <?php selectedIf($day_1,@$_REQUEST['day_1']);?>><?php echo $day_1;?></option>

<?php endforeach ?>
</select> Year:
<select name="year_1">
<?php foreach (range($lowestYear,$highestYear) as $year_1):?>
<option value="<?php echo $year_1;?>" <?php selectedIf($year_1,@$_REQUEST['year_1']);?>><?php echo
$year_1;?></option>
<?php endforeach?>
</select>

</tr>




<tr>
<td>Date of Birth - Student 2</td>
<td><?php

//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();

$_REQUEST['month_2']=date("n",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['day_2']=date("j",strtotime($CURRENT_USER['student_2_dob']));
$_REQUEST['year_2']=date("Y",strtotime($CURRENT_USER['student_2_dob']));
?>
Month:
<select name="month_2">
<?php foreach(range(1,12) as $month_2): ?>
<option value="<?php echo $month_2;?>" <?php selectedIf($month_2,@$_REQUEST['month_2']);?>><?php echo
date("F",strtotime("0000-$month_2"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_2">
<?php foreach(range(1,31)as $day_2): ?>
<option value="<?php echo $day_2;?>" <?php selectedIf($day_2,@$_REQUEST['day_2']);?>><?php echo $day_2;?></option>

<?php endforeach ?>
</select> Year:
<select name="year_2">
<?php foreach (range($lowestYear,$highestYear) as $year_2):?>
<option value="<?php echo $year_2;?>" <?php selectedIf($year_2,@$_REQUEST['year_2']);?>><?php echo
$year_2;?></option>
<?php endforeach?>
</select>

</tr>



And,


<tr>
<td>Date of Birth - Student 3</td>
<td><?php

//pre-populate date fields from the database
//get most current $CURRENT_USER
$CURRENT_USER = _websiteLogin_getCurrentUser();

$_REQUEST['month_3']=date("n",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['day_3']=date("j",strtotime($CURRENT_USER['student_3_dob']));
$_REQUEST['year_3']=date("Y",strtotime($CURRENT_USER['student_3_dob']));
?>
Month:
<select name="month_3">
<?php foreach(range(1,12) as $month_3): ?>
<option value="<?php echo $month_3;?>" <?php selectedIf($month_3,@$_REQUEST['month_3']);?>><?php echo
date("F",strtotime("0000-$month_3"));?></option>
<?php endforeach ?>
</select> Day:
<select name="day_3">
<?php foreach(range(1,31)as $day_3): ?>
<option value="<?php echo $day_3;?>" <?php selectedIf($day_3,@$_REQUEST['day_3']);?>><?php echo $day_3;?></option>

<?php endforeach ?>
</select> Year:
<select name="year_3">
<?php foreach (range($lowestYear,$highestYear) as $year_3):?>
<option value="<?php echo $year_3;?>" <?php selectedIf($year_3,@$_REQUEST['year_3']);?>><?php echo
$year_3;?></option>
<?php endforeach?>
</select>

</tr>



The materials on this web site have been created for use with CMS Builder content management software. CMS Builder software is published and licensed for use by InteractiveTools.com. Please contact Interactive Tools for information on the downloading of the software or the purchasing of licenses.


Terms of Service