Jump to content

Convert Month Number to Month Name in a Range


newphpcoder

Recommended Posts

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.

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

Deep breath.

 

Look at the two lines of code I pointed out. You see them? Good. Now, tell me: what is the difference between them? They are not identical. How are they not identical? Explain to me, in English, what both lines do in as much detail as you can. Just do something that requires you actually looking at them and reading the code.

 

If you haven't realized, I'm trying to show you why one works and the other looks like it doesn't work. (Because it does work.)

Link to comment
Share on other sites

I was looking for the more specific "one uses implode() and one doesn't" but that's close enough.

 

implode() turns an array into a string, right? monthNames() returns an array, right? If you echo an array you just get "Array", right? (The answer to all three is "yes", by the way.)

Try using implode() on the one line that doesn't seem to work. Just like how you use it on the other line that does work.

Link to comment
Share on other sites

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;
}

$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:

 

<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> </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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.