0

I have months in this format : 07,06,05,04,03,02. I want to convert them to : July, Jun, May, April, March, February.

Actually I need a drop down listing of 6 months back from the current date.

<select>
   <option value="07">July</option>
   <option value="06">June</option>
   <option value="05">May</option>
   <option value="04">April</option>
   <option value="03">March</option>
   <option value="02">February</option>
 </select>
halfer
  • 19,824
  • 17
  • 99
  • 186
Bikram Pahi
  • 1,107
  • 1
  • 12
  • 33

3 Answers3

3

You can get today's date and modify it while looping.

$d = new Datetime();
for($i = 0; $i < 6; $i++){

    echo $d->format('m'); // numeric
    echo $d->format('M'); // short string   
    echo $d->format('F'); // long string  
    $d->modify('-1 MONTH');
}
engvrdr
  • 541
  • 2
  • 9
2

YOu can use an array for this -

$months = array(
'01' => 'January',
'02' => 'February',
....
);

echo $months[$yourVal];
Sougata Bose
  • 31,517
  • 8
  • 49
  • 87
1

Use Datetime class if you have latest PHP, else,

Try this, and apply your logic accordingly. Its example for December.

echo date('F', strtotime(date('Y-12-d')));
Hytool
  • 1,358
  • 1
  • 7
  • 22