1<?php
2
3// Careful! strtotime() will interpret 12/10/2020 as "10 December 2020", where you expect it to be 12 October 2020!
4// Consider the code below for an alternative, more robust solution.
5
6$today = new DateTime;
7
8// Added time for uniformity.
9// "Notice of default" used to indicate the final date for possible payment,
10// before services are suspended and/or legal action is taken.
11// use setDate() and setTime() to explicitly set the date/time, to avoid caveats with international date formats
12// as pointed out above.
13$noticeOfDefaultAt = (new DateTime)->setDate(2021, 2, 10)->setTime(7, 0);
14
15// First reminder (yellow) sent 3 months before expiration date.
16// DateInterval() accepts a formatted string which decodes here to:
17// P = Period of
18// 3 = 3
19// M = Months
20// Use sub() to get the period offset from the final payment date ($noticeOfDefault)
21$firstReminderAt = (clone $noticeOfDefaultAt)->sub(new DateInterval('P3M'));
22// Second reminder (orange) sent 1 month before expiration date.
23$secondReminderAt = (clone $noticeOfDefaultAt)->sub(new DateInterval('P1M'));
24
25// Default to transparent if within payment period.
26$bgColor = 'transparent';
27
28if ($today >= $firstReminderAt && $today < $secondReminderAt)
29 // Today is within grace period of first reminder.
30 $bgColor = 'yellow';
31
32if ($today >= $secondReminderAt && $today < $noticeOfDefaultAt)
33 // Today is within grace period of second reminder.
34 $bgColor = 'orange';
35
36if ($today >= $noticeOfDefaultAt)
37 // We have a really sh*tty customer. Send legal team.
38 $bgColor = 'red';
39
40// Change the color names to any rgb-hex value you want and use them in your "style" attribute.
41echo $bgColor;
42