469,950 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

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
Jul 17 '05 #1
5 2950
In article <3b************************@posting.google.com>, Tencip wrote:
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.


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>
Jul 17 '05 #2
te****@yahoo.com (Tencip) writes:
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?


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/
Jul 17 '05 #3
te****@yahoo.com (Tencip) wrote in message
news:<3b************************@posting.google.co m>...

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.


No need to find today's date for that:

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

Cheers,
NC
Jul 17 '05 #4
Tim Van Wassenhove <eu**@pi.be> wrote in message news:<2p************@uni-berlin.de>...
In article <3b************************@posting.google.com>, Tencip wrote:
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.


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");


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):
>CODE<<<<<<<<
<?
$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;
?>

RESULTS<<<<<<<<

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?
Jul 17 '05 #5
Hi,

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

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


(...)
>>RESULTS<<<<<<<<

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?


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/
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

26 posts views Thread by Frank | last post: by
6 posts views Thread by charliewest | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.