@@ -16,15 +16,15 @@ class KhmerDateTime
1616 /**
1717 * @var Config
1818 */
19- private $ data ;
19+ private $ config ;
2020 /**
2121 * @var string
2222 */
2323 private $ sign ;
2424
2525 public function __construct ()
2626 {
27- $ this ->data = new Config ();
27+ $ this ->config = new Config ();
2828 return $ this ;
2929 }
3030
@@ -39,11 +39,7 @@ public static function parse($date) {
3939 $ instance = new static ();
4040 $ instance ->date = strtotime ($ date );
4141
42- if ($ instance ->isForwardSlash ($ date )) {
43- $ instance ->sign = "/ " ;
44- } elseif ($ instance ->isDash ($ date )) {
45- $ instance ->sign = "- " ;
46- } else {
42+ if (!$ instance ->date ) {
4743 throw new Exception ('Undefined date format ' );
4844 }
4945
@@ -71,7 +67,7 @@ public static function now()
7167 public function month ()
7268 {
7369 $ month = date ('m ' , $ this ->date );
74- return $ this ->data ->numbers ($ month );
70+ return $ this ->config ->numbers ($ month );
7571 }
7672
7773 /**
@@ -82,7 +78,7 @@ public function month()
8278 public function fullMonth ()
8379 {
8480 $ month = date ('n ' , $ this ->date );
85- return $ this ->data ->months ($ month );
81+ return $ this ->config ->months ($ month );
8682 }
8783
8884 /**
@@ -92,7 +88,7 @@ public function fullMonth()
9288 */
9389 public function day ()
9490 {
95- return $ this ->data ->numbers (date ('d ' , $ this ->date ));
91+ return $ this ->config ->numbers (date ('d ' , $ this ->date ));
9692 }
9793
9894 /**
@@ -102,7 +98,7 @@ public function day()
10298 */
10399 public function fullDay ()
104100 {
105- return $ this ->data ->days (date ('w ' , $ this ->date ));
101+ return $ this ->config ->days (date ('w ' , $ this ->date ));
106102 }
107103
108104 /**
@@ -112,53 +108,57 @@ public function fullDay()
112108 */
113109 public function year ()
114110 {
115- return $ this ->data ->numbers (date ('Y ' , $ this ->date ));
111+ return $ this ->config ->numbers (date ('Y ' , $ this ->date ));
116112 }
117113
118114 /**
119- * Get date base on format.
115+ * Get hour in Khmer
120116 *
121- * @param null $format
122117 * @return string
123- * @throws Exception
124118 */
125- public function date ( $ format = null )
119+ public function hour ( )
126120 {
127- if ($ format == "long " ) {
128- return "ថ្ងៃ " .$ this ->fullDay ()." ទី " .$ this ->day ()." ខែ " .$ this ->fullMonth ()." ឆ្នាំ " .$ this ->year ();
129- }
130- if ($ format == "short " ) {
131- return $ this ->day ()." " .$ this ->fullMonth ()." " .$ this ->year ();
132- }
133- if ($ format == "forward " ) {
134- return $ this ->day ()."/ " .$ this ->month ()."/ " .$ this ->year ();
135- }
136- if ($ format == "dash " ) {
137- return $ this ->day ()."- " .$ this ->month ()."- " .$ this ->year ();
138- }
139-
140- return implode (" " , [$ this ->fullDay (), $ this ->day (), $ this ->fullMonth (), $ this ->year ()]);
121+ $ hour = date ('H ' , $ this ->date );
122+ return $ this ->config ->numbers ($ hour );
141123 }
142124
143125 /**
144- * Checking if given date is a forward slash format.
126+ * Get minute in Khmer
145127 *
146- * @param $date
147- * @return bool
128+ * @return string
148129 */
149- private function isForwardSlash ( $ date )
130+ public function minute ( )
150131 {
151- return is_object (DateTime:: createFromFormat ( ' Y/m/d ' , $ date ));
132+ return $ this -> config -> numbers ( date ( ' i ' , $ this -> date ));
152133 }
153134
154135 /**
155- * Checking if given date is a dash format.
136+ * Get time meridiem
156137 *
157- * @param $date
158- * @return bool
138+ * @return string
159139 */
160- private function isDash ($ date )
140+ public function meridiem () {
141+ return $ this ->config ->meridiem [date ('a ' , $ this ->date )];
142+ }
143+
144+ public function format ($ format )
161145 {
162- return is_object (DateTime::createFromFormat ('Y-m-d ' , $ date ));
146+ try {
147+ return $ this ->availableFormats ()[$ format ];
148+ } catch (Exception $ e ) {
149+ throw new Exception ("Invalid format " );
150+ }
151+ }
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+ ];
163163 }
164164}
0 commit comments