## Remaining days calculation in MySql – Multiple maturities

I have the following problem when calculating the remaining days. I need the first period to expire before computing the days of the next period. In this example, even though 2 periods of 30 days were started on the same day, the duration is 60 days and therefore, as of today there are 38 days remaining, and not 16 as my formula gives.

Let’s imagine it as a subscription model in which a user is charged two payments for 30 days.

This is my solution so far.