domi771
domi771

Reputation: 450

php/mysql: 30 day interval for pre-paid membership

I have a membership where each user gets 100 credits per 30 days interval. All memberships are prepaid for one to unlimited months upfront.

I want to reset the credits balance each 30 days back to 100 no matter if the user used his credits or not. I do not want to use any cronjob or something similar. I want to simply check when the user logs into the website if he already logged in the current 30 days interval and if not reset his credits balance.

I have in my DB a timestamp for the membership expiry date and when he logged in the last time.

Can anybody give me the formula how to calculate this?

I have so far:

$days = ($paid_until - $today) / 60 / 60 / 24;
$months = $days / 30;

echo "<pre>
Expires/ Renews in Days: $days
Expires/ Renews in Months: $months

Upvotes: 2

Views: 364

Answers (1)

halfer
halfer

Reputation: 20430

I am thinking along these lines. Pseudo-code, which you can easily convert to PHP (not tested):

start transaction
if (last_reset_date not null)
{
    days_since_reset = last_reset_date
}
else
{
    // Handle the case where no reset has yet been performed
    days_since_reset = now - sign_up_date
    update last_reset_date to sign_up_date
}

months = floor(days_since_reset / 30)
if (months >= 1)
{
    // Add in a reset for every missing month
    for(month = 1 to months)
    {
        update last_reset_date += 30
        store balance in log, month number reset
        reset user balance to 100
    }
}
end transaction

The idea here is to reset all the missing months, even if this is not run for a particular user for over a month -- hence the loop.

Upvotes: 2

Related Questions