Skip to content

Commit 3ff6c3e

Browse files
author
Jimmi Westerberg
committed
Merge branch 'master' of github.com:jimmiw/php-time-ago
2 parents 8f7f6fe + 2fd774a commit 3ff6c3e

File tree

7 files changed

+84
-28
lines changed

7 files changed

+84
-28
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
composer.lock
12
vendor
23
report

Changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### 3.1.0
44
* Changed from psr-0 to psr-4, since some language classes could not be loaded properly
5+
* Added support for DateTimeImmutable objects, thanks to @xtreamwayz
6+
* Added Indonesian translation, thanks to @dhutapratama
57

68
### 3.0.6
79
* Fixed #79 where 1h30m would be 1 hours ago instead of 2 hours ago

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ The translation is simply a language code string added to the end of the class i
117117
Examples using the Danish translations:
118118

119119
```
120-
$timeZone = null; // just use the system timezone
120+
$myLang = new \Westsworld\TimeAgo\Translations\Da();
121121
122-
$timeAgo = new Westsworld\TimeAgo($timeZone, 'da'); // default language is en (english)
122+
$timeAgo = new Westsworld\TimeAgo($myLang); // default language is en (english)
123123
echo $timeAgo->inWords("2010/1/10 23:05:00");
124124
```
125125

126+
# Available translation languages
127+
128+
You can view the file of available list inside /src/Westsworld/TimeAgo/Translations/ folder.
129+
126130
# Changelog
127131

128132
For a full list of changes, please see Changelog.md

src/Westsworld/TimeAgo.php

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
namespace Westsworld;
44

55
// just making life easier :)
6-
use Exception;
76
use DateTime;
7+
use DateTimeInterface;
8+
use Exception;
89
// importing the language class
910
use Westsworld\TimeAgo\Language;
1011
use Westsworld\TimeAgo\Translations\En;
@@ -65,11 +66,11 @@ protected function getLanguage(): Language
6566

