Skip to content

Commit 5a195b3

Browse files
committed
wip
1 parent 6884284 commit 5a195b3

File tree

5 files changed

+269
-118
lines changed

5 files changed

+269
-118
lines changed
Lines changed: 187 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,192 @@
11
<?php
22

3-
namespace SOS\ArabicDiffForHumans\Classes;
3+
namespace SOS\ArabicDiffForHumans\Abstracts;
44

5-
class ArabicDiffForHumansAbstract
5+
/**
6+
* Class ArabicDiffForHumansAbstract
7+
*
8+
* @author karam mustafa
9+
* @package SOS\ArabicDiffForHumans\Classes
10+
*/
11+
abstract class ArabicDiffForHumansAbstract
612
{
13+
14+
/**
15+
*
16+
*
17+
* @param $diff
18+
* @param $unit
19+
*
20+
* @return float|int
21+
* @author karam mustafa
22+
*/
23+
protected function DiffValue($diff, $unit)
24+
{
25+
$diffValue = 0;
26+
27+
switch ($unit) {
28+
case 1:
29+
$diffValue = $diff;
30+
break;
31+
case 2:
32+
$diffValue = $this->getMinutes($diff);
33+
break;
34+
case 3:
35+
$diffValue = $this->getHours($diff);
36+
break;
37+
case 4:
38+
$diffValue = $this->getDays($diff);
39+
break;
40+
case 5:
41+
$diffValue = $this->getMonths($diff);
42+
break;
43+
case 6:
44+
$diffValue = $this->getYears($diff);
45+
}
46+
return $diffValue;
47+
}
48+
49+
/**
50+
*
51+
*
52+
* @param $timestamp
53+
*
54+
* @return float|int
55+
* @author karam mustafa
56+
*/
57+
protected function getDifference($timestamp)
58+
{
59+
return abs(time() - $timestamp);
60+
}
61+
62+
/**
63+
*
64+
*
65+
* @param $diff
66+
*
67+
* @return int
68+
* @author karam mustafa
69+
*/
70+
protected function unit($diff)
71+
{
72+
if ($diff < 60) {
73+
return 1;
74+
} // seconds
75+
else {
76+
if ($diff < 60 * 60) {
77+
return 2;
78+
} // minutes
79+
else {
80+
if ($diff < 24 * 60 * 60) {
81+
return 3;
82+
} // hours
83+
else {
84+
if ($diff < 24 * 60 * 60 * 30) {
85+
return 4;
86+
} //days
87+
else {
88+
if ($diff < 24 * 60 * 60 * 365) {
89+
return 5;
90+
} // months
91+
else {
92+
return 6;
93+
}
94+
}
95+
}
96+
}
97+
} //years
98+
}
99+
100+
/**
101+
*
102+
*
103+
* @param $diff
104+
*
105+
* @return float|int
106+
* @author karam mustafa
107+
*/
108+
protected function getMinutes($diff)
109+
{
110+
return $diff / 60;
111+
}
112+
113+
/**
114+
*
115+
*
116+
* @param $diff
117+
*
118+
* @return float|int
119+
* @author karam mustafa
120+
*/
121+
protected function getHours($diff)
122+
{
123+
return $diff / (60 * 60);
124+
}
125+
126+
/**
127+
*
128+
*
129+
* @param $diff
130+
*
131+
* @return float|int
132+
* @author karam mustafa
133+
*/
134+
protected function getDays($diff)
135+
{
136+
return $diff / (60 * 60 * 24);
137+
}
138+
139+
/**
140+
*
141+
*
142+
* @param $diff
143+
*
144+
* @return float|int
145+
* @author karam mustafa
146+
*/
147+
protected function getMonths($diff)
148+
{
149+
return $diff / (60 * 60 * 24 * 30);
150+
}
151+
152+
/**
153+
*
154+
*
155+
* @param $diff
156+
*
157+
* @return float|int
158+
* @author karam mustafa
159+
*/
160+
protected function getYears($diff)
161+
{
162+
return $diff / (365 * 24 * 60 * 60);
163+
}
164+
165+
/**
166+
*
167+
*
168+
* @param $valueOfDifference
169+
* @param $single
170+
* @param $double
171+
* @param $plural
172+
*
173+
* @return string
174+
* @author karam mustafa
175+
*/
176+
protected function formatResult($valueOfDifference, $single, $double, $plural)
177+
{
178+
if ($valueOfDifference == 1) {
179+
return "منذ ".$single;
180+
}
181+
182+
if ($valueOfDifference == 2) {
183+
return " منذ ".$double;
184+
}
185+
186+
if ($valueOfDifference <= 10) {
187+
return "منذ ".$valueOfDifference." ".$plural;
188+
}
189+
190+
return "منذ ".$valueOfDifference." ".$single;
191+
}
7192
}

src/Classes/ArabicDiffForHumans.php

Lines changed: 33 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -2,118 +2,55 @@
22

33
namespace SOS\ArabicDiffForHumans\Classes;
44

