Keeping Subscription dates tidy during February and Leap years

Posted about 1 year ago by Philip Ingram

This will be a quick one.

If a client of yours signed up for your service on jan 30th, and you bill monthly, there are a couple ways you can handle the dreaded ‘end of February’ problem you could find yourself in.

I use two billing dates in my Model that handles the, well, billing dates for a shop.

The following code seems to be the most concise way I’ve found to handle both leap years and Feb. 28th.


billing_date = shop.shop_billing_date
#Is today the end of Feb? or a leap year?
      if Date.today == Date.new(Date.today.year, 2, 28) || Date.today.leap? && Date.today == Date.new(Date.today.year, 2, 29)
        #if it is update the next_billing_date by creating a new Date object
       #using a database column that has just the actual numerical Date of the month they should be charged on.
       billing_date.update_attributes(:last_billing_date => Date.today, :next_billing_date => Date.new(Date.today.year, 3, billing_date.bill_day))

      else
       #Else, just bill them and update the normal way that the next billing date is next month at this time. 
     billing_date.update_attributes(:last_billing_date => Date.today, :next_billing_date => Date.today >> 1)
      end

Summary

Again, i use a last_billing_date column and a next_billing_date. Just in case something happens, i also store a bill_day in the database which is the Day of the Created_at.

The first line just checks for today’s date and makes sure it is one of the two end of Feb dates. If it is, it will update the next billing date with the correct Day for March, by adding the bill_date to the Date.new object it is creating.

If it isn’t the end of Feb, then it just uses today’s date.

Note: I run this every day, so that is why I am using Date.today, although i may change it to bill date, just incase i change the formula to catch ‘past due’ shops…. Even in the middle of a blog post, you can always learn, eh!

Cheers.

Comments

bhpoxo
dVxtUPQE
urtcpi
zxhika
thanks a lot for the post! very interesting!
Amazing! This blog looks just like my old one! It’s on a entirely different subject but it has pretty much the same page layout and design. Outstanding choice of colors!
Hola! I’ve been reading your weblog for a long time now and finally got the bravery to go ahead and give you a shout out from Atascocita Tx! Just wanted to tell you keep up the fantastic job!
bnagpob
PuSQTO comment3
IvWYuy comment1
Hello! Read the pages not for the first day. Yes, the connection speed is not good. How can I subscribe? I would like to read you in the future!…
Uh, well, explain me a please, I am not quite in the subject, how can it be?!…
pRxFeo Extremely easy by words but in reality�, a lot of things don`t correspond. Not everything is so rosy..!!
Amazing blog! Is your theme custom made or did you download it from somewhere? A theme like yours with a few simple adjustements would really make my blog jump out. Please let me know where you got your design. Thanks
Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts.
Clear, informative, smilpe. Could I send you some e-hugs?
Sometimes I also see something like this, but earlier I didn`t pay much attention to this!….
The author deserves for the monument:DD
Interesting. We are waiting for new messages on the same topic!!….
I am getting married on the 15th of November. Congratulate me! Then will be here rarely!….
I do`t see a feedback or the other coordinates from the blog administration!….
I am really enjoying reading your well written articles.
Uh, well, explain me a please, I am not quite in the subject, how can it be?!….
I subscribed to RSS, but for some reason, the messages are written in the form of some hieroglyph (How can it be corrected?!….
The Author is crazy..!!
Yeah, it is clear now !… Just can not figure out how often do you update your blog?!…
I read online (computer problems) positive feedback about your resource. Didnt even believe it, and now saw myself. It turned out that I was not fooled!…
Of course, I understand a little about this post but will try cope with it!!….
It`s really useful! Looking through the Internet you can mostly observe watered down information, something like bla bla bla, but not here to my deep surprise. It makes me happy..!!
Somewhere in the Internet I have already read almost the same selection of information, but anyway thanks!!…
Comrade kill yourself..
I read and feel at home. Thanks the creators for a good resource..!!
It’s pleasant sitting at work to distract from it�to relax and read the information written here:DD
Informative, but not convincing. Something is missing but what I can not understand. But I will say frankly: bright and benevolent thoughts!….
52. “The road will be overcome by that person, who goes.” I wish you never stopped and be creative – forever..!
The topic is pretty complicated for a beginner!….
Thanks for the article! I hope the author does not mind if I use it for my course work!…
Youth rock band “Ranetki” says thank you for such a wonderful blog..!!
Uh, well, explain me a please, I am not quite in the subject, how can it be?!….
Certainly. And I have faced it. We can communicate on this theme. Here or in PM.
Internet is written with the capital letter in a sentence, by the way. And hundredths are written not with a point but with a comma. This is according to the standard. And actually everything is very good..!!
I would add something else, of course, but in fact almost everything is mentioned!….
Stupid article..!!
Yeah !… life is like riding a bicycle. You will not fall unless you stop pedaling!!….
Si dovrebbe dire di aver messo in confusione.
I`m so grateful that you enlightened me and the most important thing that it happened in time. Just think, I have been using the internet for six years already but it`s the first time I`ve ever heard about it!…
07128c07baf4da1c3e38f902fe89366b seo
Yeah, now it’s clear !… And firstly I did not understand very much where there was the link with the title itself !!….
Well, actually, a lot of what you write is not quite true !… well, okay, it does not matter:DD
Scribbler, give me a student’s record-book!))))
Thank you, a very interesting note!!….
Current blog, fresh information, I read it from time to time!!….
Somewhere in the Internet I have already read almost the same selection of information, but anyway thanks!!….
Author, keep doing in the same way..!!
I read online (computer problems) positive feedback about your resource. Didnt even believe it, and now saw myself. It turned out that I was not fooled!….
022b324d26b559eb9b1ce991d8dd3447 seo
Cool:) I would say say it exploded my brain..!!
Heartfelt thanks..!!