Skip to content

Commit 486afb5

Browse files
Merge pull request #2 from karam-mustafa/main
v1,0,0
2 parents 24fd8dd + 39f3929 commit 486afb5

File tree

6 files changed

+290
-178
lines changed

6 files changed

+290
-178
lines changed

README.md

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,42 @@
1-
![logo](assets/logo.png)
2-
3-
Design a simple and expressive logo with the same model you see above,
4-
the logo design helps to unify the visual identity and further develop the optimal packages to increase the quality of the software we develop
5-
6-
#NOTES:
7-
* You should replace all your dependencies into your custom names and files including:
8-
* vendor namespace.
9-
* composer information.
10-
* logo
11-
* credit and contributors links
12-
* security email
13-
* To use this template, please click on button above "Use this template".
14-
* Delete this section after you have finished developing your package
15-
16-
# Package Name
17-
Your package description,Try to be briefly describing the features of the package,
18-
and if there are external resources for learning or explanation
1+
# Arabic Diff For Humans
2+
PHP Package to convert time difference to Arabic human readable string
193

204
##### 1 - Dependency
215
The first step is using composer to install the package and automatically update your composer.json file, you can do this by running:
226

237
```shell
24-
composer require vendor_name/package_name
8+
composer require syrian-open-source/php-arabic-diff-for-human
259
```
2610

27-
Features Or Usage
11+
Usage
2812
-----------
29-
Put the name of the feature and a link to this feature
30-
- [Any feature](https://github.com/syrian-open-source/php-package-template/blob/main/docs/feature.md#usage)
31-
13+
Convert from string date, this feature will work to compare years, months,days,minutes, and seconds.
14+
You can pass the date as a string with any format.
15+
```php
16+
$instance = (new ArabicDiffForHumans());
17+
$time = "2018-1-1";
18+
echo $instance->getFromDateString($time);
19+
// the result should be: "منذ 4 سنين"
20+
```
21+
Convert from string date, this feature will work to compare years, months,days,minutes, and seconds.
22+
You can pass the date as a string with any format.
23+
```php
24+
$instance = (new ArabicDiffForHumans());
25+
$time = "2018-1-1";
26+
echo $instance->get(strtotime($time));
27+
// the result should be: "منذ 4 سنين"
28+
```
3229

3330
Changelog
3431
---------
35-
Please see the [CHANGELOG](https://github.com/syrian-open-source/php-package-template/blob/master/CHANGELOG.md) for more information about what has changed or updated or added recently.
32+
Please see the [CHANGELOG](https://github.com/syrian-open-source/php-arabic-diff-for-human/blob/master/CHANGELOG.md) for more information about what has changed or updated or added recently.
3633

3734
Security
3835
--------
39-
If you discover any security related issues, please email them first to "your email",
36+
If you discover any security related issues, please email them first to [email protected],
4037
if we do not fix it within a short period of time please open a new issue describing your problem.
4138

4239
Credits
4340
-------
44-
* [your email](https://github.com/syrian-open-source/php-package-template/graphs/contributors)
45-
* [All contributors](https://github.com/syrian-open-source/php-package-template/graphs/contributors)
41+
* [Abdussalam M. Al-Ali](https://www.linkedin.com/in/abdussalam-alali/)
42+
* [All contributors](https://github.com/syrian-open-source/php-arabic-diff-for-human/graphs/contributors)
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
}

0 commit comments

Comments
 (0)