In the Christian liturgical year, Advent starts things off four weeks before Christmas, and there are three cycles (Year A, B, and C).
In PHP, what would be the most efficient and elegant method to determine what cycle we would be in for any given year.
Here are the rules:
- The beginning of the cycle occurs on whatever Sunday falls on or closest to Nov. 30
- November 30, 2016 was the beginning of year A.
- There are three iterations, being year A, year B, and year C, after which we return to year A.
So if given any year month and day, would it be possible to determine whether that date was in the cycle A, B, or C?
UPDATE: What I’ve tried
I’m not very good with math, so haven’t had much luck in figuring this out. I’ve been focusing first on the year and how it relates to A B and C. So if I equate A to 1 and B to 2 and C to 3, I could get the first three years by subtracting 2015 from the current date – but I don’t know what to do after 2018.
Advertisement
Answer
Thanks to @Qirel for pointing me in the right direction. I’ve never really used the modulus operator before!
So problem is solved as follows (though my coding might be sloppy / inefficient):
$cycle = array(1=>"A",2=>"B",3=>"C"); for($year=2016;$year<2120;$year++){ $date = strtotime("Nov 30, " . $year); echo "Sunday closest to " . date("Y-m-d",$date) . " is "; $date = new DateTime($year . "/11/30 -4 days"); $date->modify("next Sunday"); echo $date->format("Y-d-m"); echo " where we start Year " . $cycle[(($year % 3) + 1)] . "<br/>"; }
Here’s what’s going on:
$cycle is defined as an array, where numbers 1, 2, and 3 are associated with years A, B, and C.
I then start a for loop to work through the years 2016 to 2120, incrementing the years by 1 on each iteration.
I then create a date of XXXX Nov 30 minus 4 days, where XXXX is the year we’re iterating through. The minus four days helps us to be sure we’re considering the date closest to the 30th, so that when we modify it in the following line to “next Sunday” it will always be the one closest to the 30th.
We then use the array $cycle, divide the year by 3 and take the remainder (using the modulus operator) which gives us either 0, 1, or 2 as an answer. I add one to it so it’s returning 1, 2, or 3.
That isn’t really necessary if I assign 0=>A, 1=>B, and 2=>C but…
I wrote a function to return this information:
function returnLiturgicalYear($year){ $cycle = array(0=>"A",1=>"B",2=>"C"); $date = new DateTime($year . "/11/30 -4 days"); $date->modify("next Sunday"); return $cycle[($year % 3)]; }
Hopefully this helps someone else trying to determine the Christian liturgical year for any date.