how do you get a list of all dates between two dates more than a day apart?

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

    how do you get a list of all dates between two dates more than a day apart?

    Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
    array or all the date between and including those two dates.

    I want to include this list in a query so I need it in a format like:
    '2005-01-01', '2005-01-02',...

    Any ideas? thanks


  • Adam Plocher

    #2
    Re: how do you get a list of all dates between two dates more than a day apart?

    How about something like this....

    <?
    $start = "2005-05-05";
    $end = "2005-06-05";

    $init_date = strtotime($star t);
    $dst_date = strtotime($end) ;

    $offset = $dst_date-$init_date;

    $dates = floor($offset/60/60/24) + 1;

    for ($i = 0; $i < $dates; $i++)
    {
    $newdate = date("Y-m-d", mktime(12,0,0,d ate("m", strtotime($star t)),
    (date("d", strtotime($star t)) + $i), date("Y", strtotime($star t))));
    echo $newdate ."<br>";
    }
    ?>

    Comment

    • Al

      #3
      Re: how do you get a list of all dates between two dates more than a day apart?

      Adam Plocher wrote:[color=blue]
      > How about something like this....
      >
      > <?
      > $start = "2005-05-05";
      > $end = "2005-06-05";
      >
      > $init_date = strtotime($star t);
      > $dst_date = strtotime($end) ;
      >
      > $offset = $dst_date-$init_date;
      >
      > $dates = floor($offset/60/60/24) + 1;
      >
      > for ($i = 0; $i < $dates; $i++)
      > {
      > $newdate = date("Y-m-d", mktime(12,0,0,d ate("m", strtotime($star t)),
      > (date("d", strtotime($star t)) + $i), date("Y", strtotime($star t))));
      > echo $newdate ."<br>";
      > }
      > ?>[/color]

      I'd have done something less mathematical.
      Maybe something like this: (sorry it looks a lot, it isn;t really, I
      just got overenthusiasti c with comments and perfecting it!)

      <?php

      print_r(getAllD ays("2005-01-01", "2005-01-24")); // the original
      problem
      echo "<br /><br />\n\n";
      print_r(getAllD ays("2005-01-11", "2005-03-24")); // going over month
      boundaries
      echo "<br /><br />\n\n";
      print_r(getAllD ays("2005-01-11", "2004-12-14")); // going backwards in
      time
      echo "<br /><br />\n\n";
      print_r(getAllD ays("2000-03-11", "2000-02-11")); // leap year? oh yes!
      echo "<br /><br />\n\n";
      print_r(getAllD ays("2005-01-01", "2005-01-24", false)); // the original
      problem returned as an array

      function getAllDays($sta rt, $end, $aslist = true) {
      // convert the strings we get in to a timestamp
      $start = strtotime($star t);
      $end = strtotime($end) ;

      // this will make sure there isn't an infinite loop by deciding
      // which way (back or forwards one day) the loop should go
      // based on whether the start date is before the end date or not
      $whichway = ($start < $end) ? "tomorrow" : "yesterday" ;

      // we'll increment $curday so set it to the start date
      $curday = $start;

      // initialise the $days array and add our first date to it (could
      be done in one line but looks nicer like this)
      $days = array();
      $days[] = date("Y-m-d", $curday);

      // iterate through the days until we reach the end date
      while ($curday != $end) {
      // get the 'next' day in the sequence (might be forwards OR
      backwards one day)
      $curday = strtotime($whic hway, $curday);
      $days[] = date("Y-m-d", $curday);
      }

      // if we only wanted an array back, return the array now
      if ($aslist === false) return $days;

      // if we wanted a formatted list...

      // inititalise empty string for the list
      $daylist = "";

      // go through each date in the array
      foreach ($days as $day) {
      // add it to the string and stick a comma on the end
      $daylist .= $day.", ";
      }

      // take the trailing comma-space off
      $daylist = substr($daylist , 0, -2);

      return $daylist;
      }

      ?>

      Comment

      • Pedro Graca

        #4
        Re: how do you get a list of all dates between two dates more than a day apart?

        Notgiven wrote:[color=blue]
        > Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
        > array or all the date between and including those two dates.
        >
        > I want to include this list in a query so I need it in a format like:
        > '2005-01-01', '2005-01-02',...[/color]

        <?php
        $d0 = gmmktime(0, 0, 0, 1, 1, 2005);
        $d1 = gmmktime(0, 0, 0, 1, 24, 2005);
        $day = 24*60*60;

        $date_list = array();
        for ($date_index = $d0; $date_index <= $d1; $date_index += $day) {
        $date_list[] = gmdate('Y-m-d', $date_index);
        }

        print_r($date_l ist);
        ?>

        --
        If you're posting through Google read <http://cfaj.freeshell. org/google>

        Comment

        • Ken Robinson

          #5
          Re: how do you get a list of all dates between two dates more than a day apart?

          Pedro Graca wrote:[color=blue]
          > Notgiven wrote:[color=green]
          > > Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
          > > array or all the date between and including those two dates.
          > >
          > > I want to include this list in a query so I need it in a format like:
          > > '2005-01-01', '2005-01-02',...[/color][/color]

          Try this simple function:

          <?php
          $date_array = date_range('10/20/2005','01/31/2006');
          $dates = "'" . implode("','",$ date_array) . "'";
          echo $dates . "<br />\n";

          function date_range($sd, $ed)
          {
          $tmp = array()
          $sdu = strtotime($sd);
          $edu = strtotime($ed);
          while ($sdu <= $edu) {
          $tmp[] = date('Y-m-d',$sdu);
          $sdu = strtotime('+1 day',$sdu);
          }
          }
          ?>

          Ken

          Comment

          • Ken Robinson

            #6
            Re: how do you get a list of all dates between two dates more than a day apart?

            Pedro Graca wrote:[color=blue]
            > Notgiven wrote:[color=green]
            > > Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
            > > array or all the date between and including those two dates.
            > >
            > > I want to include this list in a query so I need it in a format like:
            > > '2005-01-01', '2005-01-02',...[/color][/color]

            Try this simple function:

            <?php
            $date_array = date_range('10/20/2005','01/31/2006');
            $dates = "'" . implode("','",$ date_array) . "'";
            echo $dates . "<br />\n";

            function date_range($sd, $ed)
            {
            $tmp = array()
            $sdu = strtotime($sd);
            $edu = strtotime($ed);
            while ($sdu <= $edu) {
            $tmp[] = date('Y-m-d',$sdu);
            $sdu = strtotime('+1 day',$sdu);
            }
            return ($tmp);
            }
            ?>

            Ken

            Comment

            • Jerry Gitomer

              #7
              Re: how do you get a list of all dates between two dates more thana day apart?

              Notgiven wrote:[color=blue]
              > Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
              > array or all the date between and including those two dates.
              >
              > I want to include this list in a query so I need it in a format like:
              > '2005-01-01', '2005-01-02',...
              >
              > Any ideas? thanks
              >
              >[/color]
              Since you are planning to use the dates in a query (assuming you meant
              a database query) there is no need to get a list of the dates. Use the
              BETWEEN operator in your WHERE clause. For example to select all of the
              dates in January 2005 you can use the following in your queries WHERE
              clause:

              WHERE data_date BETWEEN '2005-01-01' AND '2005-01-31'

              HTH

              Jerry

              Comment

              • Al

                #8
                Re: how do you get a list of all dates between two dates more than a day apart?


                Ken Robinson wrote:[color=blue]
                > Try this simple function:
                >
                > <?php
                > $date_array = date_range('10/20/2005','01/31/2006');
                > $dates = "'" . implode("','",$ date_array) . "'";
                > echo $dates . "<br />\n";
                >
                > function date_range($sd, $ed)
                > {
                > $tmp = array()
                > $sdu = strtotime($sd);
                > $edu = strtotime($ed);
                > while ($sdu <= $edu) {
                > $tmp[] = date('Y-m-d',$sdu);
                > $sdu = strtotime('+1 day',$sdu);
                > }
                > return ($tmp);
                > }
                > ?>
                >
                > Ken[/color]

                That was my simple function :) Mine just got out of control with
                comments and stuff...

                As for the sql BETWEEN operator, I was thinking that too... but I'm not
                100% on sql so I was just kinda thinking "surely it has a between
                operator".

                Comment

                • Notgiven

                  #9
                  Re: how do you get a list of all dates between two dates more than a day apart?

                  "Notgiven" <notreallyme@in valid.invalid> wrote in message
                  news:wacAf.425$ f57.407@bignews 7.bellsouth.net ...[color=blue]
                  > Say you have two dates, 2005-01-01 and 2005-01-24. I want to get a list or
                  > array or all the date between and including those two dates.
                  >
                  > I want to include this list in a query so I need it in a format like:
                  > '2005-01-01', '2005-01-02',...
                  >
                  > Any ideas? thanks[/color]

                  Wow - thanks so much to everyone who posted their ideas and functions!

                  Regarding the use of BETWEEN in sql code, I needed this function in case I
                  can't figure out a way to check for overlapping TIME and DATE interval in
                  the sql code. If I can't, I would simply add records for every day of an
                  event instead of a start and end date. That way, I would only check for
                  overlapping time within each day in the records.

                  Thanks again!


                  Comment

                  Working...