Skip to content

Commit 1f8425b

Browse files
Add time ago functionality and update sub/add second, minute and hour
1 parent 3848cbe commit 1f8425b

File tree

8 files changed

+272
-7
lines changed

8 files changed

+272
-7
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
vendor/**
2+
test.php

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ As datium output
3939
```js
4040
Datium::now()->get(); // ex: 2016-01-01 00:00:00
4141

42-
Datium::now()->timestamp(); ex: 1420057800
42+
Datium::now()->timestamp(); // ex: 1420057800
4343
```
4444
Or working with date as simple as you need:
4545

@@ -124,6 +124,57 @@ Datium::now()->sub('1 year')
124124
// output => 2014-09-29 00:00:00
125125
```
126126

127+
## Date Difference
128+
This method will return the difference between two specific date with php date interval type.
129+
130+
```
131+
// current generated date difference with next 5000 days
132+
$diff = Datium::diff(
133+
Datium::now()->object(),
134+
Datium::now()->add('5000 day')->object()
135+
);
136+
137+
echo $diff->days;
138+
139+
// output => 5000
140+
141+
dd ( $diff );
142+
143+
/**
144+
output => object(DateInterval)#3 (15) {
145+
["y"]=>
146+
int(13)
147+
["m"]=>
148+
int(8)
149+
["d"]=>
150+
int(7)
151+
["h"]=>
152+
int(0)
153+
["i"]=>
154+
int(0)
155+
["s"]=>
156+
int(0)
157+
["weekday"]=>
158+
int(0)
159+
["weekday_behavior"]=>
160+
int(0)
161+
["first_last_day_of"]=>
162+
int(0)
163+
["invert"]=>
164+
int(0)
165+
["days"]=>
166+
int(5000)
167+
["special_type"]=>
168+
int(0)
169+
["special_amount"]=>
170+
int(0)
171+
["have_weekday_relative"]=>
172+
int(0)
173+
["have_special_relative"]=>
174+
int(0)
175+
} */
176+
```
177+
127178
## Leap year
128179
Define leap year of current year with generalization support.
129180

src/Config.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
'default_calendar' => 'gregorian',
3636

3737

38-
'date_interval' => array( 'D', 'M', 'Y', 'H', 'm', 'S' ),
38+
'date_interval' => array( 'D', 'M', 'Y', 'HT', 'MT', 'ST' ),
3939

4040

4141
'date_simple' => array( 'day', ' month', ' year', ' hour', ' minute', ' second' ),

src/Datium.php

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use OpenCafe\Tools\Leap;
1919
use OpenCafe\Tools\DayOf;
2020
use OpenCafe\Tools\Lang;
21+
use OpenCafe\Tools\TimeAgo;
2122

2223
use OpenCafe\Datium;
2324

@@ -91,6 +92,13 @@ class Datium
9192

9293
protected $language;
9394

95+
/**
96+
* Timeago
97+
*
98+
* @param integer
99+
*/
100+
protected $ago;
101+
94102
/**
95103
* Datium class constructure
96104
*/
@@ -307,7 +315,6 @@ public function to($calendar)
307315

308316
}
309317

310-
311318
/**
312319
* Difference between two time
313320
*
@@ -339,10 +346,24 @@ public function add($value)
339346
$value
340347
);
341348

349+
$unit = 'P';
350+
351+
if( strpos($this->date_interval_expression, 'T') ) {
352+
353+
$this->date_interval_expression= str_replace(
354+
'T',
355+
'',
356+
$this->date_interval_expression
357+
);
358+
359+
$unit = 'PT';
360+
361+
}
362+
342363
$this->date_interval_expression = str_replace(
343364
' ',
344365
'',
345-
'P' . $this->date_interval_expression
366+
$unit . $this->date_interval_expression
346367
);
347368

348369
$this->date_time->add(
@@ -369,10 +390,24 @@ public function sub($value)
369390
$value
370391
);
371392

393+
$unit = 'P';
394+
395+
if( strpos($this->date_interval_expression, 'T') ) {
396+
397+
$this->date_interval_expression= str_replace(
398+
'T',
399+
'',
400+
$this->date_interval_expression
401+
);
402+
403+
$unit = 'PT';
404+
405+
}
406+
372407
$this->date_interval_expression = str_replace(
373408
' ',
374409
'',
375-
'P' . $this->date_interval_expression
410+
$unit . $this->date_interval_expression
376411
);
377412

378413
$this->date_time->sub(
@@ -399,6 +434,20 @@ public function leap()
399434

400435
}
401436

437+
/**
438+
* Calculate how many time ago datetime happens
439+
*
440+
* @return string
441+
*/
442+
public function ago()
443+
{
444+
445+
$this->ago = new TimeAgo( $this->date_time, $this->language );
446+
447+
return $this->ago->get();
448+
449+
}
450+
402451
/**
403452
* Caculate day of year or week
404453
*

src/DayOf.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
<?php namespace OpenCafe\Tools;
1+
<?php
2+
3+
namespace OpenCafe\Tools;
24

35
/**
46
* Define Day of current date.

src/Lang.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,12 @@ public static function getConfig()
6767
return self::$config;
6868

6969
}
70+
71+
public static function get( $lang, $value ) {
72+
73+
$file = include( 'lang/' . $lang . '/general.php' );
74+
75+
return $file[$value];
76+
77+
}
7078
}

src/lang/en/general.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,19 @@
3535
'PM' => 'PM',
3636
'Jumada I' => 'Jumada I',
3737
'Shanbe' => 'Shanbe',
38-
'Tir' => 'Tir'
38+
'Tir' => 'Tir',
39+
'ago' => 'ago',
40+
'just-now' => 'just now',
41+
'one-minute' => 'one minute',
42+
'minutes' => 'minutes',
43+
'an-hour' => 'an hour',
44+
'hours' => 'hours',
45+
'yesterday' => 'yesterday',
46+
'days' => 'days',
47+
'a-week' => 'a week',
48+
'weeks' => 'weeks',
49+
'a-month' => 'a month',
50+
'months' => 'months',
51+
'one-year' => 'one year',
52+
'years' => 'years'
3953
];

tests/DatiumTest.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,146 @@ public function testCreateDateTime()
2828

2929
}
3030

31+
public function testAddDateTime()
32+
{
33+
34+
$this->assertEquals(
35+
'2016-01-01 00:00:00',
36+
Datium::create(2016, 1, 1, 0, 0, 0)->get()
37+
);
38+
39+
$this->assertEquals(
40+
'2016-01-01 01:00:00',
41+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 hour')->get()
42+
);
43+
44+
$this->assertEquals(
45+
'2016-01-02 01:00:00',
46+
Datium::create(2016, 1, 1, 0, 0, 0)->add('25 hour')->get()
47+
);
48+
49+
$this->assertEquals(
50+
'2016-01-01 00:01:00',
51+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 minute')->get()
52+
);
53+
54+
$this->assertEquals(
55+
'2016-01-01 01:01:00',
56+
Datium::create(2016, 1, 1, 0, 0, 0)->add('61 minute')->get()
57+
);
58+
59+
$this->assertEquals(
60+
'2016-01-01 00:00:01',
61+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 second')->get()
62+
);
63+
64+
$this->assertEquals(
65+
'2016-01-01 00:01:01',
66+
Datium::create(2016, 1, 1, 0, 0, 0)->add('61 second')->get()
67+
);
68+
69+
$this->assertEquals(
70+
'2016-01-02 00:00:00',
71+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 day')->get()
72+
);
73+
74+
$this->assertEquals(
75+
'2016-01-11 00:00:00',
76+
Datium::create(2016, 1, 1, 0, 0, 0)->add('10 day')->get()
77+
);
78+
79+
$this->assertEquals(
80+
'2016-02-01 00:00:00',
81+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 month')->get()
82+
);
83+
84+
$this->assertEquals(
85+
'2016-11-01 00:00:00',
86+
Datium::create(2016, 1, 1, 0, 0, 0)->add('10 month')->get()
87+
);
88+
89+
$this->assertEquals(
90+
'2017-01-01 00:00:00',
91+
Datium::create(2016, 1, 1, 0, 0, 0)->add('1 year')->get()
92+
);
93+
94+
$this->assertEquals(
95+
'2116-01-01 00:00:00',
96+
Datium::create(2016, 1, 1, 0, 0, 0)->add('100 year')->get()
97+
);
98+
99+
}
100+
101+
public function testSubDateTime()
102+
{
103+
104+
$this->assertEquals(
105+
'2016-01-01 00:00:00',
106+
Datium::create(2016, 1, 1, 1, 0, 0)->sub('1 hour')->get()
107+
);
108+
109+
$this->assertEquals(
110+
'2016-01-01 00:00:00',
111+
Datium::create(2016, 1, 2, 1, 0, 0)->sub('25 hour')->get()
112+
);
113+
114+
$this->assertEquals(
115+
'2016-01-01 00:00:00',
116+
Datium::create(2016, 1, 1, 0, 1, 0)->sub('1 minute')->get()
117+
);
118+
119+
$this->assertEquals(
120+
'2016-01-01 00:00:00',
121+
Datium::create(2016, 1, 1, 1, 1, 0)->sub('61 minute')->get()
122+
);
123+
124+
$this->assertEquals(
125+
'2016-01-01 00:00:00',
126+
Datium::create(2016, 1, 1, 0, 0, 1)->sub('1 second')->get()
127+
);
128+
129+
$this->assertEquals(
130+
'2016-01-01 00:00:00',
131+
Datium::create(2016, 1, 1, 0, 1, 1)->sub('61 second')->get()
132+
);
133+
134+
$this->assertEquals(
135+
'2016-01-01 00:00:00',
136+
Datium::create(2016, 1, 1, 0, 0, 0)->get()
137+
);
138+
139+
$this->assertEquals(
140+
'2016-01-01 00:00:00',
141+
Datium::create(2016, 1, 2, 0, 0, 0)->sub('1 day')->get()
142+
);
143+
144+
$this->assertEquals(
145+
'2016-01-01 00:00:00',
146+
Datium::create(2016, 1, 11, 0, 0, 0)->sub('10 day')->get()
147+
);
148+
149+
$this->assertEquals(
150+
'2016-01-01 00:00:00',
151+
Datium::create(2016, 2, 1, 0, 0, 0)->sub('1 month')->get()
152+
);
153+
154+
$this->assertEquals(
155+
'2016-01-01 00:00:00',
156+
Datium::create(2016, 11, 1, 0, 0, 0)->sub('10 month')->get()
157+
);
158+
159+
$this->assertEquals(
160+
'2016-01-01 00:00:00',
161+
Datium::create(2017, 1, 1, 0, 0, 0)->sub('1 year')->get()
162+
);
163+
164+
$this->assertEquals(
165+
'2016-01-01 00:00:00',
166+
Datium::create(2116, 1, 1, 0, 0, 0)->sub('100 year')->get()
167+
);
168+
169+
}
170+
31171
public function testDateDifference()
32172
{
33173

0 commit comments

Comments
 (0)