Posted By


brownrl on 11/18/10

Tagged


Statistics


Viewed 686 times
Favorited by 0 user(s)

PHP getDateSelect


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

Here is a little bit of code I use from time to time to make a generic some what safe date selector for forms. I wanted something easy, small, and not relying on images, jquery, libraries, etc... Most importantly I wanted the date that was selected to be in INT format for storing directly into the database as an INT. Mind you the point of this snippet is the function, not the example tester code.


Copy this code and paste it in your HTML
  1. <?php
  2.  
  3. if( count( $_POST ) )
  4. {
  5. die( "<pre>" . print_r( $_POST , true ) . "</pre>" );
  6. }
  7.  
  8. function getDateSelect( $name )
  9. {
  10. // use today if not set.
  11. if( ! isset( $_POST[$name] ) )
  12. {
  13. $_POST[$name] = time();
  14. }
  15.  
  16. //$t makes it simplier to type.
  17. $t = $_POST[$name];
  18.  
  19. // make $t always midnight
  20. $t = strtotime( date( "Y-m-d 00:00:00" ,$t ) );
  21.  
  22. $d = date( "d" , $t );
  23. $m = date( "m" , $t ); // note javascript wants the month to be -1
  24. $y = date( "Y" , $t );
  25.  
  26. $r = "";
  27.  
  28.  
  29. // make the js function
  30. // php generating JS YEAH!
  31. // actually not too bad just take the selector value and make a date out of it
  32. // then convert it to PHP time()/MYSQL
  33. $r .= "<script>\n";
  34. $r .= "function update_".$name."()\n";
  35. $r .= "{\n";
  36. $r .= "var d=document.getElementById( '".$name."-d' ).value;\n";
  37. $r .= "var m=document.getElementById( '".$name."-m' ).value-1;\n";
  38. $r .= "var y=document.getElementById( '".$name."-y' ).value;\n";
  39. $r .= "var myDate=new Date(y,m,d);\n";
  40. $r .= "var val=Math.floor( myDate.getTime() / 1000 );\n";
  41. $r .= "//alert( val );\n";
  42. $r .= "document.getElementById( '".$name."' ).value = val;\n";
  43. $r .= "}\n";
  44. $r .= "</script>\n\n";
  45.  
  46.  
  47. // make the hidden field that the selector will work on.
  48. $r .= "<input type=\"hidden\" name=\"".$name."\" id= \"".$name."\" value='".$t."' />\n";
  49.  
  50.  
  51.  
  52.  
  53. // now make the day select
  54. $i = 0;
  55. $r .= "\n<select name=\"\" id='".$name."-d' onChange='update_".$name."();' />\n";
  56. while( $i++ < 31 )
  57. {
  58. $sel = "";
  59. if( $d == $i )
  60. {
  61. $sel = " selected ";
  62. }
  63. $r .= "<option value='".$i."'".$sel.">".$i."</option>\n";
  64. }
  65. $r .= "</select>\n\n";
  66.  
  67. // now make the month select
  68. $i = 0;
  69. $r .= "\n<select name=\"\" id='".$name."-m' onChange='update_".$name."();' />\n";
  70. while( $i++ < 12 )
  71. {
  72. $sel = "";
  73. if( $m == $i )
  74. {
  75. $sel = " selected ";
  76. }
  77. $r .= "<option value='".$i."'".$sel.">".$i."</option>\n";
  78. }
  79. $r .= "</select>\n\n";
  80.  
  81. // now make the year select
  82. $i = 1900;
  83. $r .= "\n<select name=\"\" id='".$name."-y' onChange='update_".$name."();' />\n";
  84. while( $i++ < 2025 )
  85. {
  86. $sel = "";
  87. if( $y == $i )
  88. {
  89. $sel = " selected ";
  90. }
  91. $r .= "<option value='".$i."'".$sel.">".$i."</option>\n";
  92. }
  93. $r .= "</select>\n\n";
  94.  
  95. return $r;
  96.  
  97. }
  98.  
  99. ?>
  100. <html>
  101. <head>
  102. </head>
  103. <body>
  104. <form method="post">
  105. <?= getDateSelect( "tester" ); ?>
  106. <br />
  107. <input type="submit" />
  108. </form>
  109. </body>
  110. </html>

URL: http://www.itsgotto.be

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.