Skip to content

Commit c5f36bf

Browse files
added config_time function helper
1 parent 2baa138 commit c5f36bf

File tree

5 files changed

+237
-72
lines changed

5 files changed

+237
-72
lines changed

README.md

+123-31
Original file line numberDiff line numberDiff line change
@@ -72,27 +72,19 @@ Support Package For PHP and Laravel
7272
* [PDF](#pdf)
7373
* [Read PDF](#read-pdf)
7474
* [Time](#time)
75+
* [time-data](#time-data)
7576
* [now](#now)
76-
* [sec](#sec)
77-
* [min](#min)
78-
* [day](#day)
79-
* [hour](#hour)
80-
* [weeks](#weeks)
81-
* [month](#month)
82-
* [year](#year)
77+
* [date](#date)
8378
* [today](#today)
84-
* [time](#time)
85-
* [format](#format)
8679
* [yesterday](#yesterday)
8780
* [timestamp](#timestamp)
8881
* [toJsTimer](#toJsTimer)
89-
* [getSeconds](#getSeconds)
90-
* [getMinutes](#getMinutes)
91-
* [getHours](#getHours)
92-
* [getDays](#getDays)
93-
* [getWeeks](#getWeeks)
94-
* [getMonths](#getMonths)
95-
* [getYears](#getYears)
82+
* [diff](#diff)
83+
* [diffBetween](#diffBetween)
84+
* [ago](#ago)
85+
* [range](#range)
86+
* [format](#format)
87+
* [toDateTimeString](#toDateTimeString)
9688
* [addSeconds](#addSeconds)
9789
* [subSeconds](#subSeconds)
9890
* [addMinutes](#addMinutes)
@@ -108,13 +100,9 @@ Support Package For PHP and Laravel
108100
* [addYears](#addYears)
109101
* [subYears](#subYears)
110102
* [greeting](#greeting)
111-
* [diff](#diff)
112-
* [ago](#ago)
113-
* [dateRange](#dateRange)
114103
* [allTimezone](#allTimezone)
115104
* [setTimeZone](#setTimeZone)
116105
* [getTimeZone](#getTimeZone)
117-
* [toDateTimeString](#toDateTimeString)
118106
* [UrlHelper](#UrlHelper)
119107
* [url](#url)
120108
* [http](#http)
@@ -553,31 +541,135 @@ use Tamedevelopers\Support\Time;
553541
$time = new Time('now', 'Africa/Lagos');
554542
```
555543

544+
### time-data
545+
- Get time date from class
546+
547+
| function name | Description |
548+
|---------------------------|---------------------------|
549+
| `sec() \| getSec()` | Get seconds from time |
550+
| `min() \| getMin()` | Get minutes |
551+
| `hour() \| getHour()` | Get hour |
552+
| `day() \| getDay()` | Get days |
553+
| `week() \| getWeek()` | Get weeks |
554+
| `month() \| getMonth()` | Get months |
555+
| `year() \| getYear()` | Get years |
556+
| `time() \| getTime()` | Get time as int |
557+
558+
```
559+
[
560+
$time4->time(),
561+
$time4->sec(),
562+
$time4->min(),
563+
$time4->hour(),
564+
$time4->day(),
565+
$time4->week(),
566+
$time4->month(),
567+
$time4->year(),
568+
]
569+
```
570+
556571
### now
572+
- Returns the Time Object with current timestamp of `now`
573+
```
574+
$time->now()->format()
575+
```
576+
577+
### date
578+
- Accepts one param as (time) `int|string`
579+
```
580+
$time->date("first day of this month")->toDateTimeString()
581+
```
582+
583+
### today
584+
- Thesame as `now()` with timestamp of `today`
585+
586+
### yesterday
587+
- Thesame as `now()` with timestamp of `yesterday`
557588

558589
```
559-
$time->now()
560-
// returns the Time Object with current timestamp
590+
$time->today();
591+
$time->yesterday();
561592
```
562593

563-
### dateRange
594+
### timestamp
595+
- Accepts two parameter [date, format]
596+
- only [date] is mandatory and returns formated timestamp
597+
598+
```
599+
$time->timestamp('24 Jan 2025 14:00:00');
600+
// Output: 2025-01-24 14:00:00
601+
```
602+
603+
### toJsTimer
604+
- Accept one parameter as [date]. Returns formated javascript timestamp
605+
606+
```
607+
$time->toJsTimer('24 Jan 2025 14:00:00');
608+
$time->jsTimer('24 Jan 2025 14:00:00');
609+
// Output: Jan 24, 2025 14:00:00
610+
```
611+
612+
### diff
613+
- Takes one paramater as `mode`. Different between the given date a current time as `now`
614+
- Return an array if [mode] is not found or value of `mode set`
615+
616+
| mode |
617+
|-----------------------------------------------------------|
618+
| `year \| month \| hour \| mins \| sec \| days \| weeks` |
619+
620+
```
621+
$time->date('last year december')->diff('month');
622+
// Output: 1
623+
```
624+
625+
### diffBetween
626+
- Takes three paramater as `firstDate \| lastDate \| mode`. Thesame as diff.
627+
628+
```
629+
$time->diffBetween('last year december', 1737752400, 'weeks');
630+
// Output: 4
631+
```
632+
633+
### ago
634+
- `ago() or timeAgo()`, Takes one paramater as `mode`
635+
636+
| mode |
637+
|------------------------------------------------------------------------|
638+
| `full \| short \| duration \| time \| date \| date_time \| time_stamp` |
639+
640+
```
641+
$time->date('today')->ago()
642+
$time->date('today')->timeAgo()
643+
644+
// Output: [
645+
"full" => "4 hours ago"
646+
"short" => "4h"
647+
"duration" => 4
648+
"time" => 1737752400
649+
"date" => "24 Jan, 2025"
650+
"date_time" => "24 Jan, 2025 10:01am"
651+
"time_stamp" => "Jan 24, 2025 10:00:00"
652+
]
653+
```
654+
655+
### range
564656
- Build date range according to value given
565657
- Accepts (2) params `value and format`
566658

567659
```
568-
$time->dateRange('0-10', 'D, M j')
660+
$time->range('0-10', 'D, M j')
569661
// Output: returns class of Tamedevelopers\Support\Capsule\TimeHelper
662+
```
570663

571-
To get the output, we need to call the TimeHelper format method
572-
The format() method takes two [optional] param. `start, year` as boolean
664+
#### get output
665+
- To get the output, we need to call the TimeHelper format method
666+
- The format() method takes two [optional] param. `start, year` as boolean
573667

574-
$time->dateRange('0-10', 'D, M j')
575-
->format(true, true)
668+
```
669+
$time->range('0-10')->format(true, true)
576670
// Output: Thu, Jan 23 - Tue, Mar 4, 2025
577671
578-
579-
$time->dateRange('0-10', 'D, M j')
580-
->format()
672+
$time->range('0-10')->format()
581673
// Output: Tue, Mar 4
582674
```
583675

Tests/time.php

+17-4
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@
3434
$time4 = TameTime('first day of December 2008', 'Pacific/Pago_Pago');
3535

3636
dd(
37+
[
38+
$time->toJsTimer('24 Jan 2025 14:00:00'),
39+
$time4->time(),
40+
$time4->sec(),
41+
$time4->min(),
42+
$time4->hour(),
43+
$time4->day(),
44+
$time4->week(),
45+
$time4->month(),
46+
$time4->year(),
47+
],
48+
3749
[
3850
$time->getTimeZone(),
3951
$time2->getTimeZone(),
@@ -67,13 +79,14 @@
6779
[
6880
$time4->yesterday()->toDateTimeString(),
6981
$time4->format(null, 'first day of December 2008'),
70-
$time4->date('last year december')->diff(),
71-
$time4->date('last year december')->ago(),
82+
$time4->date('last year december')->diff('weeks'),
83+
$time4->setTimezone('Indian/Antananarivo')->diffBetween('last year', 'today', 'weeks'),
84+
$time4->date('today')->ago(),
7285
],
7386

7487

75-
TameTime()->dateRange('1-14'),
76-
TameTime()->dateRange('0-40')->format(true, true),
88+
TameTime()->range('1-14'),
89+
TameTime()->range('0-40')->format(true, true),
7790
//
7891

7992
);

Time.php

+47-21
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,11 @@ public function __construct($date = null, $timezone = null)
5757
}
5858

5959
if(empty($this->date)){
60-
$this->__setDate($date);
60+
$clone = $this->__setDate($date);
61+
62+
$this->date = $clone->date;
63+
$this->timestamp = $clone->timestamp;
64+
$this->timezone = $clone->timezone;
6165
}
6266

6367
// clone copy of self
@@ -286,7 +290,7 @@ public function format($format = null, $date = null)
286290
if(!empty($date)){
287291
$clone = $this->__setDate($date);
288292

289-
$this->date = $clone->date;
293+
$this->date = $this->date;
290294
}
291295

292296
if(empty($format)){
@@ -303,7 +307,7 @@ public function format($format = null, $date = null)
303307
*/
304308
public function toDateTimeString()
305309
{
306-
return date('Y-m-d H:i:s', $this->date);
310+
return $this->format();
307311
}
308312

309313
/**
@@ -401,6 +405,15 @@ static public function config(?array $options = [])
401405
}
402406
}
403407

408+
/**
409+
* Get the stored date time
410+
* @return int
411+
*/
412+
public function __getDate()
413+
{
414+
return (int) $this->date;
415+
}
416+
404417
/**
405418
* Get the number of seconds between the stored time and the current time.
406419
* @return mixed
@@ -494,6 +507,30 @@ public function __greeting($date = 'now')
494507
return $text['night'];
495508
}
496509

510+
/**
511+
* Calculate the time difference between both given time.
512+
*
513+
* @param mixed $firstDate
514+
* @param mixed $lastDate
515+
* @param string|null $mode
516+
*
517+
* @return mixed
518+
*/
519+
public function __timeDifferenceBetween($firstDate, $lastDate, $mode = null)
520+
{
521+
$clone = $this->clone();
522+
523+
// convert to actual time as int
524+
$firstDate = TimeHelper::setPassedDate($firstDate);
525+
$lastDate = TimeHelper::setPassedDate($lastDate);
526+
527+
// Get the current time in the specified timezone
528+
$first = new DateTime($clone->timestamp($firstDate), new DateTimeZone($clone->timezone));
529+
$last = new DateTime($clone->timestamp($lastDate), new DateTimeZone($clone->timezone));
530+
531+
return $this->calculateTimeDifference($first, $last, $mode);
532+
}
533+
497534
/**
498535
* Calculate the time difference between the stored time and the current time.
499536
* @param string|null $mode
@@ -504,28 +541,17 @@ public function __timeDifference($mode = null)
504541
{
505542
$clone = $this->clone();
506543

507-
$selfDate = TimeHelper::carbonInstance($clone->date);
508-
$now = new DateTime('now', new DateTimeZone($clone->timezone));
509-
$date = new DateTime();
510-
511-
if(!empty($selfDate)){
544+
// Convert the stored time to a DateTime object
545+
$selfDate = TimeHelper::carbonInstance($clone->date);
546+
$date = new DateTime();
547+
if (!empty($selfDate)) {
512548
$date->setTimestamp($selfDate);
513549
}
514550

515-
// get difference
516-
$difference = $now->diff($date);
517-
518-
$timeData = [
519-
'year' => $difference->y,
520-
'month' => ($difference->y * 12) + $difference->m,
521-
'hour' => $difference->h,
522-
'mins' => $difference->i,
523-
'sec' => $difference->s,
524-
'days' => $difference->days, //total number of days
525-
'weeks' => (int) floor($difference->days / 7), //weeks
526-
];
551+
// Get the current time in the specified timezone
552+
$now = new DateTime('now', new DateTimeZone($clone->timezone));
527553

528-
return $timeData[$mode] ?? $timeData;
554+
return $this->calculateTimeDifference($now, $date, $mode);
529555
}
530556

531557
/**

0 commit comments

Comments
 (0)