loop last six months

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris

    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,da te("m")-
    $i,date("d"),da te("Y")));
    $val_lastmonth = date("m", mktime(0,0,0,da te("m")-
    $i,date("d"),da te("Y")));
    echo "<option value=\"{$val_l astmonth}\">${s tr_lastmonth}</option>\n";
    }
    echo "</select>\n";
  • Paul Herber

    #2
    Re: loop last six months

    On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris
    <matchett123@go oglemail.comwro te:
    >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_lastmon th = date("F", mktime(0,0,0,da te("m")-
    >$i,date("d"),d ate("Y")));
    >$val_lastmon th = date("m", mktime(0,0,0,da te("m")-
    >$i,date("d"),d ate("Y")));
    >echo "<option value=\"{$val_l astmonth}\">${s tr_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/

    Comment

    • Chris

      #3
      Re: loop last six months

      On 31 Aug, 13:50, Paul Herber <SubstituteMyFi rstNameH...@phe rber.com>
      wrote:
      On Sun, 31 Aug 2008 04:12:40 -0700 (PDT), Chris
      >
      >
      >
      >
      >
      <matchett...@go oglemail.comwro te:
      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,da te("m")-
      $i,date("d"),da te("Y")));
      $val_lastmonth = date("m", mktime(0,0,0,da te("m")-
      $i,date("d"),da te("Y")));
      echo "<option value=\"{$val_l astmonth}\">${s tr_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

      Comment

      • Jerry Stuckle

        #4
        Re: loop last six months

        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,da te("m")-
        $i,date("d"),da te("Y")));
        $val_lastmonth = date("m", mktime(0,0,0,da te("m")-
        $i,date("d"),da te("Y")));
        echo "<option value=\"{$val_l astmonth}\">${s tr_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@attgl obal.net
        =============== ===

        Comment

        • Curtis

          #5
          Re: loop last six months

          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,da te("m")-
          $i,date("d"),da te("Y")));
          $val_lastmonth = date("m", mktime(0,0,0,da te("m")-
          $i,date("d"),da te("Y")));
          echo "<option value=\"{$val_l astmonth}\">${s tr_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

          Comment

          Working...