-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHolidayChecker.php
106 lines (89 loc) · 2.88 KB
/
HolidayChecker.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
namespace Kvnc\Services;
use Carbon\Carbon;
class HolidayChecker
{
/**
* Get Turkish public holidays for a given year, including fixed and religious holidays.
*
* @param int|null $year
* @return array
*/
public static function getPublicHolidays(?int $year = null): array
{
$year = $year ?? (int) date('Y');
// Fixed public holidays
$fixedHolidays = [
"$year-01-01", // New Year's Day
"$year-04-23", // National Sovereignty and Children's Day
"$year-05-01", // Labour and Solidarity Day
"$year-05-19", // Commemoration of Atatürk, Youth and Sports Day
"$year-07-15", // Democracy and National Unity Day
"$year-08-30", // Victory Day
"$year-10-29", // Republic Day
];
// Add religious holidays
$religiousHolidays = self::getReligiousHolidays($year);
return array_merge($fixedHolidays, $religiousHolidays);
}
/**
* Calculate religious holidays for Turkey for a given year.
*
* @param int $year
* @return array
*/
public static function getReligiousHolidays(int $year): array
{
$holidays = [];
// Islamic dates for Eid al-Fitr (Ramazan Bayramı) and Eid al-Adha (Kurban Bayramı)
$eidAlFitr = self::hijriToGregorian($year, 10, 1); // 1st of Shawwal
$eidAlAdha = self::hijriToGregorian($year, 12, 10); // 10th of Dhu al-Hijjah
// Add the start day and possible multi-day celebrations
$holidays[] = $eidAlFitr->toDateString();
$holidays[] = $eidAlFitr->addDay()->toDateString();
$holidays[] = $eidAlFitr->addDay()->toDateString(); // Ramazan Bayramı: 3 days
$holidays[] = $eidAlAdha->toDateString();
$holidays[] = $eidAlAdha->addDay()->toDateString();
$holidays[] = $eidAlAdha->addDay()->toDateString();
$holidays[] = $eidAlAdha->addDay()->toDateString(); // Kurban Bayramı: 4 days
return $holidays;
}
/**
* Convert a Hijri date to a Gregorian date using an astronomical approximation.
*
* @param int $gregorianYear
* @param int $hijriMonth
* @param int $hijriDay
* @return Carbon
*/
private static function hijriToGregorian(int $gregorianYear, int $hijriMonth, int $hijriDay): Carbon
{
// Approximate difference between Gregorian and Hijri years
$hijriYear = $gregorianYear - 622 + intdiv(($gregorianYear - 622), 33);
// Convert Hijri date to Julian Day Number
$julianDay = intdiv(11 * $hijriYear + 3, 30)
+ 354 * $hijriYear
+ 30 * ($hijriMonth - 1)
+ $hijriDay
+ 1948440 - 385;
// Convert Julian Day Number to Gregorian date
$timestamp = jdtounix($julianDay);
return Carbon::createFromTimestamp($timestamp);
}
/**
* Check if today is a weekend or a public holiday in Turkey.
*
* @return bool
*/
public static function isDayOff(): bool
{
$today = Carbon::today();
// Check if it's a weekend
if ($today->isWeekend()) {
return true;
}
// Check if it's a public holiday
$holidays = self::getPublicHolidays();
return in_array($today->toDateString(), $holidays, true);
}
}