Convert Month Number to Month Name in a Range

Hi…

I need help in getting the 3 Months Name from my table field FromMonth and ToMonth.
Here is the scenario.

First, I select FromMonth and ToMonth.
Ex.
FromMonth = 5
ToMonth = 7
So it means
FromMonth is May
ToMonth is July.

Now I save the MonthNumber to my database:

table- so_month
FromMonth = 5
ToMonth = 7

Now I need to get the between Months Name from FromMonth to ToMonth which are (May, June, July).

How does it possible?
Thank you so much.

Use the date() when outputing.
For example… this foo() would give you an array of the month names.


function monthNames($from, $to){
   $range=array();
   for($i=$from; $i<=$to; $i++){
   		$range[$i]=date('M', mktime(0,0,0,$i));
   }
	return $range;
}
 echo  implode(", ",monthNames(5,7));

hope that helps

Thank’s it works…

Now i have an issue…

Is their any way so I can select the first consecutive months in my table sales_order
fields - ETD type - Date.

Thank you

I tried this:


function monthNames($from, $to){
   $range=array();
   for($i=$from; $i<=$to; $i++){
           $range[$i]=date('M', mktime(0,0,0,$i));
   }
    return $range;
}
 echo  implode(", ",monthNames(5,7));

 $sql = "SELECT FromMonth, ToMonth FROM so_month";
$res = mysql_query($sql,$con);

$row = mysql_fetch_assoc($res);
$FromMonth = $row['FromMonth'];
$ToMonth = $row['ToMonth'];

echo $FromMonth;
echo $ToMonth;

 echo monthNames($FromMonth, $ToMonth);


the output is:

May, Jun, Jul57Array

it did not read the $FromMonth, $ToMonth

why?

Thank you

this foo() would give you an array of the month names.

so what is happening is :monthNames(5, 7)=Array( 5=>‘may’, 6=>‘june’, 7=>‘july’)
Arrays can’t be echoed.

If you wanted to echo them all in one statement, use implode(); ( or change the function to store the month in a string and not an array, tho I think you would find this would trully limit the functions versatility.

You could also do this:

foreach( monthNames($FromMonth, $ToMonth) as $month){ echo $month,‘<br>’; }

ok thank you…

Now how can I put the 3 months in table <td>

for example:
May
June
Jul

Thank you

same way…
what script are you using to build your table? You will need to take into account how HTML tables work. but just for the sake of showing you how to output:

foreach( monthNames($FromMonth, $ToMonth) as $month){ echo ‘<td>’,$month,‘</td>’; }

somebody else had exactly the same question! :eek:

Thank you