Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In MySQL date intervals, is 1 MONTH the same as 30 DAY? Is 1 QUARTER the same as 3 MONTH? And so on?

I am trying to write a PHP script that will process recurring payments every month, quarter, year, etc. This script will run as a nightly Cron job.

I don't want to run into a situation where somebody subscribes, say, on the 15th of a January, and then gets billed again on the 1st of February.

Is that what would happen if I checked the last payment against INTERVAL 1 MONTH? Or would it be the same as INTERVAL 30 DAY, and only process the payment again on the 15th of February, which is what I want?

like image 316
Nub Avatar asked Oct 09 '22 17:10

Nub


1 Answers

Accroding to MYSQL

If you add MONTH, YEAR_MONTH, or YEAR and the resulting date has a day that is larger than the maximum day for the new month, the day is adjusted to the maximum days in the new month:

mysql> SELECT DATE_ADD('2009-01-30', INTERVAL 1 MONTH); -> '2009-02-28' Date arithmetic operations require complete dates and do not work with incomplete dates such as '2006-07-00' or badly malformed dates:

Thus if you use the 1 month built in function you do not have to worry when the last day of the month is. MYSQL does all the work for you.

like image 79
Michael Eakins Avatar answered Oct 13 '22 10:10

Michael Eakins