Skip to content

Commit ff04bde

Browse files
update
1 parent 371031d commit ff04bde

File tree

2 files changed

+42
-61
lines changed

2 files changed

+42
-61
lines changed

src/Time.php

Lines changed: 38 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,16 @@ class Time {
7777
*/
7878
public function __construct($date = null, $timezone = null)
7979
{
80-
if(empty($this->timezone)){
80+
if (empty($this->timezone)) {
8181
$this->timezone = TimeHelper::configureAndSetTimezone($timezone);
8282
}
8383

84-
if (empty($this->date)) {
85-
$date = $date ?: 'now';
86-
$clone = self::date($date);
84+
// Avoid recursive static initialization by computing directly
85+
$resolvedDate = TimeHelper::setPassedDate($date ?: 'now');
86+
$this->date = $resolvedDate;
87+
$this->timestamp = $this->timestampPrint();
8788

88-
$this->date = $clone->date;
89-
$this->timestamp = $clone->timestamp;
90-
$this->timezone = $clone->timezone;
91-
}
92-
89+
// Bind the freshly constructed instance for static context reuse
9390
$this->keepStaticBinding($this->clone());
9491
}
9592

@@ -117,18 +114,11 @@ public static function __callStatic($name, $args)
117114
return self::nonExistMethod($name, $args, self::$staticData);
118115
}
119116

120-
121-
122-
/**
123-
* Add Second to curent date
124-
* @param int $value
125-
* @return $this
126-
*/
127117
/**
128118
* Add seconds to the current date.
129119
*
130-
* @param int $value Number of seconds to add
131-
* @return $this New cloned instance with updated time
120+
* @param int $value
121+
* @return $this
132122
*/
133123
public function addSeconds($value = 0)
134124
{
@@ -310,6 +300,36 @@ public static function createFromFormat($datetime, $format = 'm/d/Y h:i:sa')
310300
);
311301
}
312302

303+
/**
304+
* Format a date range.
305+
*
306+
* @param string $value The range in the format "1-7" (days from today).
307+
* @param string $format The desired date format, default is 'D, M j'.
308+
*
309+
* @return Tamedevelopers\Support\Capsule\TimeHelper
310+
* - The formatted date, e.g., "Mon, May 27".
311+
*/
312+
public static function dateRange($value, $format = 'D, M j')
313+
{
314+
// Check if the range has a hyphen
315+
if (strpos($value, '-') !== false) {
316+
// Split the range into start and end days
317+
[$start, $end] = explode('-', $value);
318+
} else {
319+
[$start, $end] = [0, $value];
320+
}
321+
322+
// Ensure the end value is the maximum number of days
323+
$daysToStart = (int) Str::trim($start);
324+
$daysToAdd = (int) Str::trim($end);
325+
326+
// Create a DateTime object for the current date
327+
$startDate = self::today()->addDays($daysToStart);
328+
$endDate = self::today()->addDays($daysToAdd);
329+
330+
return new TimeHelper($startDate, $endDate, $format);
331+
}
332+
313333
/**
314334
* Set time to `now`
315335
*
@@ -388,36 +408,6 @@ public static function toJsTimer($date)
388408
return self::timestamp($date, 'M j, Y H:i:s');
389409
}
390410

391-
/**
392-
* Format a date range.
393-
*
394-
* @param string $value The range in the format "1-7" (days from today).
395-
* @param string $format The desired date format, default is 'D, M j'.
396-
*
397-
* @return Tamedevelopers\Support\Capsule\TimeHelper
398-
* - The formatted date, e.g., "Mon, May 27".
399-
*/
400-
public function dateRange($value, $format = 'D, M j')
401-
{
402-
// Check if the range has a hyphen
403-
if (strpos($value, '-') !== false) {
404-
// Split the range into start and end days
405-
[$start, $end] = explode('-', $value);
406-
} else {
407-
[$start, $end] = [0, $value];
408-
}
409-
410-
// Ensure the end value is the maximum number of days
411-
$daysToStart = (int) Str::trim($start);
412-
$daysToAdd = (int) Str::trim($end);
413-
414-
// Create a DateTime object for the current date
415-
$startDate = $this->today()->addDays($daysToStart);
416-
$endDate = $this->today()->addDays($daysToAdd);
417-
418-
return new TimeHelper($startDate, $endDate, $format);
419-
}
420-
421411
/**
422412
* Set the configuration options for text representations.
423413
*

tests/time.php

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,6 @@
3535
// $time4->year(),
3636
// ];
3737

38-
dd(
39-
Time::now(),
40-
$time->getTimezone(),
41-
$time2,
42-
$time3->date('last month')->getWeek(),
43-
$time3->getTimezone(),
44-
$time4->getTimezone(),
45-
);
46-
4738
dd(
4839
[
4940
'Time-1: '. $time->getTimeZone(),
@@ -64,10 +55,10 @@
6455
],
6556

6657
// [
67-
// $time3->date('first day of December 2008')->format(),
68-
// $time3->yesterday()->format(),
69-
// $time3->today()->format(),
70-
// $time3->now()->format(),
58+
// Time::date('first day of December 2008')->format(),
59+
// Time::yesterday()->format(),
60+
// Time::today()->format(),
61+
// Time::now()->format(),
7162
// ],
7263

7364
$time4->date('first day of this month')->subDays(4),

0 commit comments

Comments
 (0)