Happy Codings - Programming Code Examples
Html Css Web Design Sample Codes CPlusPlus Programming Sample Codes JavaScript Programming Sample Codes C Programming Sample Codes CSharp Programming Sample Codes Java Programming Sample Codes Php Programming Sample Codes Visual Basic Programming Sample Codes


Php Programming Code Examples

Php > HTML and Php Code Examples

This PHP function creates dropdown select lists for time and date that you can

This PHP function creates dropdown select lists for time and date that you can change, outputs a 14 char MySQL timestamp in a text field Enjoy this - I'm sure it could be more elegant, but I couldn't find something like this anywhere else. Pull your hair out on more important programming problems! If you like it, or improve upon it, drop me a line. call the function like this: <?PHP stamper($previous_value); ?> <?PHP function stamper($previous_value) { // your constants $form_name = "form1"; $store_stamp_field = "datestamp"; // looks for previous 14 char stamp or defaults to now if (!$previous_value) { $my_stamp = date("YmdHis"); } else { $my_stamp = $previous_value; } // parses the timestamp & digests it into useful variables $year=substr($my_stamp,0,4); $month=substr($my_stamp,4,2); $day=substr($my_stamp,6,2); $hour=substr($my_stamp,8,2); $minute=substr($my_stamp,10,2); $second=substr($my_stamp,12,2); // here you can set your ("starting value", "range") for each $year_range = array("2000","11"); $month_range = array("01","12"); $day_range = array("01","31"); $hour_range = array("00","24"); $minute_range = array("00","60"); $second_range = array("00","60"); $names = array("month","day","year","hour","minute","second"); $names_for_stamp = array("year","month","day","hour","minute","second"); // this drops in some javascript to do the onChange event handler for each select list echo "<script language=\"JavaScript\">\n"; echo "function adj_timestamp () {\n"; echo "var each_field = new Array(" . count($names) . ");\n"; for ($i=0;$i<count($names);$i++) { echo "each_field[" . $i . "] = document." . $form_name . "." . $names_for_stamp[$i] . ".options [document." . $form_name . "." . $names_for_stamp[$i] . ".selectedIndex].value;"; } echo "var timestamp = "; for ($i=0;$i<count($names);$i++) { if ($i!=(count($names)-1)) { echo "each_field[" . $i . "] + "; } else { echo "each_field[" . $i . "];\n"; } } echo "document." . $form_name . "." . $store_stamp_field . ".value = timestamp;\n"; echo "}\n"; echo "</script>\n"; // this creates the selects and add the JS event handler for ($i=0;$i<count($names);$i++) { echo "<select name=\"$names[$i]\" onChange=\"adj_timestamp();\">\n"; $this_one = ${"$names[$i]" . "_range"}; for ($k=0;$k<$this_one[1];$k++) { $each_val = $this_one[0]+$k; if (strlen($each_val)<2) { $each_val = sprintf("%02d", $each_val); } if (${"$names[$i]"}==$each_val) { $selected=" selected"; } else { $selected=""; } echo "<option value=\"$each_val\"$selected>$each_val</option>\n"; } echo "</select>\n"; } //this creates the initial timestamp $the_stamp = $year . $month . $day . $hour . $minute . $second; //this holds the dynamic timestamp value, make it hidden once you get the hang of how it all works echo "<input type=\"text\" name=\"$store_stamp_field\" value=\"$the_stamp\">\n"; } ?>