6667
/**
6768
* Fetches the different between $past and $now in a spoken format.
68-
* @param DateTime $past the past date to use
69-
* @param DateTime $now the current time, defaults to now, using timezone from $past
69+
* @param DateTimeInterface $past the past date to use
70+
* @param DateTimeInterface $now the current time, defaults to now, using timezone from $past
7071
* @return string the time difference in a spoken format, e.g. 1 day ago
7172
*/
72-
public function inWords(DateTime $past, DateTime $now = null)
73+
public function inWords(DateTimeInterface $past, DateTimeInterface $now = null)
7374
{
7475
// ensuring that "now" is a DateTime object, using the past's timeZone
7576
// if needed, to create a new now object.
@@ -95,12 +96,12 @@ public function inWordsFromStrings(string $past, string $now = 'now')
9596
/**
9697
* Fetches the date difference between the two given dates.
9798
*
98-
* @param DateTime $past the "past" time to parse
99-
* @param DateTime $now the "now" time to parse
99+
* @param DateTimeInterface $past the "past" time to parse
100+
* @param DateTimeInterface $now the "now" time to parse
100101
* @return array the difference in dates, using the two dates
101102
* @deprecated 3.0.0 this method is not really needed anymore, since DateTime can do it
102103
*/
103-
public function dateDifference(DateTime $past, DateTime $now = null)
104+
public function dateDifference(DateTimeInterface $past, DateTimeInterface $now = null)
104105
{
105106
$now = $this->getNow($past, $now);
106107

@@ -119,11 +120,11 @@ public function dateDifference(DateTime $past, DateTime $now = null)
119120
/**
120121
* Fetches the given $now variable, but initializes it if it's null
121122
*
122-
* @param DateTime $past the past tiem
123-
* @param DateTime $now the now to use or initialize
124-
* @return DateTime $now initialized, if it was not, else the original object
123+
* @param DateTimeInterface $past the past tiem
124+
* @param DateTimeInterface $now the now to use or initialize
125+
* @return DateTimeInterface $now initialized, if it was not, else the original object
125126
*/
126-
public function getNow(DateTime $past, DateTime $now = null): DateTime
127+
public function getNow(DateTimeInterface $past, DateTimeInterface $now = null): DateTimeInterface
127128
{
128129
// handles cases where $now is null
129130
if (null === $now) {

src/Westsworld/TimeAgo/Language.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
namespace Westsworld\TimeAgo;
44

5-
use \DateTime;
6-
use \DateInterval;
5+
use DateInterval;
6+
use DateTime;
7+
use DateTimeInterface;
78

89
abstract class Language
910
{
@@ -42,13 +43,13 @@ public function hasTranslation($key): bool
4243

4344
/**
4445
* Fetches the different between $past and $now in a spoken format.
45-
* NOTE: both past and now should be instances of DateTime
46+
* NOTE: both past and now should be instances of DateTimeInterface
4647
*
47-
* @param DateTime $past the past date to use
48-
* @param DateTime $now the current timezone, defaults to now.
48+
* @param DateTimeInterface $past the past date to use
49+
* @param DateTimeInterface $now the current timezone, defaults to now.
4950
* @return string the difference in spoken format, e.g. 1 day ago
5051
*/
51-
public function inWords(DateTime $past, DateTime $now)
52+
public function inWords(DateTimeInterface $past, DateTimeInterface $now)
5253
{
5354
// finds the time difference as a string
5455
return $this->getTimeDifference($past->diff($now));
@@ -141,7 +142,7 @@ private function getTimeDifference(DateInterval $timeDifference)
141142
/**
142143
* Checks if the given past is empty
143144
*
144-
* @param DateTime $past the "past" to check
145+
* @param DateTimeInterface $past the "past" to check
145146
* @return bool true if empty, else false
146147
*/
147148
// private function isPastEmpty($past)
@@ -251,7 +252,7 @@ private function isLessThan23Hours59Mins29Seconds(DateInterval $timeDifference)
251252
) {
252253
return $timeDifference->s < 30;
253254
}
254-
255+
255256
return ! $this->isLessThan1Hour29Mins59Seconds($timeDifference);
256257
}
257258

@@ -300,7 +301,7 @@ private function isLessThan29Days23Hours59Mins29Seconds(DateInterval $timeDiffer
300301
) {
301302
return $timeDifference->s <= 29;
302303
}
303-
304+
304305
return ! $this->isLessThan23Hours59Mins29Seconds($timeDifference);
305306
}
306307

@@ -324,7 +325,7 @@ private function isLessThan59Days23Hours59Mins29Secs(DateInterval $timeDifferenc
324325
) {
325326
return $timeDifference->s <= 29;
326327
}
327-
328+
328329
return ! $this->isLessThan29Days23Hours59Mins29Seconds($timeDifference);
329330
}
330331

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Westsworld\TimeAgo\Translations;
4+
5+
use \Westsworld\TimeAgo\Language;
6+
7+
/**
8+
* Indonesian translations
9+
*/
10+
class Id extends Language
11+
{
12+
public function __construct()
13+
{
14+
$this->setTranslations([
15+
'aboutOneDay' => "1 hari",
16+
'aboutOneHour' => "1 jam",
17+
'aboutOneMonth' => "1 bulan",
18+
'aboutOneYear' => "1 tahun",
19+
'days' => "%s hari",
20+
'hours' => "%s jam",
21+
'lessThanAMinute' => "Baru saja",
22+
'lessThanOneHour' => "%s menit",
23+
'months' => "%s bulan",
24+
'oneMinute' => "1 menit",
25+
'years' => "%s tahun",
26+
'never' => 'Tidak pernah'
27+
]);
28+
}
29+
}

tests/TimeagoTest.php

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace Westsworld\TimeAgo\Tests;
44

5-
use Westsworld\TimeAgo;
6-
use PHPUnit\Framework\TestCase;
7-
use DateTime;
85
use DateInterval;
6+
use DateTime;
7+
use DateTimeImmutable;
8+
use PHPUnit\Framework\TestCase;
9+
use Westsworld\TimeAgo;
910

1011
/**
1112
* Testing timeago dates
@@ -42,7 +43,7 @@ public function testTimeAgoInWords()
4243
$this->assertEquals('1 minute ago', $timeAgo->inWordsFromStrings("-60 second"));
4344
$this->assertEquals('1 minute ago', $timeAgo->inWordsFromStrings("-89 second"));
4445
$this->assertNotEquals('1 minute ago', $timeAgo->inWordsFromStrings("-90 second"));
45-
46+
4647

4748
// testing 2..44 minutes
4849
$this->assertContains('minutes ago', $timeAgo->inWordsFromStrings("-2 minute"));
@@ -111,7 +112,7 @@ public function testTimeAgoInWords()
111112
public function testTimeAgoInWordsDateTime()
112113
{
113114
$timeAgo = new TimeAgo(new \Westsworld\TimeAgo\Translations\En());
114-
115+
115116
// testing "less than a minute"
116117
$this->assertEquals('less than a minute ago', $timeAgo->inWords(new DateTime()));
117118
$this->assertEquals('less than a minute ago', $timeAgo->inWords((new DateTime())->sub(new DateInterval('PT1S'))));
@@ -125,6 +126,23 @@ public function testTimeAgoInWordsDateTime()
125126
$this->assertNotEquals('1 minute ago', $timeAgo->inWords((new DateTime())->sub(new DateInterval('PT90S'))));
126127
}
127128

129+
public function testTimeAgoInWordsDateTimeImmutable()
130+
{
131+
$timeAgo = new TimeAgo(new \Westsworld\TimeAgo\Translations\En());
132+
133+
// testing "less than a minute"
134+
$this->assertEquals('less than a minute ago', $timeAgo->inWords(new DateTimeImmutable()));
135+
$this->assertEquals('less than a minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT1S'))));
136+
$this->assertEquals('less than a minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT29S'))));
137+
$this->assertNotEquals('less than a minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT30S'))));
138+
139+
// testing "1 minute"
140+
$this->assertEquals('1 minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT30S'))));
141+
$this->assertEquals('1 minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT60S'))));
142+
$this->assertEquals('1 minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT89S'))));
143+
$this->assertNotEquals('1 minute ago', $timeAgo->inWords((new DateTimeImmutable())->sub(new DateInterval('PT90S'))));
144+
}
145+
128146
public function testLanguage()
129147
{
130148
// using default (english)

0 commit comments

Comments
 (0)