PHPBuilder - PHP Date Chooser

RSS Twitter

PHP Date Chooser

by: Kenneth Brill
November 9, 2002

Version: 1.0

Type: Function

Category: Calendars/Dates

License: GNU General Public License

Description: I was looking for the easiest, quickest way to add a date picker to my internal web site without having to add more javascript or a pop-up window. I found several fantastic javascripts to add calendars to the page and a few even used layers instead of pop-ups, but I was looking for something simpler. I noticed that although the javascript calendars will allow you to pick a date years away, most of my users were picking dates in the next 60 days and no more than 120 ever. So for my application I came up with this. Quick and easy you just call the function whereever you want the date dropdown to show up. You can force the name of the <select> field with the $default param and you can specify the number of days to display with the $size param.

function DateDropDown($size=90,$default="DropDate") {
   // $size = the number of days to display in the drop down
   // $default = Todays date in m:d:Y format (SEE DATE COMMAND ON WWW.PHP.NET)
   // $skip = if set then the program will skip Sundays and Saturdays
   echo "<select name=dropdate STYLE=\"font-family: monospace;\">\n";
   for ($i = 0; $i <= $size; $i++) {
      $theday = mktime (0,0,0,date("m") ,date("d")+$i ,date("Y"));
      $option=date("D M j, Y",$theday);
      if ($dow=="Sun") {
         echo "<option disabled>&nbsp;</option>\n";
      if ($value == $default) {
      } else {
      if (($dow!="Sun" and $dow!="Sat") or !$skip) {
         echo "<option value=\"$value\" $selected>$option</option>\n";
   echo "</select>\n";

Comment and Contribute

Your comment has been submitted and is pending approval.

Kenneth Brill



(Maximum characters: 1200). You have characters left.