|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pratiksh\Nepalidate\Services; |
|
4
|
|
|
|
|
5
|
|
|
use Carbon\Carbon; |
|
6
|
|
|
|
|
7
|
|
|
final class NepaliDate extends DateConverter |
|
8
|
|
|
{ |
|
9
|
|
|
public $eng_year; |
|
10
|
|
|
public $eng_month; |
|
11
|
|
|
public $eng_day; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct($eng_year, $eng_month, $eng_day) |
|
14
|
|
|
{ |
|
15
|
|
|
$this->eng_year = $eng_year; |
|
16
|
|
|
$this->eng_month = $eng_month; |
|
17
|
|
|
$this->eng_day = $eng_day; |
|
18
|
|
|
|
|
19
|
|
|
// Conversion |
|
20
|
|
|
$this->convertToBS(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
public static function create(Carbon $date): NepaliDate |
|
24
|
|
|
{ |
|
25
|
|
|
return new static($date->year, $date->month, $date->day); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public static function fromAD(Carbon $date): NepaliDate |
|
29
|
|
|
{ |
|
30
|
|
|
return new static($date->year, $date->month, $date->day); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function convertToBS() |
|
34
|
|
|
{ |
|
35
|
|
|
// Throw exception if out of range |
|
36
|
|
|
$this->is_in_range_eng($this->eng_year, $this->eng_month, $this->eng_day); |
|
37
|
|
|
$this->conversion(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function toCarbon() |
|
41
|
|
|
{ |
|
42
|
|
|
return Carbon::create($this->year, $this->month, $this->day); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
protected function conversion() |
|
46
|
|
|
{ |
|
47
|
|
|
$total_ad_days = $this->calculateTotalEnglishDays($this->eng_year, $this->eng_month, $this->eng_day); |
|
48
|
|
|
$initial_nepali_year = 2000; |
|
49
|
|
|
$initial_nepali_month = 9; |
|
50
|
|
|
$initial_nepali_day = 16; |
|
51
|
|
|
$dayOfWeek = $this->dayOfWeek; |
|
52
|
|
|
|
|
53
|
|
|
$nepali_year = $initial_nepali_year; |
|
54
|
|
|
$nepali_month = $initial_nepali_month; |
|
55
|
|
|
$nepali_day = $initial_nepali_day; |
|
56
|
|
|
|
|
57
|
|
|
$i = 0; |
|
58
|
|
|
$j = $initial_nepali_month; |
|
59
|
|
|
|
|
60
|
|
|
while ($total_ad_days != 0) { |
|
61
|
|
|
$lastDayOfMonth = $this->calendarData[$i][$j]; |
|
62
|
|
|
|
|
63
|
|
|
$nepali_day++; |
|
64
|
|
|
$dayOfWeek++; |
|
65
|
|
|
|
|
66
|
|
|
if ($nepali_day > $lastDayOfMonth) { |
|
67
|
|
|
$nepali_month++; |
|
68
|
|
|
$nepali_day = 1; |
|
69
|
|
|
$j++; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($dayOfWeek > 7) { |
|
73
|
|
|
$dayOfWeek = 1; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
if ($nepali_month > 12) { |
|
77
|
|
|
$nepali_year++; |
|
78
|
|
|
$nepali_month = 1; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if ($j > 12) { |
|
82
|
|
|
$j = 1; |
|
83
|
|
|
$i++; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->year = $nepali_year; |
|
87
|
|
|
$this->month = $nepali_month; |
|
88
|
|
|
$this->day = $nepali_day; |
|
89
|
|
|
$this->dayOfWeek = $dayOfWeek; |
|
90
|
|
|
|
|
91
|
|
|
$total_ad_days--; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
// Conversions Outputs |
|
96
|
|
|
public function toBS(): string |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->year.'-'.sprintf('%02d', $this->month).'-'.sprintf('%02d', $this->day); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
public function toBSArray(): array |
|
102
|
|
|
{ |
|
103
|
|
|
return [ |
|
104
|
|
|
'year' => $this->year, |
|
105
|
|
|
'month' => sprintf('%02d', $this->month), |
|
106
|
|
|
'day' => sprintf('%02d', $this->day), |
|
107
|
|
|
'dayOfWeek' => $this->dayOfWeek, |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
public function toFormattedEnglishBSDate() |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->day.' '. |
|
114
|
|
|
$this->getBSMonthInEnglish($this->month).' '. |
|
115
|
|
|
$this->year.','. |
|
116
|
|
|
' '. |
|
117
|
|
|
$this->getDayOfWeekInEnglish($this->dayOfWeek); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
public function toFormattedNepaliBSDate() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->getNumbersInNepali($this->day).' '. |
|
123
|
|
|
$this->getBSMonthInNepali($this->month).' '. |
|
124
|
|
|
$this->formattedNepaliNumber($this->year).','. |
|
125
|
|
|
' '. |
|
126
|
|
|
$this->getDayOfWeekInNepali($this->dayOfWeek); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|