Saturday, 21 January 2012

How to get last 12 months in php

<?php
$months = array();
for($i = 0; $i < 12; $i++){
  $timestamp = strtotime("-$i month");
  $value =  date('n,Y', strtotime("-$i month"));
  $text  =  date('F Y', strtotime("-$i month")); 
  $months[$value] =  $text;
}
echo '<select>';
foreach($months as $value => $text){
    echo '<option value="'.$value.'">'.$text.'</option>';
}
echo '</select>';
?>

No comments:

Post a Comment