PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
When I add this to my page, it clears any responses that I'm already getting from the main page. I'm basically adjusting UNIX time stamps.
class Exception {
function __construct();
final public getMessage();
final public getCode();
final public getFile();
final public getLine();
final public getTrace();
final public getTraceAsString();
protected $message;
protected $code;
protected $file;
protected $line;
}
class Paydate_Calculator {
###
# Weekend
# determines if day is a Saturday or a Sunday
# Then calls LoopType passing "Forward" as its argument
###
*/
private function Weekend() {
if ((date('D',$this->pay_day) == "Saturday") || (date('D',$this->pay_day) == "Sunday"))
LoopType("Forward");
if ($this->pay_day == NULL) {
throw new Exception("In function Weekend, pay_day is null");
}
}
###
# LoopType
# adds a day or subtracts a day to the payday date depending on
# whether we recieve "Forward" or "Reverse" as its argument
###
private function LoopType($type) {
switch ($type) (
case "Forward":
$this->pay_day = $this->pay_day + DAY;
break;
case "Reverse":
$this->pay_day = $this->pay_day - DAY;
break;
}
if($type == NULL) {
throw new Exception("type is NULL");
}
}
###
# CheckIfHoliday
# if pay-day is a Monday we loop throught the Holiday array
# to see if the date matches a holiday. If so we add a day
# if it isn't we subtract a day
###
private function CheckIfHoliday() {
$blnIsHoliday = 0;
if(date('N',$this->pay_day) == 1) {
for ($i = 0; $i < count($this->holiday_array); $i++) {
// add one more day if holiday occurs on a Monday
if ((date("N",$this->pay_day) == 1) && ($this->pay_day == $this->holiday_array[$i])) {
LoopType("Forward");
$blnIsHoliday = 1;
} else {
LoopType("Reverse");
$blnIsHoliday = 1;
}
}
}
return $blnIsHoliday;
if ($this->payday == NULL} {
throw new Exception("In function CheckIfHoliday, pay_day is null");
}
}
###
# setDueDate
# takes the orginal date of the loan and adds 10 days to set the 1st due date
# then we determine how many intervals there are from then until today
# We then keep adding ten days per loop to he orginal due date until we are
# current with today.
###
private function setDueDate($type) {
// now
$now = time();
//Set 1st Due Date
$original_due_date = $this->fund_day + self::TENDAYS;
// if due_date occured before today calculate next due_date
While ($original_due_date < $now) {
$this->due_date = $original_due_date + self::TENDAYS;
}
if ($this->due-day == NULL) {
throw new Exception("I function setDuedate due_date is null");
}
}
###
# tunePayDay
# if the client does not desire direct deposit, we add 10 days to the date
# we then call Weekend() to determine if its a saturday
# we again call Weekend() to determine if its a sunday
# finally we check if it's a Holiday
###
private function tunePayDay() {
if (!$this->direct_deposit) { $this->pay_day = $this->pay_day + DAY; }
// check if Saturday if yes, Loop forward
Weekend();
// check if Sunday if yes, Loop forward
Weekend();
// check if it's a Monday and a Holiday, then move it forward else move it backward
CheckIfHoliday();
}
###
# Calculate_Due_Date
# we set all the class variables
# we then set 1st due date to fund_day + pay_span and make it current
# we now fine tune the pay date for weekends and holidays
# returning true or false if this waas accomplished
# We call all thrown error and print them to the page.
###
public function Calculate_Due_Date($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit) {
try {
$this->fund_day = $fund_day;
$this->holiday_array = $holiday_array;
$this->pay_span = $pay_span;
$this->pay_day = $pay_day;
$this->direct_deposit = $direct_deposit;
try {
// set 1st due date to fund_day + pay_span and make it current
setDueDate();
tunePayDay();
if ($this->pay_day >= $this->due_date) {
return 1;
} else {
$this->pay_day = $this->pay_day + $this->pay_span;
$tunePayDay();
return 0;
}
} catch (Exception $exception) {
print $exception->getMessage();
print " in file " . $exception->getFile();
print " on line " . $exception->getLine() . "\n";
}
}
###
# Get_due_date
# I added this to try and test whether the due date is being set
# we then the constructor Calculate_Due_Date is called
###
function Get_due_date() {
return $this->due_date;
}
const DAY = (1*24*60*60);
const TENDAYS = (10*24*60*60);
private $due_date;
private $fund_day;
private $holiday_array;
private $pay_span;
private $pay_day;
private $direct_deposit;
};
On the main page I'm instantiating the class as follows passing in UNIX time stamps:
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
<?php
class Exception {
public function __construct();
final public getMessage();
final public getCode();
final public getFile();
final public getLine();
public $message;
public $code;
public $file;
public $line;
}
class NullHandleException extends Exception {
public function __construct($message)
{
parent::__construct($message);
}
}
class Paydate_Calculator {
###
# __construct
# we set all the class variables here
###
public function __construct($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit)
{
$this->fund_day = $fund_day;
$this->holiday_array = $holiday_array;
$this->pay_span = $pay_span;
$this->pay_day = $pay_day;
$this->direct_deposit = $direct_deposit;
}
###
# setDueDate
# takes the orginal date of the loan and adds 10 days to set the 1st due date
# then we determine how many intervals there are from then until today
# We then keep adding ten days per loop to he orginal due date until we are
# current with today.
###
public function setDueDate() {
// now
$now = time();
//Set 1st Due Date
$original_due_date = $this->fund_day + TENDAYS;
// if due_date occured before today calculate next due_date
While ($original_due_date < $now) {
$this->due_date = $original_due_date + TENDAYS;
}
if ($this->fund_day == NULL} {
throw new NullHandleException("fund_day is null");
}
}
###
# Calculate_Due_Date
# we then set 1st due date to fund_day + pay_span and make it current
# we now fine tune the pay date for weekends and holidays
# returning true or false if this waas accomplished
# We catch all thrown error and print them to the page.
###
public function Calculate_Due_Date() {
try
{
// set 1st due date to fund_day + pay_span and make it current
setDueDate();
tunePayDay();
if ($this->pay_day >= $this->due_date) {
return 1;
} else {
$this->pay_day = $this->pay_day + $this->pay_span;
Calculate_Due_Date();
}
} catch (NullHandleException $exception) {
print $exception->getMessage();
print " in file " . $exception->getFile();
print " on line " . $exception->getLine() . "\n";
} catch (Exception $exception) {
print $exception->getMessage();
print " in file " . $exception->getFile();
print " on line " . $exception->getLine() . "\n";
}
}
###
# tunePayDay
# if the client does not desire direct deposit, we add 10 days to the date
# we then call Weekend() to determine if its a saturday
# we again call Weekend() to determine if its a sunday
# finally we check if it's a Holiday
###
public function tunePayDay() {
// check if direct_deposit is no, Loop forward
if (!$this->direct_deposit)
LoopType("Forward");
// check if Saturday if yes, Loop forward
if (date('D',$this->pay_day) == "Sat")
LoopType("Forward");
// check if Sunday if yes, Loop forward
if (date('D',$this->pay_day) == "Sun")
LoopType("Forward");
// check if it's a Monday and a Holiday, then move it forward else move it backward
if(date('D',$this->pay_day) == "Mon") {
for ($i = 0; $i < count($this->holiday_array); $i++) {
// add one more day if holiday occurs on a Monday
if ((date("D",$this->pay_day) == "Mon") && ($this->pay_day == $this->holiday_array[$i])) {
LoopType("Forward");
} else {
LoopType("Reverse");
}
}
}
if($this->direct_deposit == NULL) {
throw new NullHandleException("direct_deposit is null");
}
if($this->holiday_array == NULL) {
throw new NullHandleException("holiday_array is null");
}
}
###
# LoopType
# adds a day or subtracts a day to the payday date depending on
# whether we recieve "Forward" or "Reverse" as its argument
###
private function LoopType($type) {
switch ($type) {
case "Forward":
$this->pay_day = $this->pay_day + DAY;
break;
case "Reverse":
$this->pay_day = $this->pay_day - DAY;
break;
}
if($type == NULL) {
throw new NullHandleException("type is NULL");
}
}
###
# Get_due_date
# I added this to try and test whether the due date is being set
###
public function Get_due_date() {
return $this->due_date;
}
###
# Get_pay_day
# I added this to try and test whether the pay_day is being set
###
public function Get_pay_day() {
return $this->due_date;
}
define("DAY", (1*24*60*60));
define("TENDAYS", (10*24*60*60));
public $fund_day, $holiday_array, $due_date;
public $pay_span, $pay_day, $direct_deposit;
}
$fund_day = strtotime("12/15/2005");
// setup holiday array
$holiday_array[0] = strtotime("1/1/2006");
$holiday_array[1] = strtotime("1/16/2006");
$holiday_array[2] = strtotime("2/20/2006");
$holiday_array[3] = strtotime("5/29/2006");
$holiday_array[4] = strtotime("7/4/2006");
$holiday_array[5] = strtotime("9/4/2006");
$holiday_array[6] = strtotime("10/9/2006");
$holiday_array[7] = strtotime("11/11/2006");
$holiday_array[8] = strtotime("11/23/2006");
$holiday_array[9] = strtotime("12/25/2006");
//setup pay_span
$pay_span = (7*24*60*60); //weekly (7*24*60*60),bi-weekly (14*24*60*60),monthly(30*24*60*60)
// setup $pay_day
$now = time();
// calculaate latest pay_day = fund_day + (#pay_span intervals between fund_day and present * pay_span)
$pay_day = $fund_day + strtotime(((int)$now/$pay_span) * $this->pay_span);
//setup direct deposit false = 0
$direct_deposit = 0;
$obj = new Paydate_Calculator($fund_day, $holiday_array, $pay_span, $pay_day, $direct_deposit);
$obj->Calculate_Due_Date();
print "due_date is: " . $obj->Get_due_date();
print "pay_day is: " . $obj->Get_pay_day();
?>
and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]