creepy
creepy

Reputation: 11

Check if the subscription has expired or not

I'm working on a webapp where I have been assigned a task of user subscription and to check if the subscription has expired or not. I'm doing this is PHP. I just need a basic idea of how to do this. Please help

Upvotes: 0

Views: 417

Answers (1)

node_man
node_man

Reputation: 1449

Here's a simple approach. I'll share a code snippet I worked on. You can try it this way.Set a valid_upto field in your database. So when the user buys the subscription, store the value of the valid upto date into this field. Here's an example code snippet.

public function vendorcheckout($data)
    {
        $stmt = $this->con->prepare("INSERT INTO x3mdu_vendor_subscription(sub_id,vendor_id,sub_title,sub_amount,sub_duration,vendor_name,vendor_email,vendor_phone,vendor_address1,vendor_address2,vendor_city,vendor_state,payment_id,valid_upto,created) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,now())");
        $stmt->bind_param("iisisssissiiss", $data['sub_id'],$data['vendor_id'],$data['sub_title'],$data['sub_amount'],$data['sub_duration'],$data['vendor_name'],$data['vendor_email'],$data['vendor_phone'],$data['vendor_address1'],$data['vendor_address2'],$data['vendor_city'],$data['vendor_state'],$data['payment_id'],$data['valid_upto']);
        $result = $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        $last_id = $this->con->insert_id;
        if($result){
          return $last_id;
        }
        return false;
    }

To check if the subscription is still active you can do it this way.

private function isSubscribed($vendor_id) {
        $stmt = $this->con->prepare("SELECT vs_id from x3mdu_vendor_subscription WHERE vendor_id = ? AND DATEDIFF(CURDATE(),valid_upto) < 0");
        $stmt->bind_param("i",$vendor_id);
        $stmt->execute();
        $stmt->store_result();
        $num_rows = $stmt->num_rows;
        $stmt->close();
        return $num_rows > 0;
    }

The DATEDIFF(CURDATE(),valid_upto) will return a negative value until the valid_upto date comes. When the difference is greater than 0 that means your subscription has expired. Hope it helps

Upvotes: 1

Related Questions