Рассчитайте время, оставшееся до подписки

0

У меня есть подписка, на которую можно подписаться каждый месяц, каждые два месяца или каждые 3 месяца. Это представлено как subscription_frequency 1, 2 или 3.

Затем у меня есть свойство month_joined (с 1 по 12).

Но я пытаюсь работать, когда кто-то должен получать их подписку. До сих пор я знаю, что хочу работать, когда пользователь будет получать подписку.

Таким образом, я добавляю 12 к текущему месяцу, затем month_joined число 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
}
]

Что с моей математикой?

Теги:
math

2 ответа

1

ИМХО, вы можете проверить, является ли текущий месяц месяцем, по которому вы должны отправлять подписку (это не проверяет, была ли вы отправлена подписка на текущий месяц, это должна быть отдельная функция)

Если вы доставляете в месяц подписки

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

Если вы отправляете следующий месяц после подписки

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

Я думаю, вы должны хранить массив с месяцами, которые вы поставили, а месяц подписки должен быть датой, а впоследствии вы можете найти даты подписки, добавив частотные месяцы к дате подписки.

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

Ещё вопросы

Сообщество Overcoder
Наверх
Меню