/    Sign up×
Community /Pin to ProfileBookmark

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.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@NogDogApr 27.2012 — probably the simplest way would be to create an indexed array of month names, e.g.:
[code=php]
$monthNames = array(
1 => 'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);
[/code]

Then, for example, if you need the name of the 7th month:
[code=php]
echo $monthNames[7];
[/code]
Copy linkTweet thisAlerts:
@newphpcoderauthorApr 28.2012 — Working days is SUM of workingdays of 3 months, I have code for getting the consecutive 3 months but now I got a problem in getting the SUM of working days.

here is my code for that:


[code=php]
echo "<tr><td>Working Days</td>";
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>';

$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE Month_Name = '$month' ORDER BY MonthName";
$res = mysql_query($sql, $con);

$row = mysql_fetch_assoc($res);

$WorkingDays = $row['WorkingDays'];

echo "<td>$WorkingDays</td>";

}
[/code]


but I got this error:

Apr

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:Program FilesxampphtdocsMES PROJECTHalfShellParam.php on line 147

May

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:Program FilesxampphtdocsMES PROJECTHalfShellParam.php on line 147

Jun

Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in D:Program FilesxampphtdocsMES PROJECTHalfShellParam.php on line 147

Thank you
Copy linkTweet thisAlerts:
@NogDogApr 28.2012 — One of your queries is failing to process, so add some debug code to find out why:
[code=php]
$res = mysql_query($sql);
if($res == false) {
throw new Exception("Query failed: ".mysql_error()."n$sql");
}
[/code]
Copy linkTweet thisAlerts:
@newphpcoderauthorApr 29.2012 — Hi..

I resolved it now using thid code:

[code=php]
//-----Computation of Working Days---//
$sql = "SELECT FromMonth, ToMonth FROM so_month";
$res = mysql_query($sql,$con);

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

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

foreach( monthNames($FromMonth, $ToMonth) as $month){
$sql = "SELECT MonthName, SUM(WorkingDays) AS WorkingDays FROM working_days WHERE MonthName IN ('$month') GROUP BY MonthName";
$res = mysql_query($sql, $con);
$SumWorkingDays = 0;
while($row = mysql_fetch_array($res)){

$WorkingDays+=(int)$row['WorkingDays'];
$SumWorkingDays += $WorkingDays;

}
}
[/code]


Thank you
×

Success!

Help @newphpcoder spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...