|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pratiksh\Nepalidate\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
final class EnglishDate extends DateConverter |
|
8
|
|
|
{ |
|
9
|
|
|
public $nep_year; |
|
10
|
|
|
public $nep_month; |
|
11
|
|
|
public $nep_day; |
|
12
|
|
|
|
|
13
|
|
|
public Carbon $date; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct($nep_year, $nep_month, $nep_day) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->nep_year = $nep_year; |
|
18
|
|
|
$this->nep_month = $nep_month; |
|
19
|
|
|
$this->nep_day = $nep_day; |
|
20
|
|
|
|
|
21
|
|
|
// Conversion |
|
22
|
|
|
$this->convertToAD(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
public static function create(string $date): EnglishDate |
|
26
|
|
|
{ |
|
27
|
|
|
$date = str_replace('/', '-', $date); |
|
28
|
|
|
$date = explode('-', $date); |
|
29
|
|
|
if (count($date) != 3) { |
|
30
|
|
|
throw new \Exception('Invalid date format'); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
return new static($date[0], $date[1], $date[2]); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function fromBS(string $date): EnglishDate |
|
37
|
|
|
{ |
|
38
|
|
|
$date = str_replace('/', '-', $date); |
|
39
|
|
|
$date = explode('-', $date); |
|
40
|
|
|
if (count($date) != 3) { |
|
41
|
|
|
throw new \Exception('Invalid date format'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return new static($date[0], $date[1], $date[2]); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
public function convertToAD() |
|
48
|
|
|
{ |
|
49
|
|
|
// Throw exception if out of range |
|
50
|
|
|
$this->is_in_range_nep($this->nep_year, $this->nep_month, $this->nep_day); |
|
51
|
|
|
$this->conversion(); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function toCarbon() |
|
55
|
|
|
{ |
|
56
|
|
|
return Carbon::create($this->year, $this->month, $this->day); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Convert Nepali (BS) date to English (AD) date. |
|
61
|
|
|
*/ |
|
62
|
|
|
protected function conversion() |
|
63
|
|
|
{ |
|
64
|
|
|
// Calculate total days from reference Nepali date |
|
65
|
|
|
$total_bs_days = $this->calculateTotalNepaliDays($this->nep_year, $this->nep_month, $this->nep_day); |
|
66
|
|
|
|
|
67
|
|
|
// Initialize with reference English date (corresponding to Nepali 2000-01-01) |
|
68
|
|
|
// CORRECTED: Adjusted the reference date to fix the one-day offset |
|
69
|
|
|
$initial_english_year = 1943; |
|
70
|
|
|
$initial_english_month = 4; |
|
71
|
|
|
$initial_english_day = 13; // Changed from 14 to 13 |
|
72
|
|
|
$dayOfWeek = $this->dayOfWeek; |
|
73
|
|
|
|
|
74
|
|
|
// Set initial values |
|
75
|
|
|
$english_year = $initial_english_year; |
|
76
|
|
|
$english_month = $initial_english_month; |
|
77
|
|
|
$english_day = $initial_english_day; |
|
78
|
|
|
|
|
79
|
|
|
// Process each day |
|
80
|
|
|
while ($total_bs_days != 0) { |
|
81
|
|
|
// Increment the English date by one day |
|
82
|
|
|
$english_day++; |
|
83
|
|
|
$dayOfWeek++; |
|
84
|
|
|
|
|
85
|
|
|
// Handle day of week wraparound |
|
86
|
|
|
if ($dayOfWeek > 7) { |
|
87
|
|
|
$dayOfWeek = 1; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
// Handle month transition |
|
91
|
|
|
$days_in_month = $this->is_leap_year($english_year) ? |
|
92
|
|
|
$this->leapMonths[$english_month - 1] : |
|
93
|
|
|
$this->normalMonths[$english_month - 1]; |
|
94
|
|
|
|
|
95
|
|
|
if ($english_day > $days_in_month) { |
|
96
|
|
|
$english_month++; |
|
97
|
|
|
$english_day = 1; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
// Handle year transition |
|
101
|
|
|
if ($english_month > 12) { |
|
102
|
|
|
$english_year++; |
|
103
|
|
|
$english_month = 1; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
// Update object properties |
|
107
|
|
|
$this->year = $english_year; |
|
108
|
|
|
$this->month = $english_month; |
|
109
|
|
|
$this->day = $english_day; |
|
110
|
|
|
$this->dayOfWeek = $dayOfWeek; |
|
111
|
|
|
|
|
112
|
|
|
$total_bs_days--; |
|
113
|
|
|
} |
|
114
|
|
|
$date = Carbon::create($this->year, $this->month, $this->day); |
|
115
|
|
|
if ($date != false) { |
|
116
|
|
|
$this->date = $date; |
|
117
|
|
|
} else { |
|
118
|
|
|
throw new \Exception('Invalid date'); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function toAD($format = 'Y-m-d'): string |
|
123
|
|
|
{ |
|
124
|
|
|
return $this->date->format($format); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|