Skip to content

Commit c60d9b4

Browse files
committed
update readme to new version
1 parent 7d76628 commit c60d9b4

File tree

4 files changed

+97
-56
lines changed

4 files changed

+97
-56
lines changed

readme.md

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,42 +27,48 @@ Whenever you want to convert any datetime into Khmer language, just wrap it insi
2727

2828
Firstly, you can import or instance class
2929
```php
30-
// Using import namespace
3130
use KhmerDateTime\KhmerDateTime;
3231
```
3332

34-
```php
35-
//This library accepts two date formats when parsing.
36-
37-
$date = '2019-01-22'; // Y-m-d
38-
// and
39-
$date = '2019/01/22'; // Y/m/d
33+
You have to parse a valid string datetime format and without specific time, it will set time to 00:00
4034

41-
$dateTime = KhmerDateTime::parse($date); // or specific date that you want
35+
```php
36+
$dateTime = KhmerDateTime::parse('2019-01-22');
4237

4338
$dateTime->day(); // ២២
4439
$dateTime->fullDay(); // អង្គារ
4540
$dateTime->month(); // ០១
4641
$dateTime->fullMonth(); // មករា
4742
$dateTime->year(); // ២០១៩
43+
$dateTime->minute(); // ០០
44+
$dateTime->hour(); // ០០
45+
$dateTime->meridiem(); // ព្រឹក
4846
```
4947

50-
You can access date with following format
51-
48+
For example:
5249
```php
53-
$dateTime = KhmerDateTime::parse('2019-01-22');
54-
55-
$dateTime->date(); // អង្គារ ២២ មករា ២០១៩
56-
$dateTime->date("short"); // ២២ មករា ២០១៩
57-
$dateTime->date("dash"); // ២២-០១-២០១៩
58-
$dateTime->date("forward"); // ២២/០១/២០១៩
59-
$dateTime->date("long"); // ថ្ងៃអង្គារ ទី២២ ខែមករា ឆ្នាំ២០១៩
50+
$dateTime = KhmerDateTime::parse('2020-09-20 12:40');
6051
```
6152

62-
Using the current timestamp without specific date
53+
will producing result below
54+
55+
| Code | Format | Output |
56+
| -------------------------- |:---------:| -----:|
57+
| `$dateTime->format("L")` | `L` | `២០/០៩/២០២០` |
58+
| `$dateTime->format("LL")` | `LL` | `២០ កញ្ញា ២០២០` |
59+
| `$dateTime->format("LLT")` | `LLT` | `២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច` |
60+
| `$dateTime->format("LLL")` | `LLL` | `អាទិត្យ ២០ កញ្ញា ២០២០` |
61+
| `$dateTime->format("LLLT")` | `LLLT` | `អាទិត្យ ២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច` |
62+
| `$dateTime->format("LLLLT")` | `LLLL` | `ថ្ងៃទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០` |
63+
| `$dateTime->format("LLLLT")` | `LLLLT` | `ថ្ងៃអាទិត្យ ទី២០ ខែកញ្ញា ឆ្នាំ២០២០ ១២:៤០ ល្ងាច` |
64+
65+
66+
Using the current timestamp without specific date and time
6367

6468
```php
6569
$dateTime = KhmerDateTime::now();
70+
// or
71+
$dateTime = new KhmerDateTime();
6672
````
6773

6874
## Contributing

src/Format.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
4+
namespace KhmerDateTime;
5+
6+
7+
trait Format
8+
{
9+
/**
10+
* Return dateTime format
11+
*
12+
* @param $format
13+
* @return mixed
14+
*/
15+
public function dateTimeFormat($format) {
16+
$formats = [
17+
'L' => $this->formatL(),
18+
'LL' => $this->formatLL(),
19+
'LLT' => $this->formatLL()." ".$this->formatT(),
20+
'LLL' => $this->formatLLL(),
21+
'LLLT' => $this->formatLLL()." ".$this->formatT(),
22+
'LLLL' => $this->formatLLLL(),
23+
'LLLLT' => $this->formatLLLL()." ".$this->formatT()
24+
];
25+
26+
return $formats[$format];
27+
}
28+
29+
private function formatL() {
30+
return $this->day()."/".$this->month()."/".$this->year();
31+
}
32+
33+
private function formatLL() {
34+
return $this->day()." ".$this->fullMonth()." ".$this->year();
35+
}
36+
37+
private function formatT() {
38+
return $this->hour().":".$this->minute()." ".$this->meridiem();
39+
}
40+
41+
private function formatLLL() {
42+
return $this->fullDay()." ".$this->day()." ".$this->fullMonth()." ".$this->year();
43+
}
44+
45+
private function formatLLLL() {
46+
return "ថ្ងៃ".$this->fullDay()." ទី".$this->day()." ខែ".$this->fullMonth()." ឆ្នាំ".$this->year();
47+
}
48+
}

src/KhmerDateTime.php

Lines changed: 25 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,42 @@
22

33
namespace KhmerDateTime;
44

5-
use DateTime;
65
use Exception;
76

87
date_default_timezone_set("Asia/Phnom_Penh");
98

109
class 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
}

tests/KhmerDateTimeTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ public function test_khmer_date_time_parsing_without_time()
2424
public function test_khmer_date_time_parsing_format_with_time()
2525
{
2626
$dateTime = KhmerDateTime::parse('2020-09-20 12:40');
27-
$this->assertEquals("អាទិត្យ ២០ កញ្ញា ២០២០", $dateTime->date());
2827
$this->assertEquals("២០/០៩/២០២០", $dateTime->format("L"));
2928
$this->assertEquals("២០ កញ្ញា ២០២០", $dateTime->format("LL"));
3029
$this->assertEquals("២០ កញ្ញា ២០២០ ១២:៤០ ល្ងាច", $dateTime->format("LLT"));

0 commit comments

Comments
 (0)