Calculate future months

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

    Calculate future months

    Hi everyone,

    I'm trying to build a simple script that does the following. It
    should find today's month and year, and then go into a DB query string
    and look for all records that are from this month and the next three
    months.

    I've been playing around with the date() function for a while now, and
    from that I can pull today's month and year, but I'm having problems
    with the transition from December to January. For instance, when i
    use the date function to get the current month, which for example say
    is December, when i try to get the third month after that, my current
    function adds 3 to the current month (which is 12), and gets 15. I'm
    not sure what quite to do to make a simple function that will wrap
    back around to January (which would be 1). Additionally, at the end
    of the year, the year function has to change as well. So, three
    months after December 2004 is March 2005, so the calculation needs to
    recognize that because my month has gone over 12, I need to add 1 to
    my year.

    Anyone face something similar to this?

    Best,
    tencip
  • Tim Van Wassenhove

    #2
    Re: Calculate future months

    In article <3b7fde0.040831 0525.c03c6d7@po sting.google.co m>, Tencip wrote:[color=blue]
    > Hi everyone,
    >
    > I'm trying to build a simple script that does the following. It
    > should find today's month and year, and then go into a DB query string
    > and look for all records that are from this month and the next three
    > months.
    >
    > I've been playing around with the date() function for a while now, and
    > from that I can pull today's month and year, but I'm having problems
    > with the transition from December to January. For instance, when i
    > use the date function to get the current month, which for example say
    > is December, when i try to get the third month after that, my current
    > function adds 3 to the current month (which is 12), and gets 15. I'm
    > not sure what quite to do to make a simple function that will wrap
    > back around to January (which would be 1). Additionally, at the end
    > of the year, the year function has to change as well. So, three
    > months after December 2004 is March 2005, so the calculation needs to
    > recognize that because my month has gone over 12, I need to add 1 to
    > my year.[/color]

    Meaby the following can inspire you:

    $time = mktime();
    $day = date('j', $time);
    $month = date('m', $time);
    $year = date('Y', $time);

    $unix_now = strtotime("$yea r-$month-$day");
    $unix_in_three_ months = strtotime("$yea r-$month-$day +3 months");


    --
    Tim Van Wassenhove <http://home.mysth.be/~timvw>

    Comment

    • John Murtari

      #3
      Re: Calculate future months

      tencip@yahoo.co m (Tencip) writes:
      [color=blue]
      > Hi everyone,
      >
      > I'm trying to build a simple script that does the following. It
      > should find today's month and year, and then go into a DB query string
      > and look for all records that are from this month and the next three
      > months.
      >
      > I've been playing around with the date() function for a while now, and
      > from that I can pull today's month and year, but I'm having problems
      > with the transition from December to January. For instance, when i
      > use the date function to get the current month, which for example say
      > is December, when i try to get the third month after that, my current
      > function adds 3 to the current month (which is 12), and gets 15. I'm
      > not sure what quite to do to make a simple function that will wrap
      > back around to January (which would be 1). Additionally, at the end
      > of the year, the year function has to change as well. So, three
      > months after December 2004 is March 2005, so the calculation needs to
      > recognize that because my month has gone over 12, I need to add 1 to
      > my year.
      >
      > Anyone face something similar to this?[/color]

      You can try to do the math in your PHP code, but it gets
      to be a mess sometimes with leap years, etc... If you are connecting
      to a DB (maybe MySQL), most have date functions that will do all
      that for you, i.e.

      mysql> select now();
      +---------------------+
      | now() |
      +---------------------+
      | 2004-08-31 11:20:43 |
      +---------------------+
      1 row in set (0.00 sec)

      mysql> select adddate(now(), interval 5 day);
      +--------------------------------+
      | adddate(now(), interval 5 day) |
      +--------------------------------+
      | 2004-09-05 11:20:47 |
      +--------------------------------+
      1 row in set (0.00 sec)

      mysql> select adddate(now(), interval 5 month);
      +----------------------------------+
      | adddate(now(), interval 5 month) |
      +----------------------------------+
      | 2005-01-31 11:20:50 |
      +----------------------------------+
      1 row in set (0.00 sec)

      mysql> select adddate(now(), interval 5 year); << NO LEAP YEARS
      +---------------------------------+
      | adddate(now(), interval 5 year) |
      +---------------------------------+
      | 2009-08-31 11:20:52 |
      +---------------------------------+
      1 row in set (0.00 sec)

      mysql> select adddate(now(), interval 1825 day); << LEAP YEAR INCLUDED
      +-----------------------------------+
      | adddate(now(), interval 1825 day) |
      +-----------------------------------+
      | 2009-08-30 11:22:48 |
      +-----------------------------------+
      1 row in set (0.00 sec)


      You can use these in your query as part of a where
      clause, or just do them 'as is' to get a result and use the
      php time conversion functions.

      Hope this helps!

      --
      John
      _______________ _______________ _______________ _______________ _______
      John Murtari Software Workshop Inc.
      jmurtari@follow ing domain 315.635-1968(x-211) "TheBook.Co m" (TM)
      Own TheBook.com today. Secure checkout and guided transfer support. No hidden fees.

      Comment

      • Nikolai Chuvakhin

        #4
        Re: Calculate future months

        tencip@yahoo.co m (Tencip) wrote in message
        news:<3b7fde0.0 408310525.c03c6 d7@posting.goog le.com>...[color=blue]
        >
        > I'm trying to build a simple script that does the following. It
        > should find today's month and year, and then go into a DB query string
        > and look for all records that are from this month and the next three
        > months.[/color]

        No need to find today's date for that:

        $three_months_l ater = date('Y-m-d', strtotime('+3 months'));

        Cheers,
        NC

        Comment

        • Ozzy

          #5
          Re: Calculate future months

          Tim Van Wassenhove <euki@pi.be> wrote in message news:<2pjf56Fli ap9U1@uni-berlin.de>...[color=blue]
          > In article <3b7fde0.040831 0525.c03c6d7@po sting.google.co m>, Tencip wrote:[color=green]
          > > Hi everyone,
          > >
          > > I'm trying to build a simple script that does the following. It
          > > should find today's month and year, and then go into a DB query string
          > > and look for all records that are from this month and the next three
          > > months.
          > >
          > > I've been playing around with the date() function for a while now, and
          > > from that I can pull today's month and year, but I'm having problems
          > > with the transition from December to January. For instance, when i
          > > use the date function to get the current month, which for example say
          > > is December, when i try to get the third month after that, my current
          > > function adds 3 to the current month (which is 12), and gets 15. I'm
          > > not sure what quite to do to make a simple function that will wrap
          > > back around to January (which would be 1). Additionally, at the end
          > > of the year, the year function has to change as well. So, three
          > > months after December 2004 is March 2005, so the calculation needs to
          > > recognize that because my month has gone over 12, I need to add 1 to
          > > my year.[/color]
          >
          > Meaby the following can inspire you:
          >
          > $time = mktime();
          > $day = date('j', $time);
          > $month = date('m', $time);
          > $year = date('Y', $time);
          >
          > $unix_now = strtotime("$yea r-$month-$day");
          > $unix_in_three_ months = strtotime("$yea r-$month-$day +3 months");[/color]

          Alright, tried the code below that go me moving...but it seems that
          it's really not adding "1 month", but rather something like 31 days or
          something. Below is my code, and then the results (taken on August
          31, 2004):
          [color=blue][color=green][color=darkred]
          >>>>>CODE<<<<<< <<[/color][/color][/color]

          <?
          $time = mktime();
          $day = date('j', $time);
          $month = date('m', $time);
          $year = date('Y', $time);

          $date_now = strtotime("$yea r-$month-$day");
          $date_1 = strtotime("$yea r-$month-$day +1 month");
          $date_2 = strtotime("$yea r-$month-$day +2 months");
          $date_3 = strtotime("$yea r-$month-$day +3 months");
          $date_4 = strtotime("$yea r-$month-$day +4 months");

          $month_date = date('m', $date_now);
          $month_date_1 = date('m', $date_1);
          $month_date_2 = date('m', $date_2);
          $month_date_3 = date('m', $date_3);
          $month_date_4 = date('m', $date_4);

          $year_date = date('Y', $date_now);
          $year_date_1 = date('Y', $date_1);
          $year_date_2 = date('Y', $date_2);
          $year_date_3 = date('Y', $date_3);
          $year_date_4 = date('Y', $date_4);

          echo "Today's Month/Year<br>";
          echo $month_date;
          echo $year_date;

          echo "<br><br>+1 Month/Year<br>";
          echo $month_date_1;
          echo $year_date_1;

          echo "<br><br>+2 Month/Year<br>";
          echo $month_date_2;
          echo $year_date_2;

          echo "<br><br>+3 Month/Year<br>";
          echo $month_date_3;
          echo $year_date_3;

          echo "<br><br>+4 Month/Year<br>";
          echo $month_date_4;
          echo $year_date_4;
          ?>

          [color=blue][color=green][color=darkred]
          >>>>>RESULTS<<< <<<<<[/color][/color][/color]
          Today's Month/Year
          082004

          +1 Month/Year
          102004 //SHOULD BE 092004

          +2 Month/Year
          102004

          +3 Month/Year
          122004 //SHOULD BE 112004

          +4 Month/Year
          122004


          Any ideas why this is?

          Comment

          • Jochen Daum

            #6
            Re: Calculate future months

            Hi,

            On 31 Aug 2004 15:37:41 -0700, cozimek@hotmail .com (Ozzy) wrote:
            [color=blue][color=green]
            >>
            >> Meaby the following can inspire you:
            >>
            >> $time = mktime();
            >> $day = date('j', $time);
            >> $month = date('m', $time);
            >> $year = date('Y', $time);
            >>
            >> $unix_now = strtotime("$yea r-$month-$day");
            >> $unix_in_three_ months = strtotime("$yea r-$month-$day +3 months");[/color]
            >
            >Alright, tried the code below that go me moving...but it seems that
            >it's really not adding "1 month", but rather something like 31 days or
            >something. Below is my code, and then the results (taken on August
            >31, 2004):
            >[/color]

            (...)[color=blue][color=green][color=darkred]
            >>>>>>RESULTS<< <<<<<<[/color][/color]
            >Today's Month/Year
            >082004
            >
            >+1 Month/Year
            >102004 //SHOULD BE 092004
            >
            >+2 Month/Year
            >102004
            >
            >+3 Month/Year
            >122004 //SHOULD BE 112004
            >
            >+4 Month/Year
            >122004
            >[/color]


            [color=blue]
            >
            >Any ideas why this is?[/color]

            Define which day you want to have used, when you are on a day, which
            is not in all months of the year.


            HTH,

            Jochen
            --
            Jochen Daum - Cabletalk Group Ltd.
            PHP DB Edit Toolkit -- PHP scripts for building
            database editing interfaces.
            Download PHP DB Edit Toolkit for free. PHP DB Edit Toolkit is a set of PHP classes makes the generation of database edit interfaces easier and faster. The main class builds tabular and form views based on a data dictionary and takes over handling of insert/update/delete and user input.

            Comment

            Working...