newphpcoder Posted April 27, 2012 Share Posted April 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/ Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 Here's a start: array("", "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December") There are smarter solutions but they aren't quite as... intuitive. Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340964 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340967 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 echo implode(", ",monthNames(5,7)); echo monthNames($FromMonth, $ToMonth); There's a major difference between those two lines. Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340969 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 What is their difference? Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340971 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 Are you not looking at the code? Okay... Let me try adding some spacing to it. echo implode(", ", monthNames(5 , 7 )); echo monthNames($FromMonth, $ToMonth) ; Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340975 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 I tried this: echo $Month = monthNames($FromMonth, $ToMonth) ; still the output is Array Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340979 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 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.) Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340983 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 this code: echo implode(", ",monthNames(5,7)); get the MonthsName separated my , echo monthNames($FromMonth, $ToMonth) ; want's to display the output from the function Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340984 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340988 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 ok thank you.. Now how can I put the 3 months in table <td> for example: May June Jul Thank you Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1340992 Share on other sites More sharing options...
requinix Posted April 27, 2012 Share Posted April 27, 2012 I hesitate to ask but what have you tried so far? Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1341000 Share on other sites More sharing options...
newphpcoder Posted April 27, 2012 Author Share Posted April 27, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/261679-convert-month-number-to-month-name-in-a-range/#findComment-1341001 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.