473,396 Members | 1,760 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 3132
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: bin_P19 P | last post by:
the code i have got is as follows and now im stuck <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Shopping...
26
by: Frank | last post by:
For my website i would like to display the age of my son in years, months, days and hours. For now i manage to get a result for totals. Like the total number of days. This is the beginning: ...
6
by: charliewest | last post by:
Can someone pls point me to or recommend the easiest way to calculate someone´s age using the TimeSpan object, in .NET CF? Isn´t there a simple way to use the TimeSpan object to calculate the...
0
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
4
by: kux | last post by:
Hello everyone, I hope someone is out here who can help me with a simple calculation... I have a sales data base in access with monthly sales history by product. to make future predictions I...
6
by: rohayre | last post by:
Im a long time java developer and actually have never done anything with java scripting. I'd like to write a short simple script for calculating a date in the future based on today's date and a...
4
by: MCLR | last post by:
Good day to all: I found your website a couple of days ago as I was searching for resources that can help within my particular problem in writing a script for an Access Query. The issue is to...
4
by: neilj | last post by:
I would like to create a field that calculates a date that is 39 months from a date I am entering on a form. Once I fill in a date in a field, I would like to have Access calculate the next field...
5
by: Mike | last post by:
I use c#, V2005 How I can get difference between two dates and get value in month(s) I had found some solutions but it is not exactly what I need. private static int...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.