title: Developing a subscription plan for a website
body: <p>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:</p>
  
  <ul>
  <li>Basic: $0.00</li>
  <li>Silver: $20.00</li>
  <li>Gold: $30.00</li>
  <li>Platimum: $50.00</li>
  </ul>
  
  <p>The pre-payment options are:</p>
  
  <ul>
  <li>1 month</li>
  <li>3 months</li>
  <li>6 months</li>
  <li>12 months.</li>
  </ul>
  
  <p>These are my tables;</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>if ($plan &amp; $period) {
      $user = new fncs();
      // What plan was chosen?
      list($name, $cost) = $db-&gt;get_row("select name, cost from plans where id='$plan'", ARRAY_N);
      list($sub_period) = $db-&gt;get_row("select sub_period from plans_period where sub_period='$period'", ARRAY_N);
      list($plan, $user_plan_period, $plan_cost, $plan_expiration) = $db-&gt;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 &gt; $user-&gt;getBalance())
      {
          $errs[] = 'You have insufficient balance for the upgrade. The total cost is: '.$Daysleft.'';
      }
      else {
          $r = $db-&gt;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-&gt;getBalance() - $cost;
          $db-&gt;query("update users set balance='$balance' where user_id='{$_SESSION['user_id']}'");
          // Reset sessions
          $user-&gt;processLogin($_SESSION['email']);
          // Insert into transactions
          $txt = "Account upgrade: &lt;strong&gt;$name&lt;/strong&gt; for &lt;s&gt;N&lt;/s&gt;$cost. Expires ".$_SESSION['plan_expiration'];
          $db-&gt;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;
      }
  }
  </code></pre>
  
  <p>Below is the form where the user will select the plan to upgrade to</p>
  
  <pre><code>&lt;div class="mainbar"&gt;
      &lt;h3&gt;Upgrade Account&lt;/h3&gt;
      &lt;?php
      if (isset($errs)) {
          echo '&lt;p class="alert"&gt;';
          foreach($errs as $v){
              echo "$v ";
          }
          echo '&lt;/p&gt;';
      }
      ?&gt;
      &lt;form class="form" method="post"&gt;
          &lt;div class="rc"&gt;
              &lt;p&gt;&lt;label&gt;Current Balance&lt;/label&gt; &lt;b&gt;&lt;s&gt;N&lt;/s&gt;&lt;?= number_format($_SESSION['balance'], 2); ?&gt;&lt;/b&gt;&lt;/p&gt;
              &lt;p&gt;&lt;label&gt;Plan&lt;/label&gt;
                  &lt;select class="txt iwdt" name="plan"&gt;
                  &lt;?php
                  $r = $db-&gt;get_results("select id, name, cost from plans where id &gt; 1");
                  foreach ($r as $v) {
                      list ($id, $name, $cost) = $v;
                      echo '&lt;option value="'.$id.'"';
                      echo $id == $_SESSION['plan'] ? ' selected="selected"' : '';
                      echo '&gt;'.$name.' for N'.$cost.'&lt;/option&gt;';
                  }
                  ?&gt;
                  &lt;/select&gt;
                  &lt;select class="txt iwdt" name="period"&gt;
                      &lt;option value=""&gt;Select Period&lt;/option&gt;
                      &lt;?php
                      $r = $db-&gt;get_results("select id, sub_period from plans_period");
                      foreach ($r as $v) {
                          list ($id, $sub_period) = $v;
                          echo '&lt;option value="'.$id.'"';
                          echo $id == $_SESSION['period'] ? ' selected="selected"' : '';
                          echo '&gt;'.$sub_period.' month(s)&lt;/option&gt;';
                      }
                      ?&gt;
                  &lt;/select&gt;
              &lt;/p&gt;
              &lt;p&gt;
                  &lt;label&gt;&amp;nbsp;&lt;/label&gt; &lt;input type="submit" name="submit" class="button" value="Upgrade" /&gt; or &lt;a href="my-account.php"&gt;Cancel&lt;/a&gt;
              &lt;/p&gt;
          &lt;/div&gt;
      &lt;/form&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Can somebody point me to the right direction. Is there a tutorial i can learn from on this? Thanks.</p>
  
  <p>Thanks to all that have replied.</p>
  
  <p>Basically this is where i think i'm having some challenge</p>
  
  <pre><code>
  $plan_expiration = ($plan_expiration = "NULL") ? '30' : '$plan_expiration';
  </code></pre> 
  
  <p>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". </p>
  
  <p>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</p>
  
  <p>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 </p>
  
  <pre><code> 
   $today = date("m-d-Y"); 
      $dateDiff = strtotime($plan_expiration) - strtotime($today);
      $Daysleft = floor($dateDiff/(60*60*24));
  </code></pre> 
  
  <p>First error is that $Daysleft is returning me a negative value</p>