66
77/**
88 * Class PersianDateHelper
9- * @package Tartan \Zaman
9+ * @package PhpMonsters \Zaman
1010 * @author Aboozar Ghaffari <[email protected] > 1111 */
1212class PersianDateHelper
@@ -15,13 +15,13 @@ class PersianDateHelper
1515 * Gregorian to Jalali
1616 *
1717 * @param $date
18- * @param string $format
19- * @param string $locale
18+ * @param string $format
19+ * @param string $locale
20+ * @return string
2021 * @link http://userguide.icu-project.org/formatparse/datetime
2122 *
22- * @return string
2323 */
24- public function gToj ($ date , $ format = 'yyyy/MM/dd H:m:s ' , $ locale = 'fa ' , $ timezone = null )
24+ public function gToj ($ date , string $ format = 'yyyy/MM/dd H:m:s ' , string $ locale = 'fa ' , $ timezone = null )
2525 {
2626 $ date = new IntlDatetime ($ date , $ timezone , 'gregorian ' );
2727 $ date ->setCalendar ('persian ' );
@@ -34,15 +34,20 @@ public function gToj ($date, $format = 'yyyy/MM/dd H:m:s', $locale = 'fa', $time
3434 * Jalali to Gregorian
3535 *
3636 * @param $date
37- * @param string $format
38- * @param string $inputLocale
39- * @param string $locale
37+ * @param string $format
38+ * @param string $inputLocale
39+ * @param string $locale
40+ * @return string
4041 * @link http://userguide.icu-project.org/formatparse/datetime
4142 *
42- * @return string
4343 */
44- public function jTog ($ date , $ format = 'yyyy/MM/dd H:m:s ' , $ inputLocale = 'fa ' , $ locale = 'en ' , $ timezone = 'Asia/Tehran ' )
45- {
44+ public function jTog (
45+ $ date ,
46+ string $ format = 'yyyy/MM/dd H:m:s ' ,
47+ string $ inputLocale = 'fa ' ,
48+ string $ locale = 'en ' ,
49+ $ timezone = 'Asia/Tehran '
50+ ) {
4651 $ date = new IntlDatetime ($ date , $ timezone , 'persian ' , $ inputLocale );
4752
4853 $ date ->setCalendar ('Gregorian ' );
@@ -52,58 +57,66 @@ public function jTog ($date, $format = 'yyyy/MM/dd H:m:s', $inputLocale = 'fa',
5257 }
5358
5459 /**
55- * @param integer $timestamp timestamp
60+ * @param string $timestamp timestamp
5661 *
57- * @return false| string
62+ * @return string
5863 */
59- public function moment ( $ timestamp )
64+ public function moment ( string $ timestamp )
6065 {
6166 if (!ctype_digit ($ timestamp )) {
6267 $ timestamp = strtotime ($ timestamp );
6368 }
6469 $ diff = time () - $ timestamp ;
6570 if ($ diff == 0 ) {
6671 return 'اکنون ' ;
67- }
68- elseif ($ diff > 0 ) {
72+ } elseif ($ diff > 0 ) {
6973 $ day_diff = floor ($ diff / 86400 );
7074 if ($ day_diff == 0 ) {
71- if ($ diff < 60 ) return 'اکنون ' ;
72- if ($ diff < 120 ) return 'یک دقیقه قبل ' ;
73- if ($ diff < 3600 ) return floor ($ diff / 60 ) . ' دقیقه قبل ' ;
74- if ($ diff < 7200 ) return 'یک ساعت پیش ' ;
75- if ($ diff < 86400 ) return floor ($ diff / 3600 ) . ' ساعت قبل ' ;
75+ if ($ diff < 60 ) {
76+ return 'اکنون ' ;
77+ }
78+ if ($ diff < 120 ) {
79+ return 'یک دقیقه قبل ' ;
80+ }
81+ if ($ diff < 3600 ) {
82+ return floor ($ diff / 60 ).' دقیقه قبل ' ;
83+ }
84+ if ($ diff < 7200 ) {
85+ return 'یک ساعت پیش ' ;
86+ }
87+ if ($ diff < 86400 ) {
88+ return floor ($ diff / 3600 ).' ساعت قبل ' ;
89+ }
7690 }
7791 if ($ day_diff == 1 ) {
7892 return 'دیروز ' ;
7993 }
8094 if ($ day_diff < 7 ) {
81- return $ day_diff . ' روز قبل ' ;
95+ return $ day_diff. ' روز قبل ' ;
8296 }
8397 if ($ day_diff < 31 ) {
84- return ceil ($ day_diff / 7 ) . ' هفته قبل ' ;
98+ return ceil ($ day_diff / 7 ). ' هفته قبل ' ;
8599 }
86100 if ($ day_diff < 60 ) {
87101 return 'ماه گذشته ' ;
88102 }
89103
90104 return date ('F Y ' , $ timestamp );
91- }
92- else {
93- $ diff = abs ($ diff );
105+ } else {
106+ $ diff = abs ($ diff );
94107 $ day_diff = floor ($ diff / 86400 );
95108 if ($ day_diff == 0 ) {
96109 if ($ diff < 120 ) {
97110 return 'یک دقیقه پیش ' ;
98111 }
99112 if ($ diff < 3600 ) {
100- return floor ($ diff / 60 ) . ' دقیقه پیش ' ;
113+ return floor ($ diff / 60 ). ' دقیقه پیش ' ;
101114 }
102115 if ($ diff < 7200 ) {
103116 return 'یک ساعت پیش ' ;
104117 }
105118 if ($ diff < 86400 ) {
106- return floor ($ diff / 3600 ) . ' ساعت پیش ' ;
119+ return floor ($ diff / 3600 ). ' ساعت پیش ' ;
107120 }
108121 }
109122 if ($ day_diff == 1 ) {
@@ -116,7 +129,7 @@ public function moment ($timestamp)
116129 return 'هفته بعد ' ;
117130 }
118131 if (ceil ($ day_diff / 7 ) < 4 ) {
119- return 'در ' . ceil ($ day_diff / 7 ) . ' هفته ' ;
132+ return 'در ' . ceil ($ day_diff / 7 ). ' هفته ' ;
120133 }
121134 if (date ('n ' , $ timestamp ) == date ('n ' ) + 1 ) {
122135 return 'ماه بعد ' ;
@@ -127,58 +140,66 @@ public function moment ($timestamp)
127140 }
128141
129142 /**
130- * @param integer $timestamp timestamp
143+ * @param string $timestamp timestamp
131144 *
132145 * @return false|string
133146 */
134- public function momentEn ( $ timestamp )
147+ public function momentEn ( string $ timestamp )
135148 {
136149 if (!ctype_digit ($ timestamp )) {
137150 $ timestamp = strtotime ($ timestamp );
138151 }
139152 $ diff = time () - $ timestamp ;
140153 if ($ diff == 0 ) {
141154 return 'now ' ;
142- }
143- elseif ($ diff > 0 ) {
155+ } elseif ($ diff > 0 ) {
144156 $ day_diff = floor ($ diff / 86400 );
145157 if ($ day_diff == 0 ) {
146- if ($ diff < 60 ) return 'just now ' ;
147- if ($ diff < 120 ) return '1 minute ago ' ;
148- if ($ diff < 3600 ) return floor ($ diff / 60 ) . ' minutes ago ' ;
149- if ($ diff < 7200 ) return '1 hour ago ' ;
150- if ($ diff < 86400 ) return floor ($ diff / 3600 ) . ' hours ago ' ;
158+ if ($ diff < 60 ) {
159+ return 'just now ' ;
160+ }
161+ if ($ diff < 120 ) {
162+ return '1 minute ago ' ;
163+ }
164+ if ($ diff < 3600 ) {
165+ return floor ($ diff / 60 ).' minutes ago ' ;
166+ }
167+ if ($ diff < 7200 ) {
168+ return '1 hour ago ' ;
169+ }
170+ if ($ diff < 86400 ) {
171+ return floor ($ diff / 3600 ).' hours ago ' ;
172+ }
151173 }
152174 if ($ day_diff == 1 ) {
153175 return 'Yesterday ' ;
154176 }
155177 if ($ day_diff < 7 ) {
156- return $ day_diff . ' days ago ' ;
178+ return $ day_diff. ' days ago ' ;
157179 }
158180 if ($ day_diff < 31 ) {
159- return ceil ($ day_diff / 7 ) . ' weeks ago ' ;
181+ return ceil ($ day_diff / 7 ). ' weeks ago ' ;
160182 }
161183 if ($ day_diff < 60 ) {
162184 return 'last month ' ;
163185 }
164186
165187 return date ('F Y ' , $ timestamp );
166- }
167- else {
168- $ diff = abs ($ diff );
188+ } else {
189+ $ diff = abs ($ diff );
169190 $ day_diff = floor ($ diff / 86400 );
170191 if ($ day_diff == 0 ) {
171192 if ($ diff < 120 ) {
172193 return 'in a minute ' ;
173194 }
174195 if ($ diff < 3600 ) {
175- return 'in ' . floor ($ diff / 60 ) . ' minutes ' ;
196+ return 'in ' . floor ($ diff / 60 ). ' minutes ' ;
176197 }
177198 if ($ diff < 7200 ) {
178199 return 'in an hour ' ;
179200 }
180201 if ($ diff < 86400 ) {
181- return 'in ' . floor ($ diff / 3600 ) . ' hours ' ;
202+ return 'in ' . floor ($ diff / 3600 ). ' hours ' ;
182203 }
183204 }
184205 if ($ day_diff == 1 ) {
@@ -191,7 +212,7 @@ public function momentEn ($timestamp)
191212 return 'next week ' ;
192213 }
193214 if (ceil ($ day_diff / 7 ) < 4 ) {
194- return 'in ' . ceil ($ day_diff / 7 ) . ' weeks ' ;
215+ return 'in ' . ceil ($ day_diff / 7 ). ' weeks ' ;
195216 }
196217 if (date ('n ' , $ timestamp ) == date ('n ' ) + 1 ) {
197218 return 'next month ' ;
0 commit comments