MySQL get records for monthly invoice taking different length of months into account

155
May 30, 2017, at 07:20 AM

I have a database table called subscriptions. Each row in the table has a created_at date, from this day on, every month (afterwards) the subscription must be invoiced.

So on 2017-05-29, all subscriptions with a created_at day == '29' must be invoiced, regardless the month or year. So I thought of this:

SELECT * FROM subscriptions WHERE DAY(created_at) = DAY(DATE_SUB(CURDATE(), INTERVAL 1 MONTH))

But in that case I get in trouble when the previous month has 30 days, because on the 30th, it returns 30, but on the 31st it also returns 30. So all subscriptions with a created_at day '30' will be invoiced twice. Also February will give issues.

Another issue is the other way around, how will I invoice 2017-03-31 if there is no 31st day in April.

I could do multiple check in PHP and check if already invoiced that month etc. But I wonder if I could just fix this with MySQL.

Answer 1
WHERE
    /* don't invoice new subs from this month */
    LAST_DAY(created_at)<LAST_DAY(CURDATE())
    AND
    (
    /* exactly match sub's day value with today's day value */
    DAY(created_at)=DAY(CURDATE())
    OR
    /* on last day of month, match sub's day if equal to or greater than today's day */
    (CURDATE()=LAST_DAY(CURDATE()) AND DAY(created_at)>=DAY(CURDATE()))   
    )

CURDATE() and LAST_DAY() return a full date string (yyyy-mm-dd).

DAY() returns an integer (d or dd).

READ ALSO
Multiple search filters AJAX

Multiple search filters AJAX

I m referring to this : https://wwww3schools

125
php wysiwyg image upload library like wordpress [on hold]

php wysiwyg image upload library like wordpress [on hold]

I need some basic wysiwyg jquery editor with the ability to add images with some data like in Wordpress ediotr that has title , alt and caption to every file(http://imgur

196
Passing variables through JQuery without onclick event for setInterval

Passing variables through JQuery without onclick event for setInterval

I have a simple ajax script that inserts data in databaseWorks great

221
Cannot modify header information - WSOD

Cannot modify header information - WSOD

wp-admin or dashboard wsod

188