Simple Date Control


/ Published in: PHP
Save to your folder(s)

This is used privately simply to avoid retyping this all the time.


Copy this code and paste it in your HTML
  1. function dateControl($prefix,$session_prefix,$expire_year_start = 0)
  2. {
  3. // The prefix is used to be able to differenciate between controls in the event that multiple instances are used on the form
  4. // The session prefix is used to easily maitain the application-wide prefix used in the sesison variables just for naming purposes.
  5. // The expire_year_start variable is used to use as an offset due to the fact that the expiration year for passports will be
  6. // 10 years from the issue year.
  7. $year_control = '<select name="'.$prefix.'yearControl" id="'.$prefix.'yearControl">';
  8. for($y = date("Y")+$expire_year_start; $y >= 1900; $y--)
  9. {
  10. if(isset($_SESSION[$session_prefix.$prefix.'yearControl']) && $_SESSION[$session_prefix.$prefix.'yearControl']==$y)
  11. {
  12. $year_control .= '<option value="'.$y.'" selected>'.$y.'</option>';
  13. }else
  14. {
  15. $year_control .= '<option value="'.$y.'">'.$y.'</option>';
  16. }
  17. }
  18. $year_control .= '</select>';
  19.  
  20. $month_control = '<select name="'.$prefix.'monthControl" id="'.$prefix.'monthControl">';
  21. for($m = 1; $m <= 12; $m++)
  22. {
  23. if(isset($_SESSION[$session_prefix.$prefix.'monthControl']) && ($_SESSION[$session_prefix.$prefix.'monthControl']==$m || $_SESSION[$session_prefix.$prefix.'monthControl']=='0'.$m))
  24. {
  25. $month_control .= $m > 9 ? '<option value="'.$m.'" selected>'.$m.'</option>' : '<option value="0'.$m.'" selected>0'.$m.'</option>';
  26. }else
  27. {
  28. $month_control .= $m > 9 ? '<option value="'.$m.'">'.$m.'</option>' : '<option value="0'.$m.'">0'.$m.'</option>';
  29. }
  30. }
  31. $month_control .= '</select>';
  32.  
  33. $day_control = '<select name="'.$prefix.'dayControl" id="'.$prefix.'dayControl">';
  34. for($d = 1; $d <= 31; $d++)
  35. {
  36. if(isset($_SESSION[$session_prefix.$prefix.'monthControl']) && ($_SESSION[$session_prefix.$prefix.'dayControl']==$d || $_SESSION[$session_prefix.$prefix.'dayControl']=='0'.$d))
  37. {
  38. $day_control .= $d > 9 ? '<option value="'.$d.'" selected>'.$d.'</option>' : '<option value="0'.$d.'" selected>0'.$d.'</option>';
  39. }else
  40. {
  41. $day_control .= $d > 9 ? '<option value="'.$d.'">'.$d.'</option>' : '<option value="0'.$d.'">0'.$d.'</option>';
  42. }
  43. }
  44. $day_control .= '</select>';
  45.  
  46. return $year_control.' '.$month_control.' '.$day_control.' yyyy/mm/dd';
  47. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.