Code: Select all
,Code: Select all
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]
Hi,
I'm coding a GPL time clock, but I'm fuzzing on how to (reliably) calculate the first date of the pay period. Below is my messy attempt including a simple unit test.Code: Select all
<?php
//
// CONFIGURATION
//
// The first date of of any pay period in a format understandable to strtotime().
// See http://us.php.net/manual/en/function.strtotime.php for details about format.
$pay_period_begins = "4 April 2007";
// The length of the pay period in days.
$pay_period_days = 14;
//
// CODE
//
/**
* Calculate the starting date of the current pay period.
*
* @return: unix time of first date of current pay period
*/
function timeclock_get_current_pay_period($now = FALSE)
{
//fixme: ugly code
global $pay_period_begins;
global $pay_period_days;
date_default_timezone_set("GMT");
if (FALSE == $now)
$now = time();
$ppb_unix = strtotime($pay_period_begins);
assert(is_int($now));
assert(is_int($pay_period_days));
assert($pay_period_days > 1);
assert(is_string($pay_period_begins));
assert(is_int($ppb_unix));
$now_unix = strtotime(gmdate("Y-M-d", $now)); // ignore time of day
$diff_days = ($now_unix - $ppb_unix)/(24*60*60);
if ($diff_days >= 0)
{
// pay_period_begins is in the past
return (floor($diff_days/ $pay_period_days)*24*60*60*$pay_period_days) + $ppb_unix;
}
else
{
// pay_period_begins is in the future
//fixme!!
echo "past\n";
$x = (floor(abs($diff_days)/$pay_period_days));
$pay_period_begins = gmdate("Y-M-d", $ppb_unix - (($x++*24*60*60) * $pay_period_days));
echo "new ppb = ". gmdate("D Y-M-d", strtotime($pay_period_begins)). "\n";
return timeclock_get_current_pay_period($now);
$x2 = ($x+1) * 60 * 24 * 60 * $pay_period_days;
$x3 = $ppb_unix - $x2;
return $x3;
}
}
//
// UNIT TESTS
//
function test_same_dates($x, $y)
{
return date("Y-M-d", $x) == date("Y-M-d", $y);
}
function test_timeclock_get_current_pay_period_helper($in, $out)
{
assert(is_int($in));
assert(is_int($out));
$r = timeclock_get_current_pay_period($in);
$in_s = date("D Y-M-d", $in);
$out_s = date("D Y-M-d", $out);
$r_s = date("D Y-M-d", $r);
$msg = "Today's date $in_s, expecting $out_s, actual $r_s\n";
if (!test_same_dates($out, $r))
die("FAIL: $msg");
else
print("PASS: $msg");
}
function test_timeclock_get_current_pay_period()
{
global $pay_period_begins;
global $pay_period_days;
$pay_period_days = 14;
$pay_period_begins = "4 April 2007";
test_timeclock_get_current_pay_period_helper(strtotime('2007-03-20'), strtotime('2007-03-07'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-03-21'), strtotime('2007-03-21'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-03-22'), strtotime('2007-03-21'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-03'), strtotime('2007-03-21'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-04'), strtotime('2007-04-04'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-05'), strtotime('2007-04-04'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-09'), strtotime('2007-04-04'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-17'), strtotime('2007-04-04'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-18'), strtotime('2007-04-18'));
test_timeclock_get_current_pay_period_helper(strtotime('2007-04-19'), strtotime('2007-04-18'));
}
test_timeclock_get_current_pay_period();
echo "ALL PASS!\n";
?>Everah | Please use
Code: Select all
,Code: Select all
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]