1--TEST-- 2Tests for DateTime[Immutable]::[get|set]Microseconds 3--FILE-- 4<?php 5 6class MyDateTime extends DateTime {}; 7class MyDateTimeImmutable extends DateTimeImmutable {}; 8 9$microsecondList = array( 10 0, 11 999999, 12 -1, 13 1000000 14); 15 16$dt = new DateTime('2023-04-05T06:07:08.901234+00:00'); 17$dti = new DateTimeImmutable('2023-04-05T06:07:08.901234+00:00'); 18$myDt = new MyDateTime('2023-04-05T06:07:08.901234+00:00'); 19$myDti = new MyDateTimeImmutable('2023-04-05T06:07:08.901234+00:00'); 20 21echo 'DateTime::getMicrosecond(): ' . var_export($dt->getMicrosecond(), true) . "\n"; 22var_dump($dt); 23 24echo 'DateTimeImmutable::getMicrosecond(): ' . var_export($dti->getMicrosecond(), true) . "\n"; 25var_dump($dti); 26 27echo 'MyDateTime::getMicrosecond(): ' . var_export($myDt->getMicrosecond(), true) . "\n"; 28var_dump($myDt); 29 30echo 'MyDateTimeImmutable::getMicrosecond(): ' . var_export($myDti->getMicrosecond(), true) . "\n"; 31var_dump($myDti); 32 33foreach ($microsecondList as $microsecond) { 34 echo "##################################\n"; 35 36 echo 'DateTime::setMicrosecond('.var_export($microsecond, true).'): '; 37 try { 38 var_dump($dt->setMicrosecond($microsecond)); 39 } catch (Throwable $e) { 40 echo get_class($e) . ': ' . $e->getMessage() . "\n"; 41 } 42 echo 'DateTime::getMicrosecond(): ' . var_export($dt->getMicrosecond(), true) . "\n"; 43 44 echo 'DateTimeImmutable::setMicrosecond('.var_export($microsecond, true).'): '; 45 try { 46 var_dump($dti->setMicrosecond($microsecond)); 47 } catch (Throwable $e) { 48 echo get_class($e) . ': ' . $e->getMessage() . "\n"; 49 } 50 echo 'DateTimeImmutable::getMicrosecond(): ' . var_export($dti->getMicrosecond(), true) . "\n"; 51 52 echo 'MyDateTime::setMicrosecond('.var_export($microsecond, true).'): '; 53 try { 54 var_dump($myDt->setMicrosecond($microsecond)); 55 } catch (Throwable $e) { 56 echo get_class($e) . ': ' . $e->getMessage() . "\n"; 57 } 58 echo 'MyDateTime::getMicrosecond(): ' . var_export($myDt->getMicrosecond(), true) . "\n"; 59 60 echo 'MyDateTimeImmutable::setMicrosecond('.var_export($microsecond, true).'): '; 61 try { 62 var_dump($myDti->setMicrosecond($microsecond)); 63 } catch (Throwable $e) { 64 echo get_class($e) . ': ' . $e->getMessage() . "\n"; 65 } 66 echo 'MyDateTimeImmutable::getMicrosecond(): ' . var_export($myDti->getMicrosecond(), true) . "\n"; 67} 68?> 69--EXPECTF-- 70DateTime::getMicrosecond(): 901234 71object(DateTime)#%d (3) { 72 ["date"]=> 73 string(26) "2023-04-05 06:07:08.901234" 74 ["timezone_type"]=> 75 int(1) 76 ["timezone"]=> 77 string(6) "+00:00" 78} 79DateTimeImmutable::getMicrosecond(): 901234 80object(DateTimeImmutable)#%d (3) { 81 ["date"]=> 82 string(26) "2023-04-05 06:07:08.901234" 83 ["timezone_type"]=> 84 int(1) 85 ["timezone"]=> 86 string(6) "+00:00" 87} 88MyDateTime::getMicrosecond(): 901234 89object(MyDateTime)#%d (3) { 90 ["date"]=> 91 string(26) "2023-04-05 06:07:08.901234" 92 ["timezone_type"]=> 93 int(1) 94 ["timezone"]=> 95 string(6) "+00:00" 96} 97MyDateTimeImmutable::getMicrosecond(): 901234 98object(MyDateTimeImmutable)#%d (3) { 99 ["date"]=> 100 string(26) "2023-04-05 06:07:08.901234" 101 ["timezone_type"]=> 102 int(1) 103 ["timezone"]=> 104 string(6) "+00:00" 105} 106################################## 107DateTime::setMicrosecond(0): object(DateTime)#%d (3) { 108 ["date"]=> 109 string(26) "2023-04-05 06:07:08.000000" 110 ["timezone_type"]=> 111 int(1) 112 ["timezone"]=> 113 string(6) "+00:00" 114} 115DateTime::getMicrosecond(): 0 116DateTimeImmutable::setMicrosecond(0): object(DateTimeImmutable)#%d (3) { 117 ["date"]=> 118 string(26) "2023-04-05 06:07:08.000000" 119 ["timezone_type"]=> 120 int(1) 121 ["timezone"]=> 122 string(6) "+00:00" 123} 124DateTimeImmutable::getMicrosecond(): 901234 125MyDateTime::setMicrosecond(0): object(MyDateTime)#%d (3) { 126 ["date"]=> 127 string(26) "2023-04-05 06:07:08.000000" 128 ["timezone_type"]=> 129 int(1) 130 ["timezone"]=> 131 string(6) "+00:00" 132} 133MyDateTime::getMicrosecond(): 0 134MyDateTimeImmutable::setMicrosecond(0): object(MyDateTimeImmutable)#%d (3) { 135 ["date"]=> 136 string(26) "2023-04-05 06:07:08.000000" 137 ["timezone_type"]=> 138 int(1) 139 ["timezone"]=> 140 string(6) "+00:00" 141} 142MyDateTimeImmutable::getMicrosecond(): 901234 143################################## 144DateTime::setMicrosecond(999999): object(DateTime)#%d (3) { 145 ["date"]=> 146 string(26) "2023-04-05 06:07:08.999999" 147 ["timezone_type"]=> 148 int(1) 149 ["timezone"]=> 150 string(6) "+00:00" 151} 152DateTime::getMicrosecond(): 999999 153DateTimeImmutable::setMicrosecond(999999): object(DateTimeImmutable)#%d (3) { 154 ["date"]=> 155 string(26) "2023-04-05 06:07:08.999999" 156 ["timezone_type"]=> 157 int(1) 158 ["timezone"]=> 159 string(6) "+00:00" 160} 161DateTimeImmutable::getMicrosecond(): 901234 162MyDateTime::setMicrosecond(999999): object(MyDateTime)#%d (3) { 163 ["date"]=> 164 string(26) "2023-04-05 06:07:08.999999" 165 ["timezone_type"]=> 166 int(1) 167 ["timezone"]=> 168 string(6) "+00:00" 169} 170MyDateTime::getMicrosecond(): 999999 171MyDateTimeImmutable::setMicrosecond(999999): object(MyDateTimeImmutable)#%d (3) { 172 ["date"]=> 173 string(26) "2023-04-05 06:07:08.999999" 174 ["timezone_type"]=> 175 int(1) 176 ["timezone"]=> 177 string(6) "+00:00" 178} 179MyDateTimeImmutable::getMicrosecond(): 901234 180################################## 181DateTime::setMicrosecond(-1): DateRangeError: DateTime::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, -1 given 182DateTime::getMicrosecond(): 999999 183DateTimeImmutable::setMicrosecond(-1): DateRangeError: DateTimeImmutable::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, -1 given 184DateTimeImmutable::getMicrosecond(): 901234 185MyDateTime::setMicrosecond(-1): DateRangeError: DateTime::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, -1 given 186MyDateTime::getMicrosecond(): 999999 187MyDateTimeImmutable::setMicrosecond(-1): DateRangeError: DateTimeImmutable::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, -1 given 188MyDateTimeImmutable::getMicrosecond(): 901234 189################################## 190DateTime::setMicrosecond(1000000): DateRangeError: DateTime::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, 1000000 given 191DateTime::getMicrosecond(): 999999 192DateTimeImmutable::setMicrosecond(1000000): DateRangeError: DateTimeImmutable::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, 1000000 given 193DateTimeImmutable::getMicrosecond(): 901234 194MyDateTime::setMicrosecond(1000000): DateRangeError: DateTime::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, 1000000 given 195MyDateTime::getMicrosecond(): 999999 196MyDateTimeImmutable::setMicrosecond(1000000): DateRangeError: DateTimeImmutable::setMicrosecond(): Argument #1 ($microsecond) must be between 0 and 999999, 1000000 given 197MyDateTimeImmutable::getMicrosecond(): 901234 198