Skip to content

Commit 780a6a9

Browse files
committed
Initial commit
0 parents  commit 780a6a9

File tree

10 files changed

+2680
-0
lines changed

10 files changed

+2680
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.idea
2+
.phpunit.result.cache
3+
/vendor/

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Hadi Akbarzadeh info@elatel.ir
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
# Dati (Date Time Helper for PHP)
2+
3+
> A simple DateTime helper for common stuff.
4+
5+
Checking the validity of Gregorian and Jalali dates, spam detection based on datetime sequences,
6+
detecting datetime formats,
7+
calculating the difference between two datetimes in a preferred unit or the largest possible unit,
8+
remaining time between two datetimes based on validity,
9+
checking for leap years in both Gregorian and Jalali calendars, adding a value to a datetime,
10+
converting timezones to a desired or local one,
11+
month names, month lengths, current time, and so on!
12+
13+
<hr>
14+
15+
## 🫡 Usage
16+
17+
### 🚀 Installation
18+
19+
You can install the package via composer:
20+
21+
```bash
22+
composer require nabeghe/dati
23+
```
24+
25+
<hr>
26+
27+
### Dati Class
28+
29+
#### Example 1:
30+
31+
```php
32+
use Nabeghe\Dati\Dati;
33+
34+
echo 'Format: '.Dati::detectFormat('1995-11-20 00:00:00')."\n"; // Y-m-d H:i:s
35+
36+
echo 'Diff 1: '.Dati::diff('1995-11-20 00:00:00', '1995-11-20 00:00:01')."\n"; // 1
37+
echo 'Diff 2: '.Dati::diff('1995-11-20 00:00:00', '1995-11-31 00:00:00', 'days')."\n"; // 11
38+
echo 'Diff 3: '.Dati::diff('1995-11-20 00:00:00', '1995-11-31 12:00:00', 'days')."\n"; // 11.5
39+
echo 'Diff 4: '.((int) Dati::diff('1995-11-20 00:00:00', '2024-10-19 22:58:00', 'years'))."\n"; // 28
40+
41+
echo "How Long Ago:\n";
42+
print_r(Dati::howLongAgo('2024-10-19 00:00:00', '2025-10-19 00:00:00'));
43+
// ['unit' => 'years', 'value' => 1, 'diff' => 31536000]
44+
45+
echo Dati::isLeap(2024) ? "isLeap\n" : "not isLeap\n";
46+
47+
echo 'Join 1: '.Dati::join('+1', 'seconds', '1995-11-20 00:00:00')."\n"; // 1995-11-20 00:00:01
48+
echo 'Join 2: '.Dati::join('+2', 'seconds', '1995-11-20 00:00:00')."\n"; // 1995-11-20 00:00:02
49+
echo 'Join 3: '.Dati::join('+1', 'month', '1995-11-20 00:00:00')."\n"; // 1995-12-20 00:00:00
50+
51+
echo 'Remaining 1: '.Dati::remaining('2024-10-19 23:00:00', '2024-10-20 00:00:00', 60, 'minutes')."\n"; // 0
52+
echo 'Remaining 2: '.((int) Dati::remaining('2024-10-19 23:00:00', '2024-10-20 00:00:00', 120, 'minutes'))."\n"; // 60
53+
```
54+
55+
#### Example 2 - Check Spam:
56+
57+
Sometimes it is necessary to check the datetime of an action to determine if it is spam.
58+
For example, reviewing the last ten datetimes of messages or tickets that have been sent.
59+
60+
```php
61+
// Syntax:
62+
Diff::checkSpam(array $datetimes, int $offset = 1, int $limit = 3, $strict = false)
63+
```
64+
65+
- `datetimes`: Sequence of datetimes.
66+
- `offset`: If the difference between two consecutive datetimes is equal to or less than this amount, a warning indicating potential spam has occurred.
67+
- `limit`: Receiving how many warning indicates spam?
68+
- `strict`: If it's not strict, if no warning is received between two consecutive datetimes, one of the previous warnings will be reduced.
69+
70+
```php
71+
use Nabeghe\Dati\Dati;
72+
73+
if (Dati::checkSpam([
74+
'2024-10-19 22:46:01',
75+
'2024-10-19 22:46:02',
76+
'2024-10-19 22:46:03',
77+
'2024-10-19 22:46:04',
78+
])) {
79+
echo "Spam 1\n";
80+
}
81+
82+
if (Dati::checkSpam([
83+
'2024-10-19 22:46:01',
84+
'2024-10-19 22:46:01',
85+
'2024-10-19 22:46:01',
86+
'2024-10-19 22:46:01',
87+
])) {
88+
echo "Spam 2\n";
89+
}
90+
91+
if (!Dati::checkSpam([
92+
'2024-10-19 22:46:01',
93+
'2024-10-19 22:46:02',
94+
'2024-10-19 22:46:03',
95+
])) {
96+
echo "Not Spam 3\n";
97+
}
98+
99+
if (!Dati::checkSpam([
100+
'2024-10-19 22:46:01',
101+
'2024-10-19 22:46:03',
102+
'2024-10-19 22:46:03',
103+
'2024-10-19 22:46:03',
104+
])) {
105+
echo "Not Spam 4\n";
106+
}
107+
```
108+
109+
<hr>
110+
111+
### Months Class
112+
113+
Access to the names and lengths of the months in the Gregorian, Jalali, and lunar calendars.
114+
115+
```php
116+
use Nabeghe\Dati\Months;
117+
118+
echo "Month Length 2024/11: " . Months::length(2024, 11) . "\n"; // 30
119+
echo "Month Length 2024/12: " . Months::length(2024, 12) . "\n"; // 31
120+
echo "Month Length 1402/12 (Jalali): " . Months::length(1402, 12, 'jalali') . "\n"; // 29
121+
echo "Month Length 1403/12 (Jalali): " . Months::length(1403, 12, 'jalali') . "\n"; // 30
122+
echo "Month Length 1404/12 (Jalali): " . Months::length(1404, 12, 'jalali') . "\n"; // 29
123+
124+
echo "Month Name 11: " . Months::name(11, 'gregorian', true) . "\n"; // November
125+
echo "Month Name 11: " . Months::name(11, 'gregorian', false) . "\n"; // نوامبر
126+
```
127+
128+
### Now Class
129+
130+
Access to the current datetime.
131+
132+
#### Example 1:
133+
134+
```php
135+
use Nabeghe\Dati\Now;
136+
137+
echo "Now (GMT): " . Now::datetime() . "\n";
138+
echo "Now (GMT): " . Now::datetime() . "\n"; // The output will be the same as before. The time has been cached.
139+
echo "Now (Local): " . Now::datetimeNew() . "\n";
140+
echo "Now (Local): " . Now::datetimeLocal() . "\n";
141+
echo "Now (Local): " . Now::datetimeLocal() . "\n"; // The output will be the same as before. The time has been cached.
142+
echo "Now (Local): " . Now::datetimeLocalNew() . "\n";
143+
```
144+
145+
#### Example 2 - Initialization with previous values
146+
147+
If it is necessary to use a previously obtained datetime.
148+
149+
```php
150+
use Nabeghe\Dati\Now;
151+
152+
Now::init('1995-11-20 00:00:00', '1995-11-20 13:14:00');
153+
154+
echo "Now (GMT): " . Now::datetime() . "\n"; // 1995-11-20 00:00:00
155+
echo "Now (Local): " . Now::datetimeLocal() . "\n"; // 1995-11-20 13:14:00
156+
```
157+
158+
<hr>
159+
160+
## 📖 License
161+
162+
Copyright (c) Hadi Akbarzadeh
163+
164+
Licensed under the MIT license, see [LICENSE.md](LICENSE.md) for details.

composer.json

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
{
2+
"name": "nabeghe/dati",
3+
"description": "A simple DateTime helper for common stuff.",
4+
"keywords": [
5+
"datetime", "date", "time", "datetime helper", "datetime utils", "helper", "utils", "support", "library",
6+
"calendar"
7+
],
8+
"type": "library",
9+
"version": "0.1.0",
10+
"homepage": "https://github.com/nabeghe/dati-php",
11+
"license": "MIT",
12+
"autoload": {
13+
"psr-4": {
14+
"Nabeghe\\Dati\\": "src/"
15+
}
16+
},
17+
"authors": [
18+
{
19+
"name": "Hadi Akbarzadeh",
20+
"email": "hadicoder@gmail.com",
21+
"homepage": "https://elatel.ir",
22+
"role": "Developer"
23+
}
24+
],
25+
"require": {
26+
"php": ">=7.4"
27+
},
28+
"require-dev": {
29+
"phpunit/phpunit": "9.6"
30+
}
31+
}

0 commit comments

Comments
 (0)