generating a list of dates

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • laredotornado@zipmail.com

    #1

    generating a list of dates

    Hi,

    Using PHP 4.4.4, I want to generate an array that contains strings
    representing days from Jan. 1, 2005 to whatever the current day is.
    So the array would contain the first values

    '2005-01-01', '2005-01-02', '2005-01-03'

    and end with the values

    '2007-01-27', '2007-01-28'

    In case you're interested, I will then take this array and populate a
    MySQL table.

    Thanks for any advice, - Dave

  • Carl Vondrick

    #2
    Re: generating a list of dates

    Should do it. Uses unix time stamps.

    <?php
    function generate_dates( $start, $end)
    {
    $start = strtotime($star t);
    $end = strtotime($end) ;

    $oneday = 86400;

    $return = array();

    for ($current = $start; $current <= $end; $current+=$oned ay)
    {
    $return[] = $current;
    }

    return $return;
    }

    print '<pre>'; print_r(generat e_dates('jan 1, 2005','today')) ;
    ?>


    Hi,
    >
    Using PHP 4.4.4, I want to generate an array that contains strings
    representing days from Jan. 1, 2005 to whatever the current day is.
    So the array would contain the first values
    >
    '2005-01-01', '2005-01-02', '2005-01-03'
    >
    and end with the values
    >
    '2007-01-27', '2007-01-28'
    >
    In case you're interested, I will then take this array and populate a
    MySQL table.
    >
    Thanks for any advice, - Dave
    --
    Carl Vondrick
    Carl Vondrick is a Professor of Computer Science at Columbia University, researching computer vision, machine learning, and AI applications.

    Comment

    • Toby A Inkster

      #3
      Re: generating a list of dates

      laredotornado@z ipmail.com wrote:
      Using PHP 4.4.4, I want to generate an array that contains strings
      representing days from Jan. 1, 2005 to whatever the current day is.
      So the array would contain the first values
      <?php
      $D = array(); // For results
      $f = 'Y-m-d'; // Desired date format
      $d = 1104537600; // 1 Jan 2005, Unix timestamp
      while ($d < time()) // While still in the past...
      {
      $D[] = date($f,$d); // Add this date to the list
      $d += 24*60*60; // The next date is 24 hours later
      }
      print_r($D); // Let's see what we've got!
      ?>

      --
      Toby A Inkster BSc (Hons) ARCS
      Contact Me ~ http://tobyinkster.co.uk/contact
      Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

      * = I'm getting there!

      Comment

      • Heiko Richler

        #4
        Re: generating a list of dates

        Carl Vondrick wrote:
        $oneday = 86400;
        Watch out for daylight-saving time. There may be Days longer or shorter
        than 24h! Never start at Midnight.

        Or use mktime. mktime(0,0,0,2, 0,2007) is Jan. 31. 2007 and
        mktime(0,0,0,1, 32,2007) is Feb. 1.

        Heiko
        --
        http://portal.richler.de/ Namensportal zu Richler
        http://www.richler.de/ Heiko Richler: Computer - Know How!
        http://www.richler.info/ private Homepage

        Comment

        • Toby A Inkster

          #5
          Re: generating a list of dates

          Heiko Richler wrote:
          Watch out for daylight-saving time. There may be Days longer or shorter
          than 24h! Never start at Midnight.
          That's a good point. You can workaround it by using UTC though, as UTC
          doesn't have daylight savings.

          --
          Toby A Inkster BSc (Hons) ARCS
          Contact Me ~ http://tobyinkster.co.uk/contact
          Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

          * = I'm getting there!

          Comment

          • Captain Paralytic

            #6
            Re: generating a list of dates



            On 29 Jan, 07:28, Heiko Richler <heiko-rich...@nefkom. netwrote:
            Carl Vondrick wrote:
            $oneday = 86400;Watch out for daylight-saving time. There may be Days longer or shorter
            than 24h! Never start at Midnight.
            >
            Or use mktime. mktime(0,0,0,2, 0,2007) is Jan. 31. 2007 and
            mktime(0,0,0,1, 32,2007) is Feb. 1.
            >
            Heiko
            --http://portal.richler. de/Namensportal zu Richlerhttp://www.richler.de/ Heiko Richler: Computer - Know How!http://www.richler.info/ private Homepage
            Yes, DST is a real gotcha. I had a similar problem which was discussed
            in this forum a while ago.

            I was advised to use strtotime. My query was regarding adding numbers
            of weeks. In your case you'd use something like:

            $adate = strtotime("+1 day", strtotime($adat e));

            Comment

            • Carl Vondrick

              #7
              Re: generating a list of dates

              Heiko Richler wrote:
              Watch out for daylight-saving time. There may be Days longer or shorter
              than 24h! Never start at Midnight.
              Good point!

              KRAMER: Look how dark it's gettin' already.
              JERRY: Well, it's not Daylight Savings Time yet.
              KRAMER: When does it start?
              JERRY: I don't know, they just tell you the night before.
              KRAMER: Uh. Well, I'm sick o' waiting. I am springin' ahead riiight now.
              JERRY: Oh, I'm sure that won't cause any problems..

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

              Comment

              • Curtis

                #8
                Re: generating a list of dates

                On Jan 28, 9:39 pm, "laredotorn...@ zipmail.com"
                <laredotorn...@ zipmail.comwrot e:
                Hi,
                >
                Using PHP 4.4.4, I want to generate an array that contains strings
                representing days from Jan. 1, 2005 to whatever the current day is.
                So the array would contain the first values
                >
                '2005-01-01', '2005-01-02', '2005-01-03'
                >
                and end with the values
                >
                '2007-01-27', '2007-01-28'
                >
                In case you're interested, I will then take this array and populate a
                MySQL table.
                >
                Thanks for any advice, - Dave
                I thought I'd see if I could figure it out my own version. This seems
                to work:

                function daysUntilNow($m onth, $day, $year, $format = 'Y-m-d') {
                $d = array();
                $ts = 0;

                while ( ($ts = mktime(0,0,0,$m onth,$day,$year )) < time() ){
                $ts = mktime(0,0,0,$m onth,$day,$year );
                $d[] = date($format,$t s);
                $day++; // increment days
                }
                return $d;
                }

                Comment

                • Curtis

                  #9
                  Re: generating a list of dates

                  On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.c omwrote:
                  function daysUntilNow($m onth, $day, $year, $format = 'Y-m-d') {
                  $d = array();
                  $ts = 0;
                  >
                  while ( ($ts = mktime(0,0,0,$m onth,$day,$year )) < time() ){
                  $ts = mktime(0,0,0,$m onth,$day,$year );
                  $d[] = date($format,$t s);
                  $day++; // increment days
                  }
                  return $d;
                  >
                  }
                  Grr, Google Groups borked my code. Pretend there's a closing brace for
                  the function.

                  Comment

                  • Rik

                    #10
                    Re: generating a list of dates

                    Curtis <dyer85@gmail.c omwrote:
                    On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.c omwrote:
                    >function daysUntilNow($m onth, $day, $year, $format = 'Y-m-d') {
                    > $d = array();
                    > $ts = 0;
                    >>
                    > while ( ($ts = mktime(0,0,0,$m onth,$day,$year )) < time() ){
                    > $ts = mktime(0,0,0,$m onth,$day,$year );
                    > $d[] = date($format,$t s);
                    > $day++; // increment days
                    > }
                    > return $d;
                    >>
                    >}
                    >
                    Grr, Google Groups borked my code. Pretend there's a closing brace for
                    the function.
                    There is in real newsreaders :P

                    --
                    Rik Wasmus

                    Comment

                    • Curtis

                      #11
                      Re: generating a list of dates

                      Rik wrote:
                      Curtis <dyer85@gmail.c omwrote:
                      >
                      >On Jan 29, 6:46 pm, "Curtis" <dye...@gmail.c omwrote:
                      >>function daysUntilNow($m onth, $day, $year, $format = 'Y-m-d') {
                      >> $d = array();
                      >> $ts = 0;
                      >>>
                      >> while ( ($ts = mktime(0,0,0,$m onth,$day,$year )) < time() ){
                      >> $ts = mktime(0,0,0,$m onth,$day,$year );
                      >> $d[] = date($format,$t s);
                      >> $day++; // increment days
                      >> }
                      >> return $d;
                      >>>
                      >>}
                      >>
                      >Grr, Google Groups borked my code. Pretend there's a closing brace for
                      >the function.
                      >
                      There is in real newsreaders :P
                      >
                      --Rik Wasmus
                      Yeah, I finally found out my ISP's news server. I'm using Thunderbird
                      as my news client, for the moment, but I still want to check out Opera.

                      I hope Thunderbird is breaking the lines after the right amount of
                      characters.

                      --
                      Curtis, free of Google Groups

                      Comment

                      • Toby A Inkster

                        #12
                        Re: generating a list of dates

                        Curtis wrote:
                        I hope Thunderbird is breaking the lines after the right amount of
                        characters.
                        Yep, and it seems to be using format=flowed too, which can be a bonus for
                        some people.

                        --
                        Toby A Inkster BSc (Hons) ARCS
                        Contact Me ~ http://tobyinkster.co.uk/contact
                        Geek of ~ HTML/CSS/Javascript/SQL/Perl/PHP/Python*/Apache/Linux

                        * = I'm getting there!

                        Comment

                        Working...