22
33namespace KhmerDateTime ;
44
5- use DateTime ;
65use Exception ;
76
87date_default_timezone_set ("Asia/Phnom_Penh " );
98
109class KhmerDateTime
1110{
12- /**
13- * @var Date
14- */
15- public $ date ;
11+ use Format;
12+
13+ public $ dateTime ;
1614 /**
1715 * @var Config
1816 */
1917 private $ config ;
2018 /**
2119 * @var string
2220 */
23- private $ sign ;
2421
2522 public function __construct ()
2623 {
2724 $ this ->config = new Config ();
25+ $ this ->dateTime = strtotime (date ("Y-m-d H:i " ));
2826 return $ this ;
2927 }
3028
3129 /**
3230 * Parse the datetime in String format
3331 *
34- * @param $date
32+ * @param $dateTime
3533 * @return static
3634 * @throws Exception
3735 */
38- public static function parse ($ date ) {
36+ public static function parse ($ dateTime ) {
3937 $ instance = new static ();
40- $ instance ->date = strtotime ($ date );
38+ $ instance ->dateTime = strtotime ($ dateTime );
4139
42- if (!$ instance ->date ) {
40+ if (!$ instance ->dateTime ) {
4341 throw new Exception ('Undefined date format ' );
4442 }
4543
@@ -53,10 +51,7 @@ public static function parse($date) {
5351 */
5452 public static function now ()
5553 {
56- $ instance = new static ();
57- $ instance ->date = strtotime (date ("Y-m-d " ));
58-
59- return $ instance ;
54+ return new static ();
6055 }
6156
6257 /**
@@ -66,8 +61,7 @@ public static function now()
6661 */
6762 public function month ()
6863 {
69- $ month = date ('m ' , $ this ->date );
70- return $ this ->config ->numbers ($ month );
64+ return $ this ->config ->numbers (date ('m ' , $ this ->dateTime ));
7165 }
7266
7367 /**
@@ -77,8 +71,7 @@ public function month()
7771 */
7872 public function fullMonth ()
7973 {
80- $ month = date ('n ' , $ this ->date );
81- return $ this ->config ->months ($ month );
74+ return $ this ->config ->months (date ('n ' , $ this ->dateTime ));
8275 }
8376
8477 /**
@@ -88,7 +81,7 @@ public function fullMonth()
8881 */
8982 public function day ()
9083 {
91- return $ this ->config ->numbers (date ('d ' , $ this ->date ));
84+ return $ this ->config ->numbers (date ('d ' , $ this ->dateTime ));
9285 }
9386
9487 /**
@@ -98,7 +91,7 @@ public function day()
9891 */
9992 public function fullDay ()
10093 {
101- return $ this ->config ->days (date ('w ' , $ this ->date ));
94+ return $ this ->config ->days (date ('w ' , $ this ->dateTime ));
10295 }
10396
10497 /**
@@ -108,7 +101,7 @@ public function fullDay()
108101 */
109102 public function year ()
110103 {
111- return $ this ->config ->numbers (date ('Y ' , $ this ->date ));
104+ return $ this ->config ->numbers (date ('Y ' , $ this ->dateTime ));
112105 }
113106
114107 /**
@@ -118,7 +111,7 @@ public function year()
118111 */
119112 public function hour ()
120113 {
121- $ hour = date ('H ' , $ this ->date );
114+ $ hour = date ('H ' , $ this ->dateTime );
122115 return $ this ->config ->numbers ($ hour );
123116 }
124117
@@ -129,7 +122,7 @@ public function hour()
129122 */
130123 public function minute ()
131124 {
132- return $ this ->config ->numbers (date ('i ' , $ this ->date ));
125+ return $ this ->config ->numbers (date ('i ' , $ this ->dateTime ));
133126 }
134127
135128 /**
@@ -138,27 +131,22 @@ public function minute()
138131 * @return string
139132 */
140133 public function meridiem () {
141- return $ this ->config ->meridiem [date ('a ' , $ this ->date )];
134+ return $ this ->config ->meridiem [date ('a ' , $ this ->dateTime )];
142135 }
143136
137+ /**
138+ * Return dateTime base on format
139+ *
140+ * @param $format
141+ * @return mixed
142+ * @throws Exception
143+ */
144144 public function format ($ format )
145145 {
146146 try {
147- return $ this ->availableFormats ()[ $ format] ;
147+ return $ this ->dateTimeFormat ( $ format) ;
148148 } catch (Exception $ e ) {
149149 throw new Exception ("Invalid format " );
150150 }
151151 }
152-
153- public function availableFormats () {
154- return [
155- 'L ' => $ this ->day ()."/ " .$ this ->month ()."/ " .$ this ->year (),
156- 'LL ' => $ this ->day ()." " .$ this ->fullMonth ()." " .$ this ->year (),
157- 'LLT ' => $ this ->day ()." " .$ this ->fullMonth ()." " .$ this ->year ()." " .$ this ->hour ().": " .$ this ->minute ()." " .$ this ->meridiem (),
158- 'LLL ' => $ this ->fullDay ()." " .$ this ->day ()." " .$ this ->fullMonth ()." " .$ this ->year (),
159- 'LLLT ' => $ this ->fullDay ()." " .$ this ->day ()." " .$ this ->fullMonth ()." " .$ this ->year ()." " .$ this ->hour ().": " .$ this ->minute ()." " .$ this ->meridiem (),
160- 'LLLL ' => "ថ្ងៃ " .$ this ->fullDay ()." ទី " .$ this ->day ()." ខែ " .$ this ->fullMonth ()." ឆ្នាំ " .$ this ->year (),
161- 'LLLLT ' => "ថ្ងៃ " .$ this ->fullDay ()." ទី " .$ this ->day ()." ខែ " .$ this ->fullMonth ()." ឆ្នាំ " .$ this ->year ()." " .$ this ->hour ().": " .$ this ->minute ()." " .$ this ->meridiem (),
162- ];
163- }
164152}
0 commit comments