Version: 1.1
Type: Function
Category: Calendars/Dates
License: GNU General Public License
Description: This is 2 date functions. The first creates a date object, the second processes the data on submit.
<?php
// The functions contained in this script generate and process a php format date select object
// Author: Ulysses J Ludwig
// E-mail: ujludwig@yahoo.com
// Phone: 213.948.8278
function Fn_date_select($In_field_name, $In_date_format_string, $In_unix_time)
{ // This function generates a date and or time function based on the format and default time that you enter
if (!$In_unix_time)
{
$In_unix_time = time();
}
for ($j=0;$j<strlen($In_date_format_string);$j++)
{ // looping through the format string
$L_format_char = substr($In_date_format_string,$j,1);
switch ($L_format_char)
{ // each character in the format string means something
case "m": // numeric month
?>
<select name=<?php echo $In_field_name . "[m]"?>>
<?php
for ($i=1;$i<13;$i++)
{
$L_var_month = date("$L_format_char",mktime(0,0,0,$i,15,2000));
if ($L_var_month == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print ("<option value=$i $L_selected>" . $L_var_month . "</option>\n");
}
?>
</select>
<?php
break;
case "F": // textual full month
?>
<select name=<?php echo $In_field_name . "[m]"?>>
<?php
for ($i=1;$i<13;$i++)
{
$L_var_month = date("$L_format_char",mktime(0,0,0,$i,15,2000));
if ($L_var_month == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print ("<option value=$i $L_selected>" . $L_var_month . "</option>\n");
}
?>
</select>
<?php
break;
case "d":
?>
<select name=<?php echo $In_field_name . "[d]"?>>
<?php for ($i=1;$i<32;$i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "D":
?>
<select name=<?php echo $In_field_name . "[d]"?>>
<?php for ($i=1;$i<8;$i++)
{
$L_var_textual_day = date("$L_format_char",mktime(0,0,0,8,$i,1999));
if ($L_var_textual_day == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$L_var_textual_day</option>\n");
}
?>
</select>
<?php
break;
case "y":
?>
<select name=<?php echo $In_field_name . "[y]"?>>
<?php for ($i = 1996; $i <= (date($L_format_char) + 8); $i++) // starting in 1997
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "Y":
?>
<select name=<?php echo $In_field_name . "[y]"?>>
<?php for ($i = 1996; $i <= (date($L_format_char) + 7); $i++) // starting in 1997
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "h":
?>
<select name=<?php echo $In_field_name . "[h]"?>>
<?php for ($i = 1; $i < 13; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "g":
?>
<select name=<?php echo $In_field_name . "[h]"?>>
<?php for ($i = 1; $i < 13; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "H":
?>
<select name=<?php echo $In_field_name . "[h]"?>>
<?php for ($i = 0; $i < 24; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "G":
?>
<select name=<?php echo $In_field_name . "[h]"?>>
<?php for ($i = 0; $i < 24; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "i":
?>
<select name=<?php echo $In_field_name . "[i]"?>>
<?php for ($i = 0; $i < 60; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "s":
?>
<select name=<?php echo $In_field_name . "[s]"?>>
<?php for ($i = 0; $i < 60; $i++)
{
if ($i == date("$L_format_char",$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=$i $L_selected>$i</option>\n");
}
?>
</select>
<?php
break;
case "a":
?>
<select name=<?php echo $In_field_name . "[a]"?>>
<?php
// AM
$L_text_time = "am";
if ($L_text_time == date(strtoupper($L_format_char),$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=1 $L_selected>$L_text_time</option>\n");
// PM
$L_text_time = "pm";
if ($L_text_time == date(strtoupper($L_format_char),$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=2 $L_selected>$L_text_time</option>\n");
?>
</select>
<?php
break;
case "A":
?>
<select name=<?php echo $In_field_name . "[a]"?>>
<?php
// AM
$L_text_time = "AM";
if ($L_text_time == date(strtoupper($L_format_char),$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=1 $L_selected>$L_text_time</option>\n");
// PM
$L_text_time = "PM";
if ($L_text_time == date(strtoupper($L_format_char),$In_unix_time))
{
$L_selected=" SELECTED ";
}
else
{
unset($L_selected);
}
print("<option value=2 $L_selected>$L_text_time</option>\n");
?>
</select>
<?php
break;
default:
echo $L_format_char; // not a recognized character, so echo it out
} // end of switch
} // end of for
}
function Fn_process_date_select($In_time_array)
{
// This function processes the date select object on submit
if ($In_time_array[a]==2 and ($In_time_array[h] < 12))
{ // the user chose pm
$In_time_array[h] = $In_time_array[h] + 12;
}
else
if ($In_time_array[a] == 1 and $In_time_array[h] == 12)
{
$In_time_array[h] = 0;
}
return mktime($In_time_array[h],$In_time_array[i],$In_time_array[s],$In_time_array[m],$In_time_array[d],$In_time_array[y]);
}
?>
<html>
<?php
// To process the user entry back to php-unix time do this
if ($_GET['submit_pb'])
{
$L_php_unix_time = Fn_process_date_select($_GET['MyDateField']);
echo "The time the user entered was: " . date("F d Y, h:i:s A", $L_php_unix_time);
}
?>
<form name=myform>
<?php Fn_date_select("MyDateField","F d Y, h:i:s A",$Null); /* M */ ?>
<input name="submit_pb" type="submit" value="Submit">
</form>
</html>
</body>
</html>