Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5983259
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:12:17+00:00 2026-05-22T22:12:17+00:00

I have been working on a website that has to do with a subscription

  • 0

I have been working on a website that has to do with a subscription system where users will have to upgrade from one level to another. Let me explain a bit. There are four plans namely; Basic, Silver, Gold, Platimum. The cost is as follows:

  • Basic: $0.00
  • Silver: $20.00
  • Gold: $30.00
  • Platimum: $50.00

The pre-payment options are:

  • 1 month
  • 3 months
  • 6 months
  • 12 months.

These are my tables;

plans table:
id    name      cost
1     Basic      0.00    
2     Silver    20.00
3     Gold      30.00
4     Platinum  50.00

plans_period table:
id    sub_period
1     1
2     3
3     6
4     12

users table:
user_id  plan  plan_cost      user_plan_period  balance         plan_expiration
1        2     0.00(default)  0 (default)       0.00 (default)  NULL (default)

Now for a user to upgrade the system will check the balance table to see how must the user has left, the cost of the new plan. If the balance is more than the cost of the new plan the user is upgrading to, the upgrade will be successful and redirected to a success page, if not the payment button will be displayed for the user to pay whatever the new cost using any of the payment system integrated. Also if the user is in the middle of a plan and wants to upgrage to a higher plan, the system will also have to pro-rate the new cost and charge the user likewise. I have been trying to do this with the code below.

if ($plan & $period) {
    $user = new fncs();
    // What plan was chosen?
    list($name, $cost) = $db->get_row("select name, cost from plans where id='$plan'", ARRAY_N);
    list($sub_period) = $db->get_row("select sub_period from plans_period where sub_period='$period'", ARRAY_N);
    list($plan, $user_plan_period, $plan_cost, $plan_expiration) = $db->get_row("select u.plan, u.user_plan_period, u.plan_cost, u.plan_expiration, p.id from users u LEFT JOIN plans p ON p.id=u.plan where u.user_id='{$_SESSION['user_id']}'", ARRAY_N);
    $plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';
    /*Current Cost of plan chosen */
    $totalcost1 = $cost * $sub_period;
    /* Cost of the user current plan */
    $totalcost2 = $plan_cost * $user_plan_period;
    /* Total number of days of the user current plan */
    $totaldays1 = 30 * $user_plan_period;
    /* Total number of days of the left for the user current plan */
    $today = date("m-d-Y"); 
    $dateDiff = strtotime($plan_expiration) - strtotime($today);
    $Daysleft = floor($dateDiff/(60*60*24));

    $totaldays2 = $totaldays1 - $Daysleft;

    $expiredate = strtotime($plan_expiration);

    $totalcost =  $totalcost1 -  ($totalcost2 * ($Daysleft / $totaldays1));

    $finalcost = number_format($totalcost, 2);

    // sufficient balance?
    if ($totalcost > $user->getBalance())
    {
        $errs[] = 'You have insufficient balance for the upgrade. The total cost is: '.$Daysleft.'';
    }
    else {
        $r = $db->query("update users set plan='$plan', user_plan_period='$period', plan_cost='$cost', plan_expiration=date_add(now(), interval 30 day) where user_id='{$_SESSION['user_id']}'");
        // Update balance
        $balance = $user->getBalance() - $cost;
        $db->query("update users set balance='$balance' where user_id='{$_SESSION['user_id']}'");
        // Reset sessions
        $user->processLogin($_SESSION['email']);
        // Insert into transactions
        $txt = "Account upgrade: <strong>$name</strong> for <s>N</s>$cost. Expires ".$_SESSION['plan_expiration'];
        $db->query("insert into transactions (user_id, txt, direction, amount, balance, date) values ('{$_SESSION['user_id']}', '$txt', 'd', '$cost', '$balance', now())");
        $_SESSION['msg'] = 'Account successfully upgraded. Plan expires on '.$_SESSION['plan_expiration'];

        header('location:my-account.php');
        exit;
    }
}

