Skip to content

Commit 59639b2

Browse files
update
1 parent 44c319d commit 59639b2

File tree

3 files changed

+43
-13
lines changed

3 files changed

+43
-13
lines changed

Tests/time.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
require_once __DIR__ . '/../vendor/autoload.php';
66

77

8+
// set global time zone for server
9+
// now server time will be set to the given timezone as default
10+
Time::setGlobalTimeZone('Pacific/Pago_Pago');
11+
812

913
// set the default time and timezone
1014
// helper function
@@ -35,5 +39,8 @@
3539
$time2->getTimeZone(),
3640

3741
$time3,
38-
$time3->greetings()
42+
$time3->greetings(),
43+
44+
Time::getGlobalTimeZone(),
45+
3946
);

Time.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,11 @@ class Time {
4646
public function __construct($date = null, $timezone = null)
4747
{
4848
if(empty($this->date)){
49-
$this->date = TimeHelper::setPassedDate(
50-
$date ?? 'now'
51-
);
49+
$this->date = TimeHelper::setPassedDate($date);
5250
}
5351

5452
if(empty($this->timezone)){
55-
$this->timezone = TimeHelper::setPassedTimezone(
56-
$timezone ?? 'UTC'
57-
);
53+
$this->timezone = TimeHelper::setPassedTimezone($timezone);
5854
}
5955

6056
// clone copy of self

Traits/TimeTrait.php

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ static protected function isTimeInstance()
2222
{
2323
return self::$staticData instanceof Time;
2424
}
25+
26+
/**
27+
* Set Global TimeZone
28+
*
29+
* @return void
30+
*/
31+
public function __setGlobalTimeZone($timezone = null)
32+
{
33+
$timezone = TimeHelper::setPassedTimezone($timezone);
34+
35+
date_default_timezone_set($timezone);
36+
}
37+
38+
/**
39+
* Get Global TimeZone
40+
*
41+
* @return string|null
42+
*/
43+
public function __getGlobalTimeZone()
44+
{
45+
return date_default_timezone_get();
46+
}
2547

2648
/**
2749
* Get the stored date time
@@ -87,9 +109,7 @@ static public function setDate($date = null)
87109
if(!self::isTimeInstance()){
88110
new static(date: $date);
89111
} else{
90-
self::$staticData->date = TimeHelper::setPassedDate(
91-
$date ?? 'now'
92-
);
112+
self::$staticData->date = TimeHelper::setPassedDate($date);
93113
}
94114

95115
return self::$staticData;
@@ -108,9 +128,7 @@ static public function setTimezone($timezone = null)
108128
}
109129

110130
// set timezone
111-
self::$staticData->timezone = TimeHelper::setPassedTimezone(
112-
$timezone ?? 'UTC'
113-
);
131+
self::$staticData->timezone = TimeHelper::setPassedTimezone($timezone);
114132

115133
return self::$staticData;
116134
}
@@ -144,9 +162,18 @@ static private function nonExistMethod($method = null, $args = null, $clone = nu
144162
'gettimezone' => '__getTimeZone',
145163
'settimezone' => '__setTimeZone',
146164
'setdate' => '__setDate',
165+
'setglobaltimezone' => '__setGlobalTimeZone',
166+
'getglobaltimezone' => '__getGlobalTimeZone',
147167
default => '__timeAgo'
148168
};
149169

170+
// this will happen if __construct has not been called
171+
// before calling an existing method
172+
// mostly when using [setglobaltimezone|getglobaltimezone] methods
173+
if(empty($clone)){
174+
$clone = new static();
175+
}
176+
150177
return $clone->$method(...$args);
151178
}
152179

0 commit comments

Comments
 (0)