Re: [PHP] Date Calculation Help



At 6/30/2007 08:14 AM, revDAVE wrote:
I have segmented a year into four quarters (3 months each)

nowdate = the month of the chosen date (ex: 5-30-07 = month 5)

Q: What is the best way to calculate which quarter (1-2-3 or 4) the chosen
date falls on?

Result - Ex: 5-30-07 = month 5 and should fall in quarter 2


If you divide the month number by 3 you get:

1 0.333333333
2 0.666666667
3 1
4 1.333333333
5 1.666666667
6 2
7 2.333333333
8 2.666666667
9 3
10 3.333333333
11 3.666666667
12 4

The first digit is off by one, so subtract .1 from each result:

1 0.233333333
2 0.566666667
3 0.9
4 1.233333333
5 1.566666667
6 1.9
etc.

Now you can see from the first digit that if you take the integer value and add 1 you'll get:

1 1
2 1
3 1
4 2
5 2
6 2
etc.

In PHP this could be:

intval(($month - .1)/3 + 1)
or:
intval(($month + .9)/3)

I believe you can use intval() and floor() interchangeably in this circumstance.

Regards,

Paul
__________________________

Paul Novitski
Juniper Webcraft Ltd.
http://juniperwebcraft.com .