Below is the form where the user will select the plan to upgrade to

<div class="mainbar">
    <h3>Upgrade Account</h3>
    <?php
    if (isset($errs)) {
        echo '<p class="alert">';
        foreach($errs as $v){
            echo "$v ";
        }
        echo '</p>';
    }
    ?>
    <form class="form" method="post">
        <div class="rc">
            <p><label>Current Balance</label> <b><s>N</s><?= number_format($_SESSION['balance'], 2); ?></b></p>
            <p><label>Plan</label>
                <select class="txt iwdt" name="plan">
                <?php
                $r = $db->get_results("select id, name, cost from plans where id > 1");
                foreach ($r as $v) {
                    list ($id, $name, $cost) = $v;
                    echo '<option value="'.$id.'"';
                    echo $id == $_SESSION['plan'] ? ' selected="selected"' : '';
                    echo '>'.$name.' for N'.$cost.'</option>';
                }
                ?>
                </select>
                <select class="txt iwdt" name="period">
                    <option value="">Select Period</option>
                    <?php
                    $r = $db->get_results("select id, sub_period from plans_period");
                    foreach ($r as $v) {
                        list ($id, $sub_period) = $v;
                        echo '<option value="'.$id.'"';
                        echo $id == $_SESSION['period'] ? ' selected="selected"' : '';
                        echo '>'.$sub_period.' month(s)</option>';
                    }
                    ?>
                </select>
            </p>
            <p>
                <label>&nbsp;</label> <input type="submit" name="submit" class="button" value="Upgrade" /> or <a href="my-account.php">Cancel</a>
            </p>
        </div>
    </form>
</div>

Can somebody point me to the right direction. Is there a tutorial i can learn from on this? Thanks.

Thanks to all that have replied.

Basically this is where i think i’m having some challenge


$plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';

This plan_expiration field is date field in this format Year-month-day such as 2011-06-03. Now the default value for this is “NULL”. When a user first signs up into our site, the user is assign a basic membership plan which is $0.00 cost with their expiration date set to “NULL”.

If a user wants to upgrade, we have to calculate the days left before their plan will expire because we are going to pro-rate this based on the days left. So i did this

i assigned “30” to $plan_expiration if it is nulled based on the code above. I then calculated the days left using the syntax below

 
 $today = date("m-d-Y"); 
    $dateDiff = strtotime($plan_expiration) - strtotime($today);
    $Daysleft = floor($dateDiff/(60*60*24));

First error is that $Daysleft is returning me a negative value

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:12:18+00:00Added an answer on May 22, 2026 at 10:12 pm

    You also need the actual sign-up-date to calculate the missing days.

    Lets use this example:

    We have a sign-up on the 15th of May (2011-05-15) and an expiration of assumed 30 days (which results in an expiration on 15th of June (2011-06-15).

    The PHP calculation could work like this:

    $signupDate = strtotime('2011-05-15');
    $expiration = 30; //this is in days, lets remember that for later
    $expirationDate = $signupDate+($expiration*24*60*60); //calculate the expiration date
    $today = time(); //theres no need for using date and strtotime of you just want the timestamp
    
    $daysLeft = floor(($expirationDate-$today)/(60*60*24));
    

    If $daysLeft now has a negative value (which shouldn’t happen) his account has expired some time ago. But since the way your application is working this should normally result in a positive value giving you the days left for the users plan.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working with a string[] array in C# that gets returned from
I am going to start working on a website that has already been built
I have been working with an existing website out company has running until I
I have been working on some legacy C++ code that uses variable length structures
I have been working on a wrapper for a COM object that can only
I converted a forms website into an application and everything has been working just
I have been working on a website using MVC Preview 1 since it came
I've got an issue involving a website that I have been using VS 2008
i'm working on some website that has a section with pure AJAX, but i
update Here is the situation: I'm working on a website that has no physical

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.