2012-03-13 68 views
1

我想要一个php函数,它会以设定的时间间隔从一个初始整数500来减去一个值,例如50。以设定的时间间隔减去数值?

例如:每30天,扣除50从500所以后90天,如果只剩下350等

我可以做减法,这是我想不通的间隔程序。

如果不是PHP,JS也很棒。这是用来从一年的开始倒数到最后的值。

+0

这里您的实际目标是什么?您可能不需要每30天减去一次值,只需要计算它在您需要时应该具有的值,同时考虑自(开始日期)以来已过去多少天。 – deceze 2012-03-13 00:39:37

+0

你能解释一下吗?你想要一个函数,在运行时...每30(真实生活)天减去50个数。如果没有,你可以进一步指定你的功能吗?例如它需要一个整数(500),一个扣除(50)的值和间隔(30天 - 一个日期时间?整数)和一个天数(90 - 一个日期时间?整数),并且您希望它回报350? – 2012-03-13 00:39:45

+0

你不能用PHP做这件事,因为它是一次性脚本。你需要类似Cron的任务。 – 2012-03-13 00:40:02

回答

1

你有一个起始号码,你试图找出你的当前值是什么。它的基本总和 - (时间传递*递减)问题。

所以举个例子,你有你的原创时间。

$originalDate = '2011-01-01'; 
$now = '2011-03-01'; 

//it will count no. of days 
$dateDiff=(strtotime($now) - strtotime($originalDate))/ (60 * 60 * 24); 

$startingValue = 500; 
$descrement = 50; 

$currentValue = $startingValue - ($dateDiff/30*$descrement); 
+0

我测试了这两个函数,并略微修改了这个函数。 '$ originalDate ='2012-01-01'; $ dateDiff =(strtotime(“now”) - strtotime($ originalDate))/(60 * 60 * 24); \t $ startingValue = 588; \t $ descrement = 49; \t $ currentValue = $ startingValue - ($ dateDiff/30 * $ descrement); \t \t \t echo $ currentValue; \t echo“
”; \t echo round($ currentValue,-1);' 现在,它给了我一个确切的价值,使用470,这在本月中旬是有意义的。理想情况下,这个数字是固定的,所以在3月份,这个数字是490(588-49-49)。 – stebesplace 2012-03-13 01:49:36

+0

该解决方案适合我的需求。我修改了它,但功能在那里。 – stebesplace 2012-03-13 04:43:39

0

,如果你想在这一个功能:

/* deduct() 
* 
* @param $start  - the starting amount to subtract from 
* @param $amount  - the amount to subtract by 
* @param $interval  - the interval between deductions 
* @param $current_time - the current time to measure against the interval 
* 
* @return the amount after deduction $amount 
*   every $interval from $start until $current_time 
*/ 
function deduct($start, $amount, $interval, $current_time) { 
    return $start - ($amount * floor($current_time/$interval)); 
} 

您可以轻松地通过使用time(),或类似的东西,格式化成你的时间单位的时间值。

+0

立即检查此... – stebesplace 2012-03-13 00:52:15

+0

设置此项有一些困难,从某种意义上说,尽管我可以输入固定值,但我不确定计算是否适用于我。这可能是由于我正在使用的时间输出,或者我应该将其保留为默认格式。我其实没有获得回报价值。 – stebesplace 2012-03-13 01:06:53

+0

确保日期格式正确。使用'date('d',time())'格式化它几天。 – Jon 2012-03-13 01:15:50