Skip to content

Instantly share code, notes, and snippets.

@stelgenhof
Last active February 13, 2019 01:23
Show Gist options
  • Save stelgenhof/14a14e1ab2d3123cf81aaff866ecda49 to your computer and use it in GitHub Desktop.
Save stelgenhof/14a14e1ab2d3123cf81aaff866ecda49 to your computer and use it in GitHub Desktop.
Example showing how to calculate remaining employee vacation days using Yasumi
<?php
require 'vendor/autoload.php';
use Yasumi\Provider\AbstractProvider;
use Yasumi\Provider\Germany\NorthRhineWestphalia;
use Yasumi\Yasumi;
// Initialization
const TIMEZONE = 'UTC';
$entitledVacationDays = 25;
try {
$publicHolidays = Yasumi::create(NorthRhineWestphalia::class, 2019, 'de_DE');
} catch (ReflectionException $e) {
echo $e->getMessage();
exit();
}
// Main
echo '+++ Vacation Days Summary +++' . PHP_EOL;
echo 'Employee : John Smith' . PHP_EOL;
echo 'Region : North Rhine-Westphalia (Germany)' . PHP_EOL;
printf('Entitled Vacation Days : %d' . PHP_EOL, $entitledVacationDays);
echo PHP_EOL;
echo '* New Years Holiday:' . PHP_EOL;
try {
$days = calculateWorkingDays(
new DateTime('2019-01-1T00:00:00', new DateTimeZone(TIMEZONE)),
new DateTime('2019-01-4T23:59:59', new DateTimeZone(TIMEZONE)),
$publicHolidays
);
$entitledVacationDays -= $days;
printBalance($days, $entitledVacationDays);
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
echo '* Spring Holiday:' . PHP_EOL;
try {
$days = calculateWorkingDays(
new DateTime('2019-04-15T00:00:00', new DateTimeZone(TIMEZONE)),
new DateTime('2019-04-28T23:59:59', new DateTimeZone(TIMEZONE)),
$publicHolidays
);
$entitledVacationDays -= $days;
printBalance($days, $entitledVacationDays);
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
/**
* Helper function to output the days to consume and remaining vacation days.
*
* @param int $days days to consume for this holiday period
* @param int $entitledVacationDays remaining number of vacation days
*/
function printBalance(int $days, int $entitledVacationDays)
{
printf(' > Vacation Days to consume: %d' . PHP_EOL, $days);
printf(' >> Remaining Vacation Days after this holiday: %d' . PHP_EOL, $entitledVacationDays);
echo PHP_EOL;
}
/**
* Calculates the number of working days for the given holiday period
*
* @param DateTimeInterface $start start date of the holiday period
* @param DateTimeInterface $end end date of the holiday period
* @param AbstractProvider $provider the Yasumi Holiday Provider instance (i.e. country/region)
* @return int the number of calculated working days
*/
function calculateWorkingDays(DateTimeInterface $start, DateTimeInterface $end, AbstractProvider $provider): int
{
try {
$datePeriod = new DatePeriod($start, (new DateInterval('P1D')), $end);
} catch (Exception $e) {
echo $e->getMessage();
exit();
}
$workingDays = 0;
foreach ($datePeriod as $date) {
echo $date->format("Y-m-d") . ': is a ';
if ($provider->isWorkingDay($date)) {
$workingDays++;
echo 'working day';
} else {
echo 'non-working day / holiday';
}
echo PHP_EOL;
}
return $workingDays;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment