tags:

views:

83

answers:

4

scenario:

there are 4 items:

30day pass
60day pass
180day pass
365day pass

there is a monthly (30 day) credit cap. so if you use the credit up before the end of the month you need to purchase another 30day pass, or wait till the next renewal period.

A person purchases a 30day pass today, purchase date is recorded to DB. Expiry date is also recorded.

if ($todaysdate >= $expirydate) //DONE. 

but whAt about for 60day and 180day passes ?

we have purchasedate & expiry date. every 30days, the credit needs to be reset for the next month.

I am really lost how to best approach this problem.

A: 

You could run a task (or cron job) daily to check and see if the current date is later than the recorded expiration date, and if it is reset the users credit to 0, and send them an email letting them know their credit has expired.

As the users buys credit, you merely add 30, 60 or 90 days to their expiration date.

For more information, see php + cronjob tags.

Jonathan Sampson
what i ended up doing was just calculating 30 day. run cron everyday to see if that user has past 30 day or not. if so, reset their credit. this required also updating the tables with most recent date, so 30 day is always calculated.
ggggggggg
A: 

in your situation, you could have a table like this:

pass: (id, user_id, start_time, end_time, credit)

every day you can flush the unused credit on any pass that has expired:

update user join
  (select user_id, sum(credit) as credit from pass
    group by user_id where end_time <= now() and not flushed) b
  set user.credit = max(user.credit - b.credit, 0)
  where user.user_id = b.user_id

update pass set flushed = 1 where end_time <= now() and not flushed

by using end_time <= now() you ensure the credit is only reset once the expiration date has passed. since you remember which passes were flushed, you can run this as often as you like and missing a day is not an issue (it will be detected on the next run).

this is effectively how a prepaid calling card would work - you have a certain number of minutes that are valid for X (30/60/90 whatever) days and the minutes are reset at the end.

btw, you can simplify all that stuff by using an easy to use subscription management system like recurly or chargify.

jspcal
+1  A: 

one way to do that is with cron and mysql

when you run this cron every day

define a variable

define (DAY_AMOUNT ,30);

check the day pass by query

SELECT mem_id  DATEDIFF(CURDATE(),mem_date) AS daypass FROM table
 WHERE ((DATEDIFF(CURDATE(),mem_date))=".DAY_AMOUNT."

the results that return is all the members that pass 30 days from the date.

Haim Evgi
A: 

Or, in the database, you can save each person has their own expiry date.

To make an expiry date, you should use a unix timestamp. 30 days is 2592000 so to find the expiry date, you could use something like the following if you want to calculate the expiry date (Changing months with how many months).

$months = 2; $expiry_date = time() + 2529000*$months;

Then, put those values into a database.

Then, you can do something that does an automated task (for example, cron).

When you run that automated task, you check if the current timestamp (another word for time) is bigger then the expiry date. If so, then change the value in the database in the field for if they have the right credits.

joshli