Posted By

tylerhall on 11/30/-1


Tagged

month day year dropdown calendar date


Versions (?)

Who likes this?

16 people have marked this snippet as a favorite

theferf
Roshambo
luxuryluke
mdavie
vali29
hudge
benrasmusen
jfherring
oriolfb
irishsk
nb109
g33kish
bobbym245
ninjacharlee
mecha
robfahy


Month Day Year Smart Dropdowns


 / Published in: PHP
 

  1. function mdy($mid = "month", $did = "day", $yid = "year", $mval, $dval, $yval)
  2. {
  3. if(empty($mval)) $mval = date("m");
  4. if(empty($dval)) $dval = date("d");
  5. if(empty($yval)) $yval = date("Y");
  6.  
  7. $months = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
  8. $out = "<select name='$mid' id='$mid'>";
  9. foreach($months as $val => $text)
  10. if($val == $mval) $out .= "<option value='$val' selected>$text</option>";
  11. else $out .= "<option value='$val'>$text</option>";
  12. $out .= "</select> ";
  13.  
  14. $out .= "<select name='$did' id='$did'>";
  15. for($i = 1; $i <= 31; $i++)
  16. if($i == $dval) $out .= "<option value='$i' selected>$i</option>";
  17. else $out .= "<option value='$i'>$i</option>";
  18. $out .= "</select> ";
  19.  
  20. $out .= "<select name='$yid' id='$yid'>";
  21. for($i = date("Y"); $i <= date("Y") + 2; $i++)
  22. if($i == $yval) $out.= "<option value='$i' selected>$i</option>";
  23. else $out.= "<option value='$i'>$i</option>";
  24. $out .= "</select>";
  25.  
  26. return $out;
  27. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: Roshambo on May 17, 2007

This rox my sox. Might I recommend changing the first line to:

function mdy($mid = "month", $did = "day", $yid = "year", $mval = null, $dval = null, $yval=null)

This would avoid getting some compiler warnings when calling the function with a plain ol' mdy()

You need to login to post a comment.