Create dynamic <select> with PHP -
i have $_session
variable $_session['invoicedate']
populated value representing yyyymm
.
i need dynamically build select element have 6 options going 'invoicedate' e.g. if $_session['invoicedate'] = 201602
output should be...
<select id="invoicedate" name="invoicedate"> <option value="201602">feb 2016</option> <option value="201601">jan 2016</option> <option value="201512">dec 2015</option> <option value="201511">nov 2015</option> <option value="201510">oct 2015</option> <option value="201509">sep 2015</option> </select>
my php basic... can split session value , assign first 4 digits $year , other 2 $month. loop 6 times... each time echo output <option>
line concatenating year , month make value , subtracting 1 month , if month=0 take 1 year , make month 12. gives me....
<option value="201602"></option> <option value="201601"></option> <option value="201512"></option> <option value="201511"></option> <option value="201510"></option> <option value="201509"></option>
now can join year , month hyphen give
<option value="201602">2016-02</option> <option value="201601">2016-01</option> <option value="201512">2015-12</option> <option value="201511">2015-11</option> <option value="201510">2015-10</option> <option value="201509">2015-09</option>
but how month year instead... e.g. feb 2016 instead of 2016-02?
why aren't using datetime
?
$e = '201602'; $date = new datetime($e); echo $date->format('m');
outputs feb
in case be
$e = '201602'; $date = new datetime($e); $month = $date->format('n'); for($i=0;$i<6;$i++) { echo '<option value="'.$date->format('ym').'">'.$date->format('m y').'</option>'; $date = $date->sub(new dateinterval('p1m')); }
this outputs
<option value="201602">feb 2016</option> <option value="201601">jan 2016</option> <option value="201512">dec 2015</option> <option value="201511">nov 2015</option> <option value="201510">oct 2015</option> <option value="201509">sep 2015</option>
just edit need , don't use infernal version of date
strtotime
...
Comments
Post a Comment