@@ -108,6 +108,43 @@ public static function createFromFormat(
108108 }
109109
110110
111+ public function __construct (string $ datetime = 'now ' , ?\DateTimeZone $ timezone = null )
112+ {
113+ parent ::__construct ($ datetime , $ timezone );
114+ $ this ->handleErrors ($ datetime );
115+ }
116+
117+
118+ public function modify (string $ modifier ): static
119+ {
120+ parent ::modify ($ modifier ) && $ this ->handleErrors ($ modifier );
121+ return $ this ;
122+ }
123+
124+
125+ public function setDate (int $ year , int $ month , int $ day ): static
126+ {
127+ if (!checkdate ($ month , $ day , $ year )) {
128+ trigger_error (sprintf (self ::class . ': The date %04d-%02d-%02d is not valid. ' , $ year , $ month , $ day ), E_USER_WARNING );
129+ }
130+ return parent ::setDate ($ year , $ month , $ day );
131+ }
132+
133+
134+ public function setTime (int $ hour , int $ minute , int $ second = 0 , int $ microsecond = 0 ): static
135+ {
136+ if (
137+ $ hour < 0 || $ hour > 23
138+ || $ minute < 0 || $ minute > 59
139+ || $ second < 0 || $ second >= 60
140+ || $ microsecond < 0 || $ microsecond >= 1_000_000
141+ ) {
142+ trigger_error (sprintf (self ::class . ': The time %02d:%02d:%08.5F is not valid. ' , $ hour , $ minute , $ second + $ microsecond / 1_000_000 ), E_USER_WARNING );
143+ }
144+ return parent ::setTime ($ hour , $ minute , $ second , $ microsecond );
145+ }
146+
147+
111148 /**
112149 * Returns JSON representation in ISO 8601 (used by JavaScript).
113150 */
@@ -134,4 +171,14 @@ public function modifyClone(string $modify = ''): static
134171 $ dolly = clone $ this ;
135172 return $ modify ? $ dolly ->modify ($ modify ) : $ dolly ;
136173 }
174+
175+
176+ private function handleErrors (string $ value ): void
177+ {
178+ $ errors = self ::getLastErrors ();
179+ $ errors = array_merge ($ errors ['errors ' ] ?? [], $ errors ['warnings ' ] ?? []);
180+ if ($ errors ) {
181+ trigger_error (self ::class . ': ' . implode (', ' , $ errors ) . " ' $ value' " , E_USER_WARNING );
182+ }
183+ }
137184}
0 commit comments