计算订阅前的剩余时间

时间:2015-03-05 16:09:15

标签: php math

我订阅的订阅可以每月,每两个月或每3个月订阅一次。这表示为subscription_frequency的1,2或3。

然后我有一个month_joined属性(1到12)。

但是,当有人应该收到他们的订阅时,我正试图解决问题。到目前为止,我知道我想在用户下次收到订阅时解决。

[免责声明:在php中我在模数和余数之间混淆了]

因此,我在当前月份中添加了12,然后减去month_joined个数字,然后查找其subscription_frequency的剩余部分,添加到当前月份,告诉他们什么时候他们的下一个盒子应该到期了。

public function nextsub()
{
    $remainder = ($current_month + 12 - $this->month_joined) % $this->subscription_frequency;
    return $current_month + remainder;
}

但是我的所有数字都是疯狂的。这是用户转储:

[
{
current_month: 3,
month_joined: 1,
subscription_frequency: 1,
next_box: 3
},
{
current_month: 3,
month_joined: 3,
subscription_frequency: 3,
next_box: 3
},
{
current_month: 3,
month_joined: 11,
subscription_frequency: 3,
next_box: 4<----------------- this is incorrect
},
{
current_month: 3,
month_joined: 12,
subscription_frequency: 3,
next_box: 3<----------------- this is incorrect
},
{
current_month: 3,
month_joined: 8,
subscription_frequency: 2,
next_box: 4
},
{
current_month: 3,
month_joined: 1,
subscription_frequency: 1,
next_box: 3
},
{
current_month: 3,
month_joined: 3,
subscription_frequency: 2,
next_box: 3
},
{
current_month: 3,
month_joined: 2,
subscription_frequency: 3,
next_box: 4 <----------------- this is incorrect
},
{
current_month: 3,
month_joined: 2,
subscription_frequency: 1,
next_box: 3
}
]

我的数学问题是什么?

1 个答案:

答案 0 :(得分:1)

我想你想要这个...

$next_box = ($current_month + ($month_joined % $subscription_frequency + $current_month % subscription_frequency)) % 12;

所以,如果3月你的频率为3,你在11月加入

(3 + (11 % 3 + 3 % 3)) % 12 = 5 

下一个盒子来自五月