5+
use SOS\ArabicDiffForHumans\Abstracts\ArabicDiffForHumansAbstract;
6+
use SOS\ArabicDiffForHumans\Interfaces\ArabicDiffForHumansInterface;
7+
58
/**
69
* Class ArabicDiffForHumans
710
*
811
* @author your name
912
* @package SOS\ArabicDiffForHumans\Classes
1013
*/
11-
class ArabicDiffForHumans
14+
class ArabicDiffForHumans extends ArabicDiffForHumansAbstract implements ArabicDiffForHumansInterface
1215
{
13-
public static function getFromDateString($dateString)
16+
private $formatted = [
17+
["ثانية", "ثانيتين", "ثواني"],
18+
["دقيقة", "دقيقتين", "دقائق"],
19+
["ساعة", "ساعتين", "ساعات"],
20+
["يوم", "يومين", "أيام"],
21+
["شهر", "شهرين", "أشهر"],
22+
["سنة", "سنتين", "سنين"],
23+
];
24+
25+
/**
26+
* @inheritDoc
27+
*/
28+
public function getFromDateString($dateString)
1429
{
1530
$timeStamp = strtotime($dateString);
16-
return self::get($timeStamp);
31+
32+
return $this->get($timeStamp);
1733
}
18-
public static function get($timeStamp)
34+
35+
/**
36+
* @inheritDoc
37+
*/
38+
public function get($timeStamp)
1939
{
20-
$diff = self::getDifference($timeStamp);
21-
$unit = self::unit($diff);
22-
$valueOfDifference = floor(self::DiffValue($diff,$unit));
40+
$diff = $this->getDifference($timeStamp);
41+
42+
$unit = $this->unit($diff);
43+
44+
$valueOfDifference = floor($this->DiffValue($diff, $unit));
2345

2446
$result = "";
25-
switch ($unit){
26-
case 1:
27-
$result = self::formatResult($valueOfDifference,"ثانية","ثانيتين","ثواني");
28-
break;
29-
case 2:
30-
$result = self::formatResult($valueOfDifference,"دقيقة","دقيقتين","دقائق");
31-
break;
32-
case 3:
33-
$result = self::formatResult($valueOfDifference,"ساعة","ساعتين","ساعات");
34-
break;
35-
case 4:
36-
$result = self::formatResult($valueOfDifference,"يوم","يومين","أيام");
37-
break;
38-
case 5:
39-
$result = self::formatResult($valueOfDifference,"شهر","شهرين","أشهر");
40-
break;
41-
case 6:
42-
$result = self::formatResult($valueOfDifference,"سنة","سنتين","سنين");
43-
break;
4447

48+
foreach ($this->formatted as $key => $value) {
49+
if (($key + 1) == $unit) {
50+
$result = $this->formatResult($valueOfDifference, ...$value);
51+
}
4552
}
4653
return $result;
4754
}
48-
private static function DiffValue($diff,$unit)
49-
{
50-
$diffValue = 0;
51-
switch ($unit)
52-
{
53-
case 1:
54-
$diffValue = $diff;
55-
break;
56-
case 2:
57-
$diffValue = self::getMinutes($diff);
58-
break;
59-
case 3:
60-
$diffValue = self::getHours($diff);
61-
break;
62-
case 4:
63-
$diffValue = self::getDays($diff);
64-
break;
65-
case 5:
66-
$diffValue = self::getMonths($diff);
67-
break;
68-
case 6:
69-
$diffValue = self::getYears($diff);
70-
}
71-
return $diffValue;
72-
}
73-
private static function getDifference($timestamp)
74-
{
75-
return abs(time()-$timestamp);
76-
}
7755

78-
private static function unit($diff){
79-
if ($diff<60)
80-
return 1; // seconds
81-
else if ($diff<60*60)
82-
return 2; // minutes
83-
else if($diff<24*60*60)
84-
return 3; // hours
85-
else if($diff<24*60*60*30)
86-
return 4; //days
87-
else if($diff<24*60*60*365)
88-
return 5; // months
89-
else
90-
return 6; //years
91-
}
92-
93-
private static function getMinutes($diff) {
94-
return $diff/60;
95-
}
96-
private static function getHours($diff) {
97-
return $diff/(60*60);
98-
}
99-
private static function getDays($diff) {
100-
return $diff/(60*60*24);
101-
}
102-
private static function getMonths($diff) {
103-
return $diff/(60*60*24*30);
104-
}
105-
private static function getYears($diff) {
106-
return $diff/(365*24*60*60);
107-
}
108-
109-
private static function formatResult($valueOfDifference,$single,$double,$plural)
110-
{
111-
if($valueOfDifference==1)
112-
return "منذ ".$single;
113-
if($valueOfDifference==2)
114-
return " منذ ".$double;
115-
if($valueOfDifference<=10)
116-
return "منذ ".$valueOfDifference." ".$plural;
117-
return "منذ ".$valueOfDifference." ".$single;
118-
}
11956
}

src/Interfaces/ArabicDiffForHumansInterface.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,24 @@
44

55
interface ArabicDiffForHumansInterface
66
{
7+
8+
/**
9+
* get the date from the string date object.
10+
*
11+
* @param $date
12+
*
13+
* @return mixed
14+
* @author karam mustafa
15+
*/
16+
public function getFromDateString($date);
17+
18+
/**
19+
* get the date from the time object.
20+
*
21+
* @param $date
22+
*
23+
* @return mixed
24+
* @author karam mustafa
25+
*/
26+
public function get($date);
727
}

0 commit comments

Comments
 (0)