SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Date calculations hurt my brain

    I am developing an application that involves users signing up for a monthly subscription. They can terminate this subscription at any time - if they do so, their subscription will expire the next time the monthly payment is due.

    So in my DB I have a subscription start date of, for example, 2009-06-24. On August 25 (2009-08-25) the subscriber decides they want out, and terminates. Now, I want their subscription to expire, in this case, on 2009-09-24, so they get all the full months they paid for. I want to calculate this expiry date as soon as they terminate, and store it in the DB.

    My question is - how do I work out the expiry date? I have the subscription start date (and therefore the date the monthly charge is made), and obviously I have today's date... but I can't figure out how I use these to calculate the expiry date.

    Any help most appreciated.

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to keep a running expiry date that gets updated whenever the user buys more time.

    But some common ways to add dates
    Code:
    mysql
    select foo + INTERVAL 1 month
    PHP Code:
    $new_timestamp strtotime('+ 1 month'$reference_timestamp); 
    You could also use mktime(), by using date('H', $reference_timestamp) to feed it each argument, and just add 1 to the month argument.

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    You might want to keep a running expiry date that gets updated whenever the user buys more time.
    Thanks for the response.

    Unfortunately the date on which the user buys more time is established by a third-party payment processor, so I can't keep a running expiry date.

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's not clear to me when user subscription suppose to end if user paid on July 24, then cancells on August 23?

    Will it expire on August 24?

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sharedlog.com View Post
    It's not clear to me when user subscription suppose to end if user paid on July 24, then cancells on August 23?

    Will it expire on August 24?
    Correct. Basically, even after canceling, the user will still get the time they have already paid for. So, if they cancel on the 23rd, they will still get their time until the 24th, completing the month they paid up-front for back in July.

    Question is, how do I calculate that the expiry date is August 24? I need to make this calculation when they cancel.

  6. #6
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you have mysql, then just use mysql's date/time functions

    SELECT DATE_ADD('subscription_date',INTERVAL 1 months);

    Notice the months has 's' at the end, even though it's only 1 month, you still have to add 's' or it would not work

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sharedlog.com View Post
    If you have mysql, then just use mysql's date/time functions

    SELECT DATE_ADD('subscription_date',INTERVAL 1 months);

    Notice the months has 's' at the end, even though it's only 1 month, you still have to add 's' or it would not work
    I headed down that route myself. However, the question is, how do I know how many months to INTERVAL?

  8. #8
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by uprightdog View Post
    I headed down that route myself. However, the question is, how do I know how many months to INTERVAL?
    How do you know whether or not a user is considered an active member? Surely you have something which tells you who paid, and when they paid, and how much they paid?

    For example, whats to stop me from signing up, paying my first month, and then never paying ever again(but still using your service)?

  9. #9
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    How do you know whether or not a user is considered an active member? Surely you have something which tells you who paid, and when they paid, and how much they paid?

    For example, whats to stop me from signing up, paying my first month, and then never paying ever again(but still using your service)?
    When a user logs in their subscription status is checked with the third-party payment processor.

    This is besides the point. Establishing whether or not a user is allowed to be there is not a problem. I am simply considering the necessary date calculation.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •