maxmeffert/feiertage

A PHP7 utility for legal holidiays in Germany

v3.0.1 2020-03-15 00:27 UTC

This package is auto-updated.

Last update: 2024-04-05 20:37:10 UTC


README

Build Status Coverage Status Latest Stable Version License

feiertage

A PHP7 utility for legal holidiays in Germany.

  • Computes all 19 legal holidays in Germany for a given year.
  • Based on the Gaussian Easter Formula
  • Not limited to unix years, i.e. before 1970 or after 2037 (does not rely on easter_date).
  • Tested for years: from 1700 to 2299.

Installation

composer require maxmeffert/feiertage

Usage

Get all holidays for specific year

$ft = Feiertage::of(2020);

Get a specific holliday

$ft = Feiertage::of(2020);
$os = $ft[FeiertagEnum::OSTERSONNTAG];

Check dates for holidays

$date = new \DateTime(...);
if (Feiertage::check($date))
{
	...
}

or get the corresponding constant:

$date = new \DateTime(...);
$which = Feiertage::which($date);
if ($which == FeiertagEnum::OSTERSONNTAG)
{
	... 
}

Iterate over holidays

foreach (Feiertage::of(2020) as $holiday)
{
	...
}

Dynamic Holidays

As for most western countries, all dynamic legal holidays in Germany are christian feasts, which are organized around Easter Sunday. That's why functions like easter_date exist. However easter_date is limited to unix years, i.e. before 1970 or after 2037. Instead of relying on easter_date this implementation is based on the Gaussian Easter Formula, which is not limited to unix years. The Gaussian Easter Formula computes Easter Sunday for a given year as a day of march, e.g. the 32th March is the 1st April, the 33rd March is the 2nd April, etc.

The implementation of the Gaussian Easter Formula used by this library looks like this:

function div (int $a, int $b) : int {
	return intval( $a / $b );
}

function mod (int $a, int $b) : int {
	return intval( $a % $b );
}

function gauss (int $year) : int {
	$a = mod($year, 19);
	$b = mod($year, 4);
	$c = mod($year, 7);
	$H1 = div($year, 100);
	$H2 = div($year, 400);
	$N = 4 + $H1 - $H2;
	$M = 15 + $H1 - $H2 - div(8 * $H1 + 13, 25);
	$d = mod(19 * $a + $M, 30);
	$e = mod(2 * $b + 4 * $c + 6 * $d + $N, 7);
	$o = 22 + $d + $e;

	if ($o == 57) {
		$o = 50;
	}
	
	if ($d == 28 && $e == 6 && $a > 10) {
		$o = 49;
	}

	return $o;
}

function easterSunday (int $year) : \DateTimeImmutable {
	$day = gauss($year);
	
	$month = 3;
	
	if (31 < $os) {
		$day = mod($os, 31);
		$month = 4;
	}
	
	return new \DateTimeImmutable("{$year}-{$month}-{$day}");
}

Other reference implementations can be found here:

License

The MIT License (MIT)

Copyright (c) 2020 Maximilian Meffert

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

https://opensource.org/licenses/MIT