├── .gitignore
├── phpunit.xml
├── composer.json
├── .github
└── workflows
│ └── php.yml
├── LICENSE.md
├── src
├── Format.php
├── Config.php
└── KhmerDateTime.php
├── tests
└── KhmerDateTimeTest.php
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | /.idea
3 | /.vscode
4 | /.vagrant
5 | composer.lock
6 | .phpunit.result.cache
7 |
--------------------------------------------------------------------------------
/phpunit.xml:
--------------------------------------------------------------------------------
1 |
2 |
12 |
13 |
14 | ./tests/
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "phannaly/php-datetime-khmer",
3 | "description": "The PHP library for convert datetime to Khmer",
4 | "type": "library",
5 | "license": "MIT",
6 | "keywords": [
7 | "helpers",
8 | "datetime",
9 | "khmer",
10 | "cambodia"
11 | ],
12 | "authors": [
13 | {
14 | "name": "phanna",
15 | "email": "studentphanna@gmail.com"
16 | }
17 | ],
18 | "require": {},
19 | "autoload": {
20 | "psr-4": {
21 | "KhmerDateTime\\": "src/"
22 | }
23 | },
24 | "require-dev": {
25 | "phpunit/phpunit": "^9.0"
26 | },
27 | "minimum-stability": "stable"
28 | }
29 |
--------------------------------------------------------------------------------
/.github/workflows/php.yml:
--------------------------------------------------------------------------------
1 | name: Build
2 |
3 | on:
4 | push:
5 | branches: [ master ]
6 | pull_request:
7 | branches: [ master ]
8 |
9 | jobs:
10 | build:
11 |
12 | runs-on: ubuntu-latest
13 |
14 | steps:
15 | - uses: actions/checkout@v2
16 |
17 | - name: Validate composer.json and composer.lock
18 | run: composer validate
19 |
20 | - name: Cache Composer packages
21 | id: composer-cache
22 | uses: actions/cache@v2
23 | with:
24 | path: vendor
25 | key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
26 | restore-keys: |
27 | ${{ runner.os }}-php-
28 |
29 | - name: Install dependencies
30 | if: steps.composer-cache.outputs.cache-hit != 'true'
31 | run: composer install --prefer-dist --no-progress --no-suggest
32 |
33 | - name: Run test suite
34 | run: vendor/bin/phpunit
35 |
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | Copyright Phanna Ly
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4 |
5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6 |
7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
--------------------------------------------------------------------------------
/src/Format.php:
--------------------------------------------------------------------------------
1 | $this->formatL(),
17 | 'LL' => $this->formatLL(),
18 | 'LLT' => $this->formatLL()." ".$this->formatT(),
19 | 'LLL' => $this->formatLLL(),
20 | 'LLLT' => $this->formatLLL()." ".$this->formatT(),
21 | 'LLLL' => $this->formatLLLL(),
22 | 'LLLLT' => $this->formatLLLL()." ".$this->formatT()
23 | ];
24 |
25 | return $formats[$format];
26 | }
27 |
28 | private function formatL()
29 | {
30 | return $this->day()."/".$this->month()."/".$this->year();
31 | }
32 |
33 | private function formatLL()
34 | {
35 | return $this->day()." ".$this->fullMonth()." ".$this->year();
36 | }
37 |
38 | private function formatT()
39 | {
40 | return $this->hour().":".$this->minute()." ".$this->meridiem();
41 | }
42 |
43 | private function formatLLL()
44 | {
45 | return $this->fullDay()." ".$this->day()." ".$this->fullMonth()." ".$this->year();
46 | }
47 |
48 | private function formatLLLL()
49 | {
50 | return "ថ្ងៃ".$this->fullDay()." ទី".$this->day()." ខែ".$this->fullMonth()." ឆ្នាំ".$this->year();
51 | }
52 | }
53 |
--------------------------------------------------------------------------------
/src/Config.php:
--------------------------------------------------------------------------------
1 | '០',
9 | 1 => '១',
10 | 2 => '២',
11 | 3 => '៣',
12 | 4 => '៤',
13 | 5 => '៥',
14 | 6 => '៦',
15 | 7 => '៧',
16 | 8 => '៨',
17 | 9 => '៩'
18 | ];
19 | private $days = ['អាទិត្យ', 'ច័ន្ទ', 'អង្គារ', 'ពុធ', 'ព្រហស្បតិ៍', 'សុក្រ', 'សៅរ៍'];
20 | private $months = ['មករា', 'កុម្ភៈ', 'មីនា', 'មេសា', 'ឧសភា', 'មិថុនា', 'កក្កដា', 'សីហា', 'កញ្ញា', 'តុលា', 'វិច្ឆិកា', 'ធ្នូ'];
21 | public $meridiem = [
22 | 'am' => 'ព្រឹក',
23 | 'pm' => 'ល្ងាច'
24 | ];
25 |
26 | /**
27 | * Get Khmer day base on index
28 | *
29 | * @param $day
30 | * @return string
31 | */
32 | public function days($day)
33 | {
34 | return $this->days[$day];
35 | }
36 |
37 | /**
38 | * Get Khmer month base on index
39 | *
40 | * @param $month
41 | * @return string
42 | */
43 | public function months($month)
44 | {
45 | return $this->months[--$month];
46 | }
47 |
48 | /**
49 | * Convert English number to Khmer number
50 | *
51 | * @param $number
52 | * @return string
53 | */
54 | public function numbers($number)
55 | {
56 | $num = array_map(function ($str) {
57 | return $this->numeric[$str];
58 | }, str_split($number));
59 |
60 | return implode('', $num);
61 | }
62 | }
63 |
--------------------------------------------------------------------------------
/tests/KhmerDateTimeTest.php:
--------------------------------------------------------------------------------
1 | assertEquals('២២', $dateTime->day());
15 | $this->assertEquals('ពុធ', $dateTime->fullDay());
16 | $this->assertEquals('០៥', $dateTime->month());
17 | $this->assertEquals('ឧសភា', $dateTime->fullMonth());
18 | $this->assertEquals('២០១៩', $dateTime->year());
19 | $this->assertEquals('០០', $dateTime->hour());
20 | $this->assertEquals('០០', $dateTime->minute());
21 | $this->assertEquals('ព្រឹក', $dateTime->meridiem());
22 | $this->assertEquals('៤', $dateTime->week());
23 | $this->assertEquals('សប្តាហ៍ទី៤', $dateTime->fullWeek());
24 | $this->assertEquals('២១', $dateTime->weekOfYear());
25 | $this->assertEquals('សប្តាហ៍ទី២១', $dateTime->fullWeekOfYear());
26 | $this->assertEquals('២', $dateTime->quarter());
27 | $this->assertEquals('ត្រីមាសទី២', $dateTime->fullQuarter());
28 | }
29 |
30 | public function test_khmer_date_time_parsing_format_with_time()
31 | {
32 | $dateTime = KhmerDateTime::parse('2020-09-20 12:40');
33 | $this->assertEquals("២០/០៩/២០២០", $dateTime->format("L"));
34 | $this->assertEquals("២០ កញ្ញា ២០២០", $dateTime->format("LL"));
35 | $this->assertEquals("២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLT"));
36 | $this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០", $dateTime->format("LLL"));
37 | $this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLLT"));
38 | $this->assertEquals("ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០", $dateTime->format("LLLL"));
39 | $this->assertEquals("ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLLLT"));
40 | }
41 |
42 | public function test_throw_exception_when_parsing_incorrect_date_time_format()
43 | {
44 | $this->expectException(\Exception::class);
45 |
46 | KhmerDateTime::parse('2019\01\22');
47 | }
48 |
49 | public function test_throw_exception_when_parsing_incorrect_format()
50 | {
51 | $this->expectException(\Exception::class);
52 |
53 | KhmerDateTime::parse('2020-09-20 12:40')->format("other");
54 | }
55 |
56 | public function test_date_time_from_now_in_year()
57 | {
58 | $now = new DateTime("2020-09-20");
59 | $dateTime = KhmerDateTime::parse('2012-10-20');
60 |
61 | $this->assertEquals("៧ ឆ្នាំមុន", $dateTime->durationFrom($now, true));
62 | }
63 | public function test_date_time_from_now_in_month()
64 | {
65 | $now = new DateTime("2020-09-20");
66 | $dateTime = KhmerDateTime::parse('2020-03-20');
67 |
68 | $this->assertEquals("៦ ខែមុន", $dateTime->durationFrom($now, true));
69 | }
70 |
71 | public function test_date_time_from_now_in_day()
72 | {
73 | $now = new DateTime("2020-09-20");
74 | $dateTime = KhmerDateTime::parse('2020-09-15');
75 |
76 | $this->assertEquals("៥ ថ្ងៃមុន", $dateTime->durationFrom($now, true));
77 | }
78 |
79 | public function test_date_time_from_now_in_hour()
80 | {
81 | $now = new DateTime("2020-09-15 06:00");
82 | $dateTime = KhmerDateTime::parse('2020-09-15 02:00');
83 |
84 | $this->assertEquals("៤ ម៉ោងមុន", $dateTime->durationFrom($now, true));
85 | }
86 |
87 | public function test_date_time_from_now_in_minute()
88 | {
89 | $now = new DateTime("2020-09-15 06:03");
90 | $dateTime = KhmerDateTime::parse('2020-09-15 06:00');
91 |
92 | $this->assertEquals("៣ នាទីមុន", $dateTime->durationFrom($now, true));
93 | }
94 |
95 | public function test_date_time_from_now_in_minute_for_future()
96 | {
97 | $now = new DateTime("2020-09-15");
98 | $dateTime = KhmerDateTime::parse('2021-09-15');
99 |
100 | $this->assertEquals("១ ឆ្នាំទៀត", $dateTime->durationFrom($now, true));
101 | }
102 |
103 | public function test_date_time_from_now_without_space()
104 | {
105 | $now = new DateTime("2020-09-20");
106 | $dateTime = KhmerDateTime::parse('2012-10-20');
107 |
108 | $this->assertEquals("៧ឆ្នាំមុន", $dateTime->durationFrom($now, false));
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # The PHP libray for convert datetime into Khmer
2 |
3 | [](https://github.com/phannaly/php-datetime-khmer/actions)
4 | [](https://opensource.org/licenses/MIT)
5 |
6 | This is a small package for converting datetime to Khmer language.
7 |
8 | ## Requirements
9 |
10 | * PHP 7.0 or higher
11 |
12 | ## Setup
13 |
14 | You don't need to install by composer if your project doesn't have it.
15 |
16 | Just import it manually in `src` folder.
17 |
18 | But If you want to install by a composer, please follow the command below
19 |
20 | composer require phannaly/php-datetime-khmer
21 |
22 |
23 |
24 | ## Usage
25 |
26 | Whenever you want to convert any DateTime into Khmer language, just wrap it inside method.
27 |
28 | Firstly, you can import or instance class
29 | ```php
30 | use KhmerDateTime\KhmerDateTime;
31 | ```
32 |
33 | You have to parse a valid string DateTime format and without a specific time, it will set time to 00:00
34 |
35 | ```php
36 | $dateTime = KhmerDateTime::parse('2019-05-22');
37 |
38 | $dateTime->day(); // ២២
39 | $dateTime->fullDay(); // ពុធ
40 | $dateTime->month(); // ០៥
41 | $dateTime->fullMonth(); // ឧសភា
42 | $dateTime->year(); // ២០១៩
43 | $dateTime->minute(); // ០០
44 | $dateTime->hour(); // ០០
45 | $dateTime->meridiem(); // ព្រឹក
46 | $dateTime->week(); // ៤
47 | $dateTime->fullWeek(); // សប្តាហ៍ទី៤
48 | $dateTime->weekOfYear(); // ២១
49 | $dateTime->fullWeekOfYear(); // សប្តាហ៍ទី២១
50 | $dateTime->quarter(); // ២
51 | $dateTime->fullQuarter(); // ត្រីមាសទី២
52 | ```
53 |
54 | For example:
55 | ```php
56 | $dateTime = KhmerDateTime::parse('2020-09-20 12:40');
57 | ```
58 |
59 | will producing result below
60 |
61 | | Code | Format | Output |
62 | | -------------------------- |:---------:| -----:|
63 | | `$dateTime->format("L")` | `L` | `២០/០៩/២០២០` |
64 | | `$dateTime->format("LL")` | `LL` | `២០ កញ្ញា ២០២០` |
65 | | `$dateTime->format("LLT")` | `LLT` | `២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច` |
66 | | `$dateTime->format("LLL")` | `LLL` | `អាទិត្យ ២០ កញ្ញា ២០២០` |
67 | | `$dateTime->format("LLLT")` | `LLLT` | `អាទិត្យ ២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច` |
68 | | `$dateTime->format("LLLLT")` | `LLLL` | `ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០` |
69 | | `$dateTime->format("LLLLT")` | `LLLLT` | `ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០ ១២:៤០ ល្ងាច` |
70 |
71 | If you wanna use DateTime duration in Khmer, you can use `fromNow()` method that checks the date that you parse compare with your current timestamp.
72 | The default `fromNow` method will add space between duration, if you want to remove space just add `fromNow(false)`
73 | Ex:
74 | ```php
75 | KhmerDateTime::parse('2012-10-20')->fromNow() // ៧ ឆ្នាំមុន
76 | KhmerDateTime::parse('2012-10-20')->fromNow(false) // ៧ឆ្នាំមុន
77 | ```
78 |
79 | Below is the example how `fromNow()` method works
80 |
81 | | Code | Current timestamp | Output |
82 | | -------------------------------------------------------|:--------------------:| -------:|
83 | | `KhmerDateTime::parse('2012-10-20')->fromNow()` | `2020-09-20` | `៧ ឆ្នាំមុន` |
84 | | `KhmerDateTime::parse('2020-03-20')->fromNow()` | `2020-09-20` | `៦ ខែមុន` |
85 | | `KhmerDateTime::parse('2020-09-15')->fromNow()` | `2020-09-20` | `៥ ថ្ងៃមុន` |
86 | | `KhmerDateTime::parse('2020-09-15 02:00')->fromNow()` | `2020-09-15 06:00` | `៤ ម៉ោងមុន` |
87 | | `KhmerDateTime::parse('2020-09-15 06:00')->fromNow()` | `2020-09-15 06:03` | `៣ នាទីមុន` |
88 |
89 | If you parse the timestamp in the future
90 |
91 | | Code | Current timestamp | Output |
92 | | -------------------------------------------------------|:--------------------:| -------:|
93 | | `KhmerDateTime::parse('2027-10-20')->fromNow()` | `2020-09-20` | `៧ ឆ្នាំទៀត` |
94 | | `KhmerDateTime::parse('2021-03-20')->fromNow()` | `2020-09-20` | `៦ ខែទៀត` |
95 | | `KhmerDateTime::parse('2020-09-25')->fromNow()` | `2020-09-20` | `៥ ថ្ងៃទៀត` |
96 | | `KhmerDateTime::parse('2020-09-15 10:00')->fromNow()` | `2020-09-15 06:00` | `៤ ម៉ោងទៀត` |
97 | | `KhmerDateTime::parse('2020-09-15 06:03')->fromNow()` | `2020-09-15 06:00` | `៣ នាទីទៀត` |
98 |
99 | Using the current timestamp without specific date and time
100 |
101 | ```php
102 | $dateTime = KhmerDateTime::now();
103 | // or
104 | $dateTime = new KhmerDateTime();
105 | ````
106 |
107 | ## Contributing
108 |
109 | Feel free to contribute through PR.
110 |
111 | ## License
112 |
113 | This package operates under the MIT License (MIT). See the [LICENSE](https://github.com/phannaly/php-datetime-khmer/blob/master/LICENSE.md) file for details.
114 |
--------------------------------------------------------------------------------
/src/KhmerDateTime.php:
--------------------------------------------------------------------------------
1 | config = new Config();
26 | $this->dateTime = strtotime(date("Y-m-d H:i"));
27 | return $this;
28 | }
29 |
30 | /**
31 | * Parse the datetime in String format
32 | *
33 | * @param $dateTime
34 | * @return static
35 | * @throws Exception
36 | */
37 | public static function parse($dateTime)
38 | {
39 | $instance = new static;
40 | $instance->dateTime = strtotime($dateTime);
41 |
42 | if (! $instance->dateTime) {
43 | throw new Exception('Undefined date format');
44 | }
45 |
46 | return $instance;
47 | }
48 |
49 | /**
50 | * Use current timestamp
51 | *
52 | * @return KhmerDateTime
53 | */
54 | public static function now()
55 | {
56 | return new static();
57 | }
58 |
59 | /**
60 | * Get month in Khmer.
61 | *
62 | * @return string
63 | */
64 | public function month()
65 | {
66 | return $this->config->numbers(date('m', $this->dateTime));
67 | }
68 |
69 | /**
70 | * Get full month name in Khmer.
71 | *
72 | * @return string
73 | */
74 | public function fullMonth()
75 | {
76 | return $this->config->months(date('n', $this->dateTime));
77 | }
78 |
79 | /**
80 | * Get day in Khmer.
81 | *
82 | * @return string
83 | */
84 | public function day()
85 | {
86 | return $this->config->numbers(date('d', $this->dateTime));
87 | }
88 |
89 | /**
90 | * Get full day name in Khmer
91 | *
92 | * @return string
93 | */
94 | public function fullDay()
95 | {
96 | return $this->config->days(date('w', $this->dateTime));
97 | }
98 |
99 | /**
100 | * Get full year 4 number in Khmer.
101 | *
102 | * @return string
103 | */
104 | public function year()
105 | {
106 | return $this->config->numbers(date('Y', $this->dateTime));
107 | }
108 |
109 | /**
110 | * Get hour in Khmer
111 | *
112 | * @return string
113 | */
114 | public function hour()
115 | {
116 | $hour = date('H', $this->dateTime);
117 | return $this->config->numbers($hour);
118 | }
119 |
120 | /**
121 | * Get minute in Khmer
122 | *
123 | * @return string
124 | */
125 | public function minute()
126 | {
127 | return $this->config->numbers(date('i', $this->dateTime));
128 | }
129 |
130 | /**
131 | * Get quarter in Khmer
132 | *
133 | * @return string
134 | */
135 | public function quarter()
136 | {
137 | $month = date('m', $this->dateTime);
138 | return $this->config->numbers(ceil($month / 3));
139 | }
140 |
141 | /**
142 | * Get quarter in Khmer
143 | *
144 | * @return string
145 | */
146 | public function fullQuarter()
147 | {
148 | return "ត្រីមាសទី".$this->quarter();
149 | }
150 |
151 | /**
152 | * Get time meridiem
153 | *
154 | * @return string
155 | */
156 | public function meridiem()
157 | {
158 | return $this->config->meridiem[date('a', $this->dateTime)];
159 | }
160 |
161 | /**
162 | * Get week of the month in Khmer
163 | *
164 | * @return string
165 | */
166 | public function week()
167 | {
168 | // https://stackoverflow.com/a/32624747/4345720
169 | $firstOfMonth = strtotime(date("Y-m-01", $this->dateTime));
170 | $weekOfMonth = (date("W", $this->dateTime) - date("W", $firstOfMonth)) + 1;
171 |
172 | return $this->config->numbers($weekOfMonth);
173 | }
174 |
175 | /**
176 | * Get full week of month in Khmer
177 | *
178 | * @return string
179 | */
180 | public function fullWeek()
181 | {
182 | return "សប្តាហ៍ទី".$this->week();
183 | }
184 |
185 | /**
186 | * Get week of year in Khmer
187 | *
188 | * @return string
189 | */
190 | public function weekOfYear()
191 | {
192 | return $this->config->numbers((int) date("W", $this->dateTime));
193 | }
194 |
195 | /**
196 | * Get full week of year in Khmer
197 | * @return string
198 | */
199 | public function fullWeekOfYear()
200 | {
201 | return "សប្តាហ៍ទី".$this->weekOfYear();
202 | }
203 |
204 | /**
205 | * Return dateTime base on format
206 | *
207 | * @param $format
208 | * @return mixed
209 | * @throws Exception
210 | */
211 | public function format($format)
212 | {
213 | try {
214 | return $this->dateTimeFormat($format);
215 | } catch (Exception $e) {
216 | throw new Exception("Invalid format");
217 | }
218 | }
219 |
220 | /**
221 | * Return from now method
222 | *
223 | * @return string
224 | */
225 | public function fromNow($space = true)
226 | {
227 | return $this->durationFrom(new DateTime(), $space);
228 | }
229 |
230 | /**
231 | * Get duration from now template
232 | *
233 | * @param mixed $now
234 | * @return string
235 | */
236 | public function durationFrom($now, $space)
237 | {
238 | $interval = $now->diff($this->toDateTimeFormat());
239 | $suffix = $now > $this->toDateTimeFormat() ? 'មុន' : 'ទៀត';
240 | $durationSpace = $space ? ' ' : '';
241 |
242 | // http://php.kambing.ui.ac.id/manual/en/datetime.diff.php#97880
243 |
244 | if ($interval->y >= 1) {
245 | return $this->config->numbers($interval->y).$durationSpace.'ឆ្នាំ'.$suffix;
246 | }
247 | if ($interval->m >= 1) {
248 | return $this->config->numbers($interval->m).$durationSpace.'ខែ'.$suffix;
249 | }
250 | if ($interval->d >= 1) {
251 | return $this->config->numbers($interval->d).$durationSpace.'ថ្ងៃ'.$suffix;
252 | }
253 | if ($interval->h >= 1) {
254 | return $this->config->numbers($interval->h).$durationSpace.'ម៉ោង'.$suffix;
255 | }
256 | if ($interval->i >= 1) {
257 | return $this->config->numbers($interval->i).$durationSpace.'នាទី'.$suffix;
258 | }
259 |
260 | return $this->config->numbers($interval->s).$durationSpace.'វិនាទី'.$suffix;
261 | }
262 |
263 | /**
264 | * Get build-in DateTime format
265 | *
266 | * @return object
267 | */
268 | private function toDateTimeFormat()
269 | {
270 | return new DateTime(date("Y-m-d H:i", $this->dateTime));
271 | }
272 | }
273 |
--------------------------------------------------------------------------------