# Calculate future months

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
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

## Re: Calculate future months

tencip@yahoo.com (Tencip) writes:

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@following domain 315.635-1968(x-211)  "TheBook.Com" (TM)
http://thebook.com /

## Re: Calculate future months

tencip@yahoo.com (Tencip) wrote in message

No need to find today's date for that:

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

Cheers,
NC

## Re: Calculate future months

Meaby the following can inspire you:

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

\$unix_now = strtotime("\$year-\$month-\$day");
\$unix_in_three_months = strtotime("\$year-\$month-\$day +3 months");

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

## Re: Calculate future months

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):

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

\$date_now = strtotime("\$year-\$month-\$day");
\$date_1 = strtotime("\$year-\$month-\$day +1 month");
\$date_2 = strtotime("\$year-\$month-\$day +2 months");
\$date_3 = strtotime("\$year-\$month-\$day +3 months");
\$date_4 = strtotime("\$year-\$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;
?>

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?

## Re: Calculate future months

Hi,

On 31 Aug 2004 15:37:41 -0700, cozimek@hotmail.com (Ozzy) wrote:

(...)

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.
http://sourceforge.net/projects/phpdbedittk /