计算订阅前的剩余时间

时间:2015-03-05 17:27:48

标签: php math

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

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

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

因此,我在当前月份中添加了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
}
]

我的数学问题是什么?

2 个答案:

答案 0 :(得分:1)

恕我直言,您可以检查当前月份是否应该发送订阅的月份(这不会检查您是否发送了当月的订阅,这应该是一个单独的功能)

如果您在订阅月份交付

enter code here
$shouldSubscriptionBeSent = ($currentMonth - $monthJoined) % $subscriptionFrequency === 0

如果您在订阅后的下个月交付

$shouldSubscriptionBeSent = ($currentMonth - $monthJoined - 1) % $subscriptionFrequency === 0

我认为您应该保留一个数组,其中包含您交付的月份,订阅月实际上应该是一个日期,之后您可以通过将频率月份添加到订阅日期来查找订阅日期。

答案 1 :(得分:0)

这几乎没有效率,但应该会给你下一个订阅月

  public function nextsub()
{
    $arrayofpossiblesubscriptionmonths=array();

    for($i=1;$i<=12;$i++){
        $submonth=$month_joined+($i*$this->subscription_frequency);
        if($submonth>12){
            $submonth=$submonth-12;
        }
        //this will create some duplicate months
        $arrayofpossiblesubscriptionmonths[]=$submonth;
    }
    //cleanup duplicates
    $uniquesubmonths=array_unique($arrayofpossiblesubscriptionmonths);

    //fix the order
    sort($uniquesubmonths);

    //since they are in order.  find the first one bigger than our current month
    foreach($uniquesubmonths as $onesub){
        if($onesub>$current_month){
            return $onesub;
        }
    }
}

这是一个CodeViper ,其功能稍有改动,因此我可以更轻松地传递变量 http://codepad.viper-7.com/9Er2pi

      function nextsub($current_month,$month_joined,$subscription_frequency)
{



    $arrayofpossiblesubscriptionmonths=array();

    for($i=1;$i<=12;$i++){
        $submonth=$month_joined+($i*$subscription_frequency);
        if($submonth>12){
            $submonth=$submonth-12;
        }
        //this will create some duplicate months
        $arrayofpossiblesubscriptionmonths[]=$submonth;
    }
    //cleanup duplicates
    $uniquesubmonths=array_unique($arrayofpossiblesubscriptionmonths);

    //fix the order
    sort($uniquesubmonths);

    //since they are in order.  find the first one bigger than our current month
    foreach($uniquesubmonths as $onesub){
        if($onesub>$current_month){
            return $onesub;
        }
    }
}
$curmonth=3;
$monthjoined=1;
$sub_freq=1;
echo '<BR>Current month: '.$curmonth;
echo '<BR>Month Joined: '.$monthjoined;
echo '<BR>Subscription Frequency: '.$sub_freq;
echo '<BR>Next Subscription Month: '.nextsub($curmonth,$monthjoined,$sub_freq);

echo'<HR>';
$curmonth=3;
$monthjoined=11;
$sub_freq=3;
echo '<BR>Current month: '.$curmonth;
echo '<BR>Month Joined: '.$monthjoined;
echo '<BR>Subscription Frequency: '.$sub_freq;
echo '<BR>Next Subscription Month: '.nextsub($curmonth,$monthjoined,$sub_freq);

echo'<HR>';
$curmonth=3;
$monthjoined=8;
$sub_freq=2;
echo '<BR>Current month: '.$curmonth;
echo '<BR>Month Joined: '.$monthjoined;
echo '<BR>Subscription Frequency: '.$sub_freq;
echo '<BR>Next Subscription Month: '.nextsub($curmonth,$monthjoined,$sub_freq);

输出

Current month: 3
Month Joined: 1
Subscription Frequency: 1
Next Subscription Month: 4

Current month: 3
Month Joined: 11
Subscription Frequency: 3
Next Subscription Month: 5

Current month: 3
Month Joined: 8
Subscription Frequency: 2
Next Subscription Month: 4