looping until today

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

    #1

    looping until today

    I want to loop through dates, stopping at the current date

    I set the enddate like this
    $enddate=date(' Y-m-d');

    which prints out as '2006-04-01'

    $l['cc_startdate'] comes from a DB with the column in date format

    but when i do
    for ($dl=$l['cc_startdate'];$dl < $enddate;$dl++)
    {
    print $dl;
    blah

    }

    it goes on until the day is 99 and then loops again.
    so how do I tell it that $enddate is in the same date format asthe
    start date

  • Carl Vondrick

    #2
    Re: looping until today

    To convert a string to a date, you can use strtotime() to make a timestamp.

    So, for example:

    echo date ('Y-m-d',strotime('8: 30 PM'));

    pauld wrote:
    [color=blue]
    > I want to loop through dates, stopping at the current date
    >
    > I set the enddate like this
    > $enddate=date(' Y-m-d');
    >
    > which prints out as '2006-04-01'
    >
    > $l['cc_startdate'] comes from a DB with the column in date format
    >
    > but when i do
    > for ($dl=$l['cc_startdate'];$dl < $enddate;$dl++)
    > {
    > print $dl;
    > blah
    >
    > }
    >
    > it goes on until the day is 99 and then loops again.
    > so how do I tell it that $enddate is in the same date format asthe
    > start date[/color]

    --
    Carl Vondrick
    Web-Engineer
    Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

    Comment

    • Geoff Berrow

      #3
      Re: looping until today

      Message-ID: <1143876015.314 691.188990@z34g 2000cwc.googleg roups.com> from
      pauld contained the following:
      [color=blue]
      >it goes on until the day is 99 and then loops again.
      >so how do I tell it that $enddate is in the same date format asthe
      >start date[/color]

      Use timestamps

      $enddate=strtot ime('now');

      for ($dl=strtotime( '2006-3-1');$dl < $enddate;$dl=$d l+24*60*60)
      {
      print date('Y-m-d',$dl)."<br>";

      }
      --
      Geoff Berrow (put thecat out to email)
      It's only Usenet, no one dies.
      My opinions, not the committee's, mine.
      Simple RFDs http://www.ckdog.co.uk/rfdmaker/

      Comment

      • pauld

        #4
        Re: looping until today

        i tried strtitme("now ") which prints 1143878058 but the loop still
        goes round and round
        with

        $date=("Y-m-d");
        $enddate=strtot ime($date);

        it prints -1 and no loop at all.

        Comment

        • pauld

          #5
          Re: looping until today

          thank you . I also need to use the date format as a lookup in an sql
          query

          end result that works

          $date=("now");
          $enddate=strtot ime($date);
          //print he dates from start date to present
          for ( $dl=strtotime($ l['cc_startdate']) ;$dl < $enddate ;
          $dl=$dl+24*60*6 0)
          //for ($dl=$l['cc_startdate'];$dl < $enddate;$dl++)
          {$dlookup=date( 'Y-m-d',$dl);
          //////////////////cell for date
          print '<tr><td>'.$dlo okup.'</td>';
          print '<td>';
          {
          $sql2='SELECT * FROM table WHERE date=\''.$dlook up.'\'';

          etc

          Comment

          Working...