By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,824 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

loop last six months

P: n/a
I don't understand why the code below is behaving strangely...any
ideas. It outputs August, July, July, May, May, March?

Thanks,

Chris

echo "<select name=\"month\">\n";
for($i = 0; $i < 6; $i++) {
$str_lastmonth = date("F", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
$val_lastmonth = date("m", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
echo "<option value=\"{$val_lastmonth}\">${str_lastmonth}</option>\n";
}
echo "</select>\n";
Aug 31 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris
<ma*********@googlemail.comwrote:
>I don't understand why the code below is behaving strangely...any
ideas. It outputs August, July, July, May, May, March?

Thanks,

Chris

echo "<select name=\"month\">\n";
for($i = 0; $i < 6; $i++) {
$str_lastmonth = date("F", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
$val_lastmonth = date("m", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
echo "<option value=\"{$val_lastmonth}\">${str_lastmonth}</option>\n";
}
echo "</select>\n";
It would seemingly have worked a few days ago!
Today is the 31st of August, your loop tries to work with the current
day number of the last six months, some of these don't exist. i.e.
31st June doesn't exist, it gets converted to 1st July, hence you get
two instances of July.

--
Regards, Paul Herber, Sandrila Ltd.
Unicode characters http://www.diacrit.sandrila.co.uk/
Aug 31 '08 #2

P: n/a
On 31 Aug, 13:50, Paul Herber <SubstituteMyFirstNameH...@pherber.com>
wrote:
On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris

<matchett...@googlemail.comwrote:
I don't understand why the code below is behaving strangely...any
ideas. It outputs August, July, July, May, May, March?
Thanks,
Chris
echo "<select name=\"month\">\n";
for($i = 0; $i < 6; $i++) {
$str_lastmonth = date("F", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
$val_lastmonth = date("m", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
echo "<option value=\"{$val_lastmonth}\">${str_lastmonth}</option>\n";
}
echo "</select>\n";

It would seemingly have worked a few days ago!
Today is the 31st of August, your loop tries to work with the current
day number of the last six months, some of these don't exist. i.e.
31st June doesn't exist, it gets converted to 1st July, hence you get
two instances of July.

--
Regards, Paul Herber, Sandrila Ltd.
Unicode characters * * * * * * *http://www.diacrit.sandrila..co.uk/- Hide quoted text -

- Show quoted text -
Thanks Paul,

I see the problem now...I will assign an arbitary day date as all I am
looking for is the month details.

Cheers,

Chris
Aug 31 '08 #3

P: n/a
Chris wrote:
I don't understand why the code below is behaving strangely...any
ideas. It outputs August, July, July, May, May, March?

Thanks,

Chris

echo "<select name=\"month\">\n";
for($i = 0; $i < 6; $i++) {
$str_lastmonth = date("F", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
$val_lastmonth = date("m", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
echo "<option value=\"{$val_lastmonth}\">${str_lastmonth}</option>\n";
}
echo "</select>\n";
Today (8/31), your mktime expression comes out to:

8/31/2008
7/31/2008
6/31/2008
5/31/2008
4/31/2008
3/31/2008

Since 6/31/2008 and 4/31/2008 don't exist, PHP adjusts to the equivalent
(from the start of the year), which would be 5/1/2008 and 5/1/2008,
respectively.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Aug 31 '08 #4

P: n/a
Chris wrote:
I don't understand why the code below is behaving strangely...any
ideas. It outputs August, July, July, May, May, March?

Thanks,

Chris

echo "<select name=\"month\">\n";
for($i = 0; $i < 6; $i++) {
$str_lastmonth = date("F", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
$val_lastmonth = date("m", mktime(0,0,0,date("m")-
$i,date("d"),date("Y")));
echo "<option value=\"{$val_lastmonth}\">${str_lastmonth}</option>\n";
}
echo "</select>\n";
Just me, but I'd rather clutter up the for loop logic a little than
the block. Also, no need to include the year, if you mean the current
year.

$monthsPrior = 6;

for ( $i = date('n'); $i (date('n') - $monthsPrior); $i-- ) {
$monthFull = date('F', mktime(0,0,0,$i));
$monthVal = date('m', mktime(0,0,0,$i));

// ...
}

--
Curtis
Sep 1 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.