maxmeffert/feiertage

A PHP7 utility for legal holidiays in Germany

v2.1.0 2016-08-26 07:58 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 intrawarez/feiertage

Documentation

Documentation can be found here.

Usage

Get all holidays for specific year

$ft = Feiertage::of(2016);

Get a specific holliday

$os = Feiertag::OsterSonntag(2016);

or use array-access capabilities:

$ft = Feiertage::of(2016);
$os = $ft[Feiertag::OSTERSONNTAG];

Check dates for holidays

$date = new \DateTime(...);
Feiertage::check($date); // true or false

or get the corresponding constang:

$date = new \DateTime(...);
$which = Feiertage::which($date); // returns an Optional aka MayBe data structure

if ($which->isPresent()) {

	... $which->get() ...

}

Iterate over holidays

foreach (Feiertage::of(2016) 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) 2016 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