Search code examples
phpsystemmembership

PHP timestamp/membership system not expiring on the exact date


I'm having a few problems while creating a small membership system with timestamps. When I show the timestamp in a date, it will tell me when the active membership expires. However, on that time/date, it will NOT expire, and the script that resets membership, and counts from -NUMBERHERE.

I don't think I'm doing anything wrong? I'm doing (time() - $timestamp) to work it out. Here's my script(s);

$membership = mysql_query("SELECT * FROM `members` WHERE `membership` > 0 AND `membertype` > 0") or die(mysql_error());
while($mm = mysql_fetch_array($membership)){

  if((time() - $mm["membership"]) > $mm["membertype"]){
     mysql_query("UPDATE `members` SET `membership` = 0, `membertype` = 0 WHERE `username` = '" . $mm[username] . "'");
  } else {
     echo (time() - $mm["membership"]);
  }

}  

$membership_date  = date("d-m-Y H:i:s", $user["membership"]);
echo "Your membership expiry date: $membership_date";

It does expire, but it expires very late. Any help will be great, thanks.

P/S: the membertype variable is the amount of seconds until it expires.


Solution

  • When creating a user:

    $membership = time() + $membertype;
    

    Then set up your query and execute.

    When retrieving a user:

    Replace this line

    if((time() - $mm["membership"]) > $mm["membertype"]){
    

    with this:

    if( time() >= $user["membership"] )