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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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. |
|
|||
|
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. > |
|
|||
|
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 |