|  | loop last six months |  | |
| | | Chris |  |
| Posted: Sun Aug 31, 2008 11:12 am Post subject: loop last six months |  |
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"; |
| |
| | | Paul Herber |  |
| Posted: Sun Aug 31, 2008 11:12 am Post subject: Re: loop last six months |  |
On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris <matchett123@googlemail.com> wrote:
| Quote: | 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 LINK |
| |
| | | Jerry Stuckle |  |
| Posted: Sun Aug 31, 2008 11:17 am Post subject: Re: loop last six months |  |
Chris wrote:
| Quote: | 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. jstucklex@attglobal.net ================== |
| |
| | | Chris |  |
| Posted: Sun Aug 31, 2008 1:00 pm Post subject: Re: loop last six months |  |
| |  | |
On 31 Aug, 13:50, Paul Herber <SubstituteMyFirstNameH...@pherber.com> wrote:
| Quote: | On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris
matchett...@googlemail.com> 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";
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 |
| |
| | | Curtis |  |
| Posted: Mon Sep 01, 2008 5:32 am Post subject: Re: loop last six months |  |
Chris wrote:
| Quote: | 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 |
| |
|
|