Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Convert Month Number to Month Name in a Range

1. ## 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.

• Originally Posted by iBall
The easiest way I can think of off the top of my head is to have the month names in an array and then get the array elements between indices FromMonth and ToMonth inclusive.
Can you give the sample code?

I'm not good in array

Thank you

• Originally Posted by iBall
A tutorial on php arrays
I tried this:

PHP Code:
``` <?php  error_reporting(E_ALL ^ E_NOTICE);   date_default_timezone_set("Asia/Singapore"); //set the time zone   \$con = mysql_connect('localhost', 'root',''); if (!\$con) {     echo 'failed';     die(); } mysql_select_db("mes", \$con); /* Simple way to get current month name */ \$mons = array(1 => "Jan", 2 => "Feb", 3 => "Mar", 4 => "Apr", 5 => "May", 6 => "Jun", 7 => "Jul", 8 => "Aug", 9 => "Sep", 10 => "Oct", 11 => "Nov", 12 => "Dec"); \$sql = "SELECT FromMonth, ToMonth FROM so_month WHERE FromMonth = '5' AND ToMonth= '7' "; \$res = mysql_query(\$sql,\$con); while(\$row = mysql_fetch_assoc(\$res)) { //\$date = getdate(); //\$month = \$date['mon']; \$month_name = \$mons[\$row]; } echo \$month_name; // Displays the current month ?> ```
but the output is:

Warning: Illegal offset type in D:\Program Files\xampp\htdocs\week\month.php on line 23

Thank you

• line 23:\$month_name = \$mons[\$row];

Thank you

• I tried this:

PHP Code:
``` 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

• Originally Posted by lannlycuellar
Your code is already becoming messy and it's not clear how you are trying to do it
I tried this:

PHP Code:
``` function monthNames(\$from, \$to){    \$range=array();    for(\$i=\$from; \$i<=\$to; \$i++){            \$range[\$i]=date('M', mktime(0,0,0,\$i));    }     return \$range; }  \$sql = "SELECT FromMonth, ToMonth FROM so_month"; \$res = mysql_query(\$sql,\$con); \$row = mysql_fetch_assoc(\$res); \$FromMonth = \$row['FromMonth']; \$ToMonth = \$row['ToMonth']; foreach( monthNames(\$FromMonth, \$ToMonth) as \$month){ echo \$month,'<br>'; }  ```
and it works..

now my problem how can I put this on my code:

PHP Code:
``` <html> <head> <title>Half Shell</title> <link rel="stylesheet" type="text/css" href="kanban.css" /> <?php   error_reporting(E_ALL ^ E_NOTICE);   date_default_timezone_set("Asia/Singapore"); //set the time zone   \$con = mysql_connect('localhost', 'root',''); if (!\$con) {     echo 'failed';     die(); } mysql_select_db("mes", \$con); ?> <body> <form name="param" action="" method="post" onSubmit="return false"> <div id="fieldset_PS"> <?php    echo "<table>";        \$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";    \$res_comp = mysql_query(\$sql, \$con);     while(\$row_comp = mysql_fetch_assoc(\$res_comp)){         \$Comp[] = \$row_comp['Comp'];    }    echo "<tr><th>&nbsp;</th>";    foreach(\$Comp AS \$Comp){     echo "<th>\$Comp</th>";    }           echo "<tr><td>Total Kg/Compound</td>";  \$Compound = array();  \$sql = "SELECT DISTINCT s.Comp FROM sales_order s, param_settings p WHERE s.Comp = p.Compounds ORDER BY s.Comp";    \$res_comp = mysql_query(\$sql, \$con);     while(\$row_comp = mysql_fetch_assoc(\$res_comp)){         \$Compound[] = \$row_comp['Comp'];    }  foreach(\$Compound AS \$Compound)  {     \$sql_sec = "SELECT SUM(TotalKg) AS TotalKg FROM sales_order  WHERE Comp = '\$Compound' GROUP BY Comp ORDER BY Comp";        # add error code compliant with the rest of error code management you are already using     \$result = mysql_query(\$sql_sec, \$con) ;          while( \$row_sec = mysql_fetch_assoc( \$result ) )     {           \$TotalKg = \$row_sec['TotalKg'];           echo "<td>\$TotalKg</td>";     }  }  echo "</tr>";  echo "<tr>     <td>Working Days</td></tr>"; echo "<tr></tr>"// this part.... ?> ```
Thank you

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•