I'm writing codes for show years and months of past years
ex
<select>
<option value="03/2014">03/2014</option>
<option value="02/2014">02/2014</option>
<option value="01/2014">01/2014</option>
<option value="12/2013">12/2013</option>
<option value="11/2013">11/2013</option>
<option value="10/2013">10/2013</option>
</select>
im using this code but i can get only moonth names
$i = 1;
$month = time();
while($i <= 12)
{
$month_name = date('F', $month);
echo '<option value="'. $month_name. '">'.$month_name.'</option>';
$month = strtotime('+1 month', $month);
$i++;
}
but i need to get month and year for past 4,5 years, anyone know how to that using php date function or any other short way.thank you
Put something like this into a function:
$start = strtotime('2014-01');
$end = strtotime('2014-12');
$range = array(date('Y-m', $start) => date('Y-m', $start));
while ( $start <= strtotime('-1 month', $end) ) {
$start = strtotime('+1 month', $start);
$yearMonth = date('Y-m', $start);
$range[$yearMonth] = $yearMonth;
}
var_dump($range);
Αdd a
$year_name=date('Y',$month);
...along with $month_name
and adjust your select
's option
s accordingly.
Alternatively, you could do
$month_name = date('F/Y', $month);
Also, 4.5 years are 4*12+6=54 months, so while($i <= 54)
.