+ #PHP >= 5.2 can handle dates before 1970 or after 2038 using the DateTime object
+ #PHP < 5.2 is limited to dates between 1970 and 2038
+
+ $invalidTime = false;
+
+ if ( class_exists( DateTime ) ) { #PHP >= 5.2
+ try { #the DateTime constructor must be used because it throws exceptions when errors occur, whereas date_create appears to just output a warning that can't really be detected from within the code
+ if ( $date !== '' ) {
+ $dateObject = new DateTime( $date );
+ } else {
+ $dateObject = new DateTime(); #use current date and time
+ }
+
+ if ( $local ) {
+ if ( isset( $wgLocaltimezone ) ) { #convert to MediaWiki local timezone if set
+ $dateObject->setTimeZone( new DateTimeZone( $wgLocaltimezone ) );
+ } #otherwise leave in PHP default
+ } else {
+ #if local time was not requested, convert to UTC
+ $dateObject->setTimeZone( new DateTimeZone( 'UTC' ) );