calculating complete quarters between two dates

This is a discussion on calculating complete quarters between two dates within the PHP General forums, part of the PHP Programming Forums category; Hi, I need to write a function to calculate the number of complete quarters between two given dates. As the ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 05-12-2006
neil@invidion.co.uk
 
Posts: n/a
Default calculating complete quarters between two dates

Hi,

I need to write a function to calculate the number of complete quarters
between two given dates.

As the calculations are based on tax rules, they must be actual
calender months - not approximations.

Therefore, something, assuming I run the POSTed values through
'mktime', an instruction like:

$months = ceil((($end_date - $start_date) / 60 / 60 / 24 / 365.25 *
12))

would give an approximation. However, in some instances where there are
28 or 30 days in a month, it may not give the correct answer.

I've had a look in the PHP documentation, but there appears to be no
inbuilt functions to handle this. Can any correct me on this if I am
wrong ?

Otherwise, I guess the only way would be to use lots of conditional
logic, working out whether the difference between the start month and
end month, then if end date is higher than the start date, etc...

If anyone has done something similar, please let me know.

Rgds
Neil.

Reply With Quote
  #2 (permalink)  
Old 05-25-2006
Karel de Vos
 
Posts: n/a
Default Re: calculating complete quarters between two dates

You might use floor((date("m",$time)-1)/4)+1 for getting the quarter
based on months. A function like

function qsincey0($time) {
return date("Y",$time)*floor((date("m",$time)-1)/4);
}

Would return the number of quarters since year 0

$qdiff = qsincey0($date2)-qsincey0($date1)

Would give the difference (0 for the dates being in the same quarter)
If you would want the dates 1-Jan-2006 and 31-Mar-2006 give '1 quarter
difference' instead of 0 (for being in the same quarter), you may consider:

$qdiff = qsincey0(strtotime("-1 day",$date2))-qsincey0(strtotime("+1
day"),$date1) - 1;

Which brings those particular dates outside the quarter, giving a
'quarter difference' of 2, leaving 1 to substract.

Regards,
Karel

neil@invidion.co.uk wrote:
> Hi,
>
> I need to write a function to calculate the number of complete quarters
> between two given dates.
>
> As the calculations are based on tax rules, they must be actual
> calender months - not approximations.
>
> Therefore, something, assuming I run the POSTed values through
> 'mktime', an instruction like:
>
> $months = ceil((($end_date - $start_date) / 60 / 60 / 24 / 365.25 *
> 12))
>
> would give an approximation. However, in some instances where there are
> 28 or 30 days in a month, it may not give the correct answer.
>
> I've had a look in the PHP documentation, but there appears to be no
> inbuilt functions to handle this. Can any correct me on this if I am
> wrong ?
>
> Otherwise, I guess the only way would be to use lots of conditional
> logic, working out whether the difference between the start month and
> end month, then if end date is higher than the start date, etc...
>
> If anyone has done something similar, please let me know.
>
> Rgds
> Neil.
>

Reply With Quote
  #3 (permalink)  
Old 05-29-2006
ctclibby
 
Posts: n/a
Default Re: calculating complete quarters between two dates

Ummm

Taxed based quarters in the USoA start ( and stop ) on known days of
the year.

Can't you compare for this?
1st quarter - 1/1/year
2nd quarter - 1/4/year
3rd quarter - 1/7/year
4th quarter - 1/10/year

If your date is >= 1st AND < 3rd, wouldn't that hold 2 complete
quarters?

todh

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 12:45 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0