{"id":4282,"date":"2014-06-18T17:14:09","date_gmt":"2014-06-19T00:14:09","guid":{"rendered":"http:\/\/ahmeddirie.com\/?p=4282"},"modified":"2016-03-05T07:58:43","modified_gmt":"2016-03-05T12:58:43","slug":"building-a-dynamic-year-list-for-web-forms","status":"publish","type":"post","link":"https:\/\/ahmeddirie.com\/blog\/web-development\/building-a-dynamic-year-list-for-web-forms\/","title":{"rendered":"Building a dynamic year list for web forms"},"content":{"rendered":"<p>It&#8217;s been a while since I&#8217;ve last posted anything on here. Earlier, I was asked by a client to update the year listing on one of the forms on his site. The years were listed in a select statement and was modified manually each year. Yup &#8211; epic headache if you ask me. It made more sense to have this dynamically populated with the current year selected and work its way back to a specific year, of course. Otherwise, all hell would break loose and you&#8217;d end up with a form that goes as far back as BC.<br \/>\n<!--more--><\/p>\n<p>To avoid the pain and suffering of listing that many years, we&#8217;ll cut off our year at, let&#8217;s say, 2000&#8230; when the modern world should have collapsed after everything was supposed to stop working. Talk about being positive.<\/p>\n<h3>The code<\/h3>\n<pre class=\"brush: php; title: ; notranslate\" title=\"\">\r\n&lt;select name=&quot;dateYear&quot;&gt;\r\n\t&lt;?php \r\n\t\t$date = date(&quot;Y&quot;);\r\n\t\techo '&lt;option value=&quot;'.$date.'&quot; selected=&quot;selected&quot;&gt;'.$date.'&lt;\/option&gt;';\r\n\t\t$i = 1;\r\n\t\twhile ($date &gt; &quot;2000&quot;) {\r\n\t\t\t$date = date('Y', strtotime(&quot;-&quot;.$i.&quot; year&quot;));\r\n\t\t\techo '&lt;option value=&quot;'.$date.'&quot;&gt;'.$date.'&lt;\/option&gt;';\r\n\t\t\t$i++;\r\n\t\t}\r\n\t?&gt;\r\n&lt;\/select&gt;\r\n<\/pre>\n<p>And that&#8217;s it folks. You&#8217;ll never have to revisit that form again. You can also modify this to only go back a certain number of years based on current year so that it automatically keeps your list fresh.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s been a while since I&#8217;ve last posted anything on here. Earlier, I was asked by a client to update the year listing on one of the forms on his site. The years were listed in a select statement and was modified manually each year. Yup &#8211; epic headache if you ask me. It made more sense to have this dynamically populated with the current year selected and work its way back to a specific year, of course. Otherwise, all hell would break loose and you&#8217;d end up with a form that goes as far back as BC.<\/p>\n","protected":false},"author":2,"featured_media":4287,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[152],"tags":[476,478,22,477],"_links":{"self":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/4282"}],"collection":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/comments?post=4282"}],"version-history":[{"count":10,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/4282\/revisions"}],"predecessor-version":[{"id":4665,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/posts\/4282\/revisions\/4665"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media\/4287"}],"wp:attachment":[{"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/media?parent=4282"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/categories?post=4282"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ahmeddirie.com\/blog\/wp-json\/wp\/v2\/tags?post=4282"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}