Marcus Barnet
Marcus Barnet

Reputation: 2103

How to calculate multiple expiration date in PHP

Can you suggest me the best solution to use if I want to generate multiple expiration dates in PHP, please? I have something like this:

Name   |  activation_date  |  months
--------------------------------------
Record1    04/05/2016           3
Record2    01/01/2016           6
Record3    12/12/2015           12

For record1, I use the following function,

$expriration_date = date('Y-m-d', strtotime("+{$row['months']} months", strtotime($expriration_date)));

To generate the expiration date starting from the "activation_date" and then I save it into my SQL Database.

The problem is that I would like to generate multiple and periodic expiration dates.

For example, for record1, the expiration dates should be: 04/08/2016, 04/11/2016, 04/02/2016 and so on for 5 years and I would like to do the same for all the records. In five years, Record1 should have 20 expiration dates, Record2 should have 10 expiration dates and so on..

EDIT

Interval times can be 3, 6, 12, 24, 36, 48 and 50 months and I would like to generate periodic expiration dates for 5 years.

How can I do this in PHP?
Moreover, is there a way to notify the most recent expiration dates to the user, then?
I've read about cron-job: this is the only solution?

Thank you for all your support!

Upvotes: 0

Views: 267

Answers (1)

Adam Silenko
Adam Silenko

Reputation: 3108

You can use simple for loop

 for ($m = $months; $m <=60 ; $m += $months){
   //add $m to activation_date
   $expriration_date = DateTime::createFromFormat('Y-m-d', $activation_date);
   $expriration_date->add(new DateInterval('P'.$m.'M'));
   //do something with $expriration_date 
 }

You can do this by query, (but you don't post what db engine you use).
This query works on most popular db engines:

WITH
  intervals AS 
   ( SELECT 1 AS n, 3 AS m
     UNION ALL SELECT 2 AS n, 3 AS m
     UNION ALL SELECT 3 AS n, 3 AS m
     UNION ALL SELECT 4 AS n, 3 AS m
     UNION ALL SELECT 5 AS n, 3 AS m
     UNION ALL SELECT 6 AS n, 3 AS m
     UNION ALL SELECT 7 AS n, 3 AS m
     UNION ALL SELECT 8 AS n, 3 AS m
     UNION ALL SELECT 9 AS n, 3 AS m
     UNION ALL SELECT 10 AS n, 3 AS m
     UNION ALL SELECT 11 AS n, 3 AS m
     UNION ALL SELECT 12 AS n, 3 AS m
     UNION ALL SELECT 13 AS n, 3 AS m
     UNION ALL SELECT 14 AS n, 3 AS m
     UNION ALL SELECT 15 AS n, 3 AS m
     UNION ALL SELECT 16 AS n, 3 AS m
     UNION ALL SELECT 17 AS n, 3 AS m
     UNION ALL SELECT 18 AS n, 3 AS m
     UNION ALL SELECT 19 AS n, 3 AS m
     UNION ALL SELECT 20 AS n, 3 AS m
     UNION ALL SELECT 1 AS n, 6 AS m
     UNION ALL SELECT 2 AS n, 6 AS m
     UNION ALL SELECT 3 AS n, 6 AS m
     UNION ALL SELECT 4 AS n, 6 AS m
     UNION ALL SELECT 5 AS n, 6 AS m
     UNION ALL SELECT 6 AS n, 6 AS m
     UNION ALL SELECT 7 AS n, 6 AS m
     UNION ALL SELECT 8 AS n, 6 AS m
     UNION ALL SELECT 9 AS n, 6 AS m
     UNION ALL SELECT 10 AS n, 6 AS m
     UNION ALL SELECT 1 AS n, 12 AS m
     UNION ALL SELECT 2 AS n, 12 AS m
     UNION ALL SELECT 3 AS n, 12 AS m
     UNION ALL SELECT 4 AS n, 12 AS m
     UNION ALL SELECT 5 AS n, 12 AS m)
SELECT name, your_db_func_to_add_month(activation_date, n * m) AS expiration_dates
FROM something 
INNER JOIN intervals ON months = m
WHERE activation_date BETWEEN your_db_func_to_add_month(:testdate, -60) AND :testdate

the :testdate is parameter binded to query,
your_db_func_to_add_month is for example in mysql:

DATE_ADD(:testdate, INTERVAL 60 MONTH)

Edit:
if you define table intervals with data above, then you need only select. For MySQL this query looks like:

SELECT name, DATE_ADD(activation_date, INTERVAL n * m MONTH) (activation_date, n * m) AS expiration_dates
FROM something 
INNER JOIN intervals ON months = m
WHERE activation_date BETWEEN DATE_ADD(:testdate, - INTERVAL 60 MONTH) AND :testdate

Upvotes: 1

Related Questions