This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
khosb/application/classes/Period.php

131 lines
3.1 KiB
PHP
Raw Normal View History

2010-11-29 22:41:08 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This is class is for calculating date periods.
*
* @package OSB
* @subpackage Utilities
* @category Helpers
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
class Period {
/**
* Calculate both the start and end dates for a billing period
* and the pro-rata percentage.
*
* See [StaticList_RecurSchedule]
*
* @param StaticList_RecurSchedule Period Type [StaticList_RecurSchedule]
* @param int Starting date if recurring must start on a day of the month
* @param datetime Date to start calculating from, otherwise now() is used
* @param boolean Show dates in 'Y-m-d' format
* @return array
*/
public static function details($type,$weekday=NULL,$start=NULL,$df=FALSE,$strict=FALSE) {
2010-11-29 22:41:08 +00:00
// Round the time integer to a whole day.
if (is_null($start))
$start = strtotime('today');
else
$start = strtotime(date('Y-m-d',$start));
$inc_months = $used_months = 0;
2010-11-29 22:41:08 +00:00
switch ($type) {
// Weekly
case 0:
if ($strict)
throw new Kohana_Exception('Strict doesnt work here');
$period_start = is_null($weekday) ? $start : $start+86400*($weekday-date('w',$start));
$period_end = $period_start+(86400*7);
break;
2010-11-29 22:41:08 +00:00
// Monthly
2010-11-29 22:41:08 +00:00
case 1:
// NOTE: Strict doesnt do anything here.
2010-11-29 22:41:08 +00:00
$inc_months = 1;
break;
// Quarterly
2010-11-29 22:41:08 +00:00
case 2:
$inc_months = 3;
break;
// Half Yearly
2010-11-29 22:41:08 +00:00
case 3:
$inc_months = 6;
break;
// Yearly
2010-11-29 22:41:08 +00:00
case 4:
$inc_months = 12;
break;
// Biennial
2010-11-29 22:41:08 +00:00
case 5:
$inc_months = 24;
break;
// Triennial
2010-11-29 22:41:08 +00:00
case 6:
if ($strict)
throw new Kohana_Exception('Strict not implemented here');
2010-11-29 22:41:08 +00:00
$inc_months = 36;
break;
default:
return FALSE;
}
// Workout the period start day
2010-11-29 22:41:08 +00:00
if (is_null($weekday))
$weekday = $strict ? 1 : date('d',$start);
2010-11-29 22:41:08 +00:00
// We only work our used_months for periods monthly or more.
if ($inc_months) {
if ($strict)
$used_months = $inc_months-(($inc_months-(date('n',$start)%$inc_months))%$inc_months+1);
2010-11-29 22:41:08 +00:00
$d = mktime(0,0,0,date('m',$start)-$used_months,$weekday,date('y',$start));
if ($d <= $start)
$period_start = $d;
else
$period_start = mktime(0,0,0,date('m',$d)-1-$used_months,$weekday,date('y',$d));
2010-11-29 22:41:08 +00:00
// Workout the period end
$period_end = mktime(0,0,0,date('m',$period_start)+$inc_months,$weekday,date('y',$period_start));
}
2010-11-29 22:41:08 +00:00
$total_time = $period_end-$period_start;
$remain_time = $period_end-$start;
$used_time = $start-$period_start;
// Change our end date to the day before
$period_end -= 86400;
$return = array(
'start'=>$period_start,
'start_time'=>$start,
'date'=>$start,
'end'=>$period_end,
'end_time'=>$period_end,
'weekday'=>$weekday,
'prorata'=>round($remain_time/$total_time,4),
'total_time'=>sprintf('%3.1f',$total_time/86400),
'remain_time'=>sprintf('%3.1f',$remain_time/86400),
'used_time'=>sprintf('%3.1f',$used_time/86400),
);
2010-11-29 22:41:08 +00:00
if ($df)
foreach (array('start','date','end') as $key)
$return[$key] = Config::date($return[$key]);
2010-11-29 22:41:08 +00:00
return $return;
}
}
?>