2
2
3
3
namespace KhmerDateTime ;
4
4
5
- use DateTime ;
6
5
use Exception ;
7
6
8
7
date_default_timezone_set ("Asia/Phnom_Penh " );
9
8
10
9
class KhmerDateTime
11
10
{
12
- /**
13
- * @var Date
14
- */
15
- public $ date ;
11
+ use Format;
12
+
13
+ public $ dateTime ;
16
14
/**
17
15
* @var Config
18
16
*/
19
17
private $ config ;
20
18
/**
21
19
* @var string
22
20
*/
23
- private $ sign ;
24
21
25
22
public function __construct ()
26
23
{
27
24
$ this ->config = new Config ();
25
+ $ this ->dateTime = strtotime (date ("Y-m-d H:i " ));
28
26
return $ this ;
29
27
}
30
28
31
29
/**
32
30
* Parse the datetime in String format
33
31
*
34
- * @param $date
32
+ * @param $dateTime
35
33
* @return static
36
34
* @throws Exception
37
35
*/
38
- public static function parse ($ date ) {
36
+ public static function parse ($ dateTime ) {
39
37
$ instance = new static ();
40
- $ instance ->date = strtotime ($ date );
38
+ $ instance ->dateTime = strtotime ($ dateTime );
41
39
42
- if (!$ instance ->date ) {
40
+ if (!$ instance ->dateTime ) {
43
41
throw new Exception ('Undefined date format ' );
44
42
}
45
43
@@ -53,10 +51,7 @@ public static function parse($date) {
53
51
*/
54
52
public static function now ()
55
53
{
56
- $ instance = new static ();
57
- $ instance ->date = strtotime (date ("Y-m-d " ));
58
-
59
- return $ instance ;
54
+ return new static ();
60
55
}
61
56
62
57
/**
@@ -66,8 +61,7 @@ public static function now()
66
61
*/
67
62
public function month ()
68
63
{
69
- $ month = date ('m ' , $ this ->date );
70
- return $ this ->config ->numbers ($ month );
64
+ return $ this ->config ->numbers (date ('m ' , $ this ->dateTime ));
71
65
}
72
66
73
67
/**
@@ -77,8 +71,7 @@ public function month()
77
71
*/
78
72
public function fullMonth ()
79
73
{
80
- $ month = date ('n ' , $ this ->date );
81
- return $ this ->config ->months ($ month );
74
+ return $ this ->config ->months (date ('n ' , $ this ->dateTime ));
82
75
}
83
76
84
77
/**
@@ -88,7 +81,7 @@ public function fullMonth()
88
81
*/
89
82
public function day ()
90
83
{
91
- return $ this ->config ->numbers (date ('d ' , $ this ->date ));
84
+ return $ this ->config ->numbers (date ('d ' , $ this ->dateTime ));
92
85
}
93
86
94
87
/**
@@ -98,7 +91,7 @@ public function day()
98
91
*/
99
92
public function fullDay ()
100
93
{
101
- return $ this ->config ->days (date ('w ' , $ this ->date ));
94
+ return $ this ->config ->days (date ('w ' , $ this ->dateTime ));
102
95
}
103
96
104
97
/**
@@ -108,7 +101,7 @@ public function fullDay()
108
101
*/
109
102
public function year ()
110
103
{
111
- return $ this ->config ->numbers (date ('Y ' , $ this ->date ));
104
+ return $ this ->config ->numbers (date ('Y ' , $ this ->dateTime ));
112
105
}
113
106
114
107
/**
@@ -118,7 +111,7 @@ public function year()
118
111
*/
119
112
public function hour ()
120
113
{
121
- $ hour = date ('H ' , $ this ->date );
114
+ $ hour = date ('H ' , $ this ->dateTime );
122
115
return $ this ->config ->numbers ($ hour );
123
116
}
124
117
@@ -129,7 +122,7 @@ public function hour()
129
122
*/
130
123
public function minute ()
131
124
{
132
- return $ this ->config ->numbers (date ('i ' , $ this ->date ));
125
+ return $ this ->config ->numbers (date ('i ' , $ this ->dateTime ));
133
126
}
134
127
135
128
/**
@@ -138,27 +131,22 @@ public function minute()
138
131
* @return string
139
132
*/
140
133
public function meridiem () {
141
- return $ this ->config ->meridiem [date ('a ' , $ this ->date )];
134
+ return $ this ->config ->meridiem [date ('a ' , $ this ->dateTime )];
142
135
}
143
136
137
+ /**
138
+ * Return dateTime base on format
139
+ *
140
+ * @param $format
141
+ * @return mixed
142
+ * @throws Exception
143
+ */
144
144
public function format ($ format )
145
145
{
146
146
try {
147
- return $ this ->availableFormats ()[ $ format] ;
147
+ return $ this ->dateTimeFormat ( $ format) ;
148
148
} catch (Exception $ e ) {
149
149
throw new Exception ("Invalid format " );
150
150
}
151
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
- ];
163
- }
164
152
}
0 commit comments