3
3
namespace PhpMonsters \Zaman \Helpers ;
4
4
5
5
use Exception ;
6
+ use PhpMonsters \Zaman \Facades \Zaman ;
6
7
use PhpMonsters \Zaman \IntlDatetime ;
8
+ use RuntimeException ;
7
9
8
10
/**
9
11
* Class PersianDateHelper
10
12
* @package PhpMonsters\Zaman
11
13
* @author Aboozar Ghaffari <aboozar.ghf@gmail.com>
12
14
*/
13
- class PersianDateHelper
15
+ class ZamanDateHelper
14
16
{
15
17
/**
16
18
* Gregorian to Jalali
17
19
*
18
- * @param string $date
19
- * @param string $format
20
- * @param string $locale
21
- * @param string|null $timezone
20
+ * @param string $date
21
+ * @param string $format
22
+ * @param string $locale
23
+ * @param string|null $timezone
22
24
* @return string
23
25
* @throws Exception
24
26
* @link http://userguide.icu-project.org/formatparse/datetime
25
27
*/
26
28
public function gToj (
27
- mixed $ date ,
28
- string $ format = 'yyyy/MM/dd H:m:s ' ,
29
- string $ locale = 'fa ' ,
30
- string $ timezone = null
31
- ): string {
29
+ mixed $ date ,
30
+ string $ format = 'yyyy/MM/dd H:m:s ' ,
31
+ string $ locale = 'fa ' ,
32
+ ?string $ timezone = null
33
+ ): string
34
+ {
32
35
$ date = new IntlDatetime ($ date , $ timezone , 'gregorian ' );
33
36
$ date ->setCalendar ('persian ' );
34
37
$ date ->setLocale ($ locale );
@@ -39,11 +42,11 @@ public function gToj(
39
42
/**
40
43
* Jalali to Gregorian
41
44
*
42
- * @param mixed $date
43
- * @param string $format
44
- * @param string $inputLocale
45
- * @param string $locale
46
- * @param string $timezone
45
+ * @param mixed $date
46
+ * @param string $format
47
+ * @param string $inputLocale
48
+ * @param string $locale
49
+ * @param string $timezone
47
50
* @return string
48
51
* @throws Exception
49
52
* @link http://userguide.icu-project.org/formatparse/datetime
@@ -54,7 +57,8 @@ public function jTog(
54
57
string $ inputLocale = 'fa ' ,
55
58
string $ locale = 'en ' ,
56
59
string $ timezone = 'Asia/Tehran '
57
- ): string {
60
+ ): string
61
+ {
58
62
$ date = new IntlDatetime ($ date , $ timezone , 'persian ' , $ inputLocale );
59
63
60
64
$ date ->setCalendar ('Gregorian ' );
@@ -64,11 +68,25 @@ public function jTog(
64
68
}
65
69
66
70
/**
67
- * @param string $timestamp timestamp
71
+ * @param string $timestamp timestamp
72
+ * @param int $calendar
73
+ * @return string
74
+ */
75
+ public function moment (string $ timestamp , int $ calendar ): string
76
+ {
77
+ return match ($ calendar ) {
78
+ Zaman::CAL_JALALI => $ this ->momentJalali ($ timestamp ),
79
+ Zaman::CAL_HIJRI => $ this ->momentHijri ($ timestamp ),
80
+ default => $ this ->momentGregorian ($ timestamp ),
81
+ };
82
+ }
83
+
84
+ /**
85
+ * @param string $timestamp timestamp
68
86
*
69
87
* @return string
70
88
*/
71
- public function moment (string $ timestamp ): string
89
+ public function momentJalali (string $ timestamp ): string
72
90
{
73
91
if (!ctype_digit ($ timestamp )) {
74
92
$ timestamp = strtotime ($ timestamp );
@@ -78,7 +96,7 @@ public function moment(string $timestamp): string
78
96
return 'اکنون ' ;
79
97
}
80
98
81
- $ dayDiff = (int ) floor ($ diff / 86400 );
99
+ $ dayDiff = (int )floor ($ diff / 86400 );
82
100
83
101
if ($ diff > 0 ) {
84
102
if ($ dayDiff === 0 ) {
@@ -89,23 +107,23 @@ public function moment(string $timestamp): string
89
107
return 'یک دقیقه قبل ' ;
90
108
}
91
109
if ($ diff < 3600 ) {
92
- return floor ($ diff / 60 ). ' دقیقه قبل ' ;
110
+ return floor ($ diff / 60 ) . ' دقیقه قبل ' ;
93
111
}
94
112
if ($ diff < 7200 ) {
95
113
return 'یک ساعت پیش ' ;
96
114
}
97
115
if ($ diff < 86400 ) {
98
- return floor ($ diff / 3600 ). ' ساعت قبل ' ;
116
+ return floor ($ diff / 3600 ) . ' ساعت قبل ' ;
99
117
}
100
118
}
101
119
if ($ dayDiff === 1 ) {
102
120
return 'دیروز ' ;
103
121
}
104
122
if ($ dayDiff < 7 ) {
105
- return $ dayDiff. ' روز قبل ' ;
123
+ return $ dayDiff . ' روز قبل ' ;
106
124
}
107
125
if ($ dayDiff < 31 ) {
108
- return ceil ($ dayDiff / 7 ). ' هفته قبل ' ;
126
+ return ceil ($ dayDiff / 7 ) . ' هفته قبل ' ;
109
127
}
110
128
if ($ dayDiff < 60 ) {
111
129
return 'ماه گذشته ' ;
@@ -120,13 +138,13 @@ public function moment(string $timestamp): string
120
138
return 'یک دقیقه پیش ' ;
121
139
}
122
140
if ($ diff < 3600 ) {
123
- return floor ($ diff / 60 ). ' دقیقه پیش ' ;
141
+ return floor ($ diff / 60 ) . ' دقیقه پیش ' ;
124
142
}
125
143
if ($ diff < 7200 ) {
126
144
return 'یک ساعت پیش ' ;
127
145
}
128
146
if ($ diff < 86400 ) {
129
- return floor ($ diff / 3600 ). ' ساعت پیش ' ;
147
+ return floor ($ diff / 3600 ) . ' ساعت پیش ' ;
130
148
}
131
149
}
132
150
if ($ dayDiff === 1 ) {
@@ -139,21 +157,21 @@ public function moment(string $timestamp): string
139
157
return 'هفته بعد ' ;
140
158
}
141
159
if (ceil ($ dayDiff / 7 ) < 4 ) {
142
- return 'در ' . ceil ($ dayDiff / 7 ). ' هفته ' ;
160
+ return 'در ' . ceil ($ dayDiff / 7 ) . ' هفته ' ;
143
161
}
144
- if ((int ) date ('n ' , $ timestamp ) === (int ) date ('n ' ) + 1 ) {
162
+ if ((int )date ('n ' , $ timestamp ) === (int )date ('n ' ) + 1 ) {
145
163
return 'ماه بعد ' ;
146
164
}
147
165
148
166
return date ('F Y ' , $ timestamp );
149
167
}
150
168
151
169
/**
152
- * @param string $timestamp timestamp
170
+ * @param string $timestamp timestamp
153
171
*
154
- * @return false| string
172
+ * @return string
155
173
*/
156
- public function momentEn (string $ timestamp ): bool | string
174
+ public function momentGregorian (string $ timestamp ): string
157
175
{
158
176
if (!ctype_digit ($ timestamp )) {
159
177
$ timestamp = strtotime ($ timestamp );
@@ -163,7 +181,7 @@ public function momentEn(string $timestamp): bool|string
163
181
return 'now ' ;
164
182
}
165
183
166
- $ dayDiff = (int ) floor ($ diff / 86400 );
184
+ $ dayDiff = (int )floor ($ diff / 86400 );
167
185
168
186
if ($ diff > 0 ) {
169
187
if ($ dayDiff === 0 ) {
@@ -174,23 +192,23 @@ public function momentEn(string $timestamp): bool|string
174
192
return '1 minute ago ' ;
175
193
}
176
194
if ($ diff < 3600 ) {
177
- return floor ($ diff / 60 ). ' minutes ago ' ;
195
+ return floor ($ diff / 60 ) . ' minutes ago ' ;
178
196
}
179
197
if ($ diff < 7200 ) {
180
198
return '1 hour ago ' ;
181
199
}
182
200
if ($ diff < 86400 ) {
183
- return floor ($ diff / 3600 ). ' hours ago ' ;
201
+ return floor ($ diff / 3600 ) . ' hours ago ' ;
184
202
}
185
203
}
186
204
if ($ dayDiff === 1 ) {
187
205
return 'Yesterday ' ;
188
206
}
189
207
if ($ dayDiff < 7 ) {
190
- return $ dayDiff. ' days ago ' ;
208
+ return $ dayDiff . ' days ago ' ;
191
209
}
192
210
if ($ dayDiff < 31 ) {
193
- return ceil ($ dayDiff / 7 ). ' weeks ago ' ;
211
+ return ceil ($ dayDiff / 7 ) . ' weeks ago ' ;
194
212
}
195
213
if ($ dayDiff < 60 ) {
196
214
return 'last month ' ;
@@ -205,13 +223,13 @@ public function momentEn(string $timestamp): bool|string
205
223
return 'in a minute ' ;
206
224
}
207
225
if ($ diff < 3600 ) {
208
- return 'in ' . floor ($ diff / 60 ). ' minutes ' ;
226
+ return 'in ' . floor ($ diff / 60 ) . ' minutes ' ;
209
227
}
210
228
if ($ diff < 7200 ) {
211
229
return 'in an hour ' ;
212
230
}
213
231
if ($ diff < 86400 ) {
214
- return 'in ' . floor ($ diff / 3600 ). ' hours ' ;
232
+ return 'in ' . floor ($ diff / 3600 ) . ' hours ' ;
215
233
}
216
234
}
217
235
if ($ dayDiff === 1 ) {
@@ -224,12 +242,22 @@ public function momentEn(string $timestamp): bool|string
224
242
return 'next week ' ;
225
243
}
226
244
if (ceil ($ dayDiff / 7 ) < 4 ) {
227
- return 'in ' . ceil ($ dayDiff / 7 ). ' weeks ' ;
245
+ return 'in ' . ceil ($ dayDiff / 7 ) . ' weeks ' ;
228
246
}
229
- if ((int ) date ('n ' , $ timestamp ) === (int ) date ('n ' ) + 1 ) {
247
+ if ((int )date ('n ' , $ timestamp ) === (int )date ('n ' ) + 1 ) {
230
248
return 'next month ' ;
231
249
}
232
250
233
251
return date ('F Y ' , $ timestamp );
234
252
}
253
+
254
+ /**
255
+ * @param string $timestamp timestamp
256
+ *
257
+ * @return string
258
+ */
259
+ private function momentHijri (string $ timestamp ): string
260
+ {
261
+ throw new RuntimeException ('Hijri moment has not implemented yet! ' );
262
+ }
235
263
}
0 commit comments