@@ -208,12 +208,12 @@ public static function uuid6(): string
208208 public static function uuid7 (): string
209209 {
210210 [$ unixts , $ subsec ] = self ::getUnixTime ();
211- $ uhex = str_pad (dechex ($ unixts ), 9 , '0 ' , \STR_PAD_LEFT );
211+ $ uhex = substr ( str_pad (dechex ($ unixts ), 9 , '0 ' , \STR_PAD_LEFT ), - 9 );
212212 $ shex = str_pad (dechex ($ subsec ), 6 , '0 ' , \STR_PAD_LEFT );
213213 $ uhex .= sprintf (
214214 '%03s7%03s ' ,
215215 substr ($ shex , 0 , 3 ),
216- substr ($ shex , - 3 )
216+ substr ($ shex , 3 , 3 )
217217 );
218218 $ uhex .= bin2hex (random_bytes (8 ));
219219 return self ::uuidFromHex ($ uhex , 7 );
@@ -242,6 +242,28 @@ public static function equals(string $uuid1, string $uuid2): bool
242242 return self ::getBytes ($ uuid1 ) === self ::getBytes ($ uuid2 );
243243 }
244244
245+ /**
246+ * Returns Unix time from a UUID.
247+ *
248+ * @param string $uuid The UUID string
249+ * @return string Unix time
250+ */
251+ public static function getTime (string $ uuid ): ?string
252+ {
253+ $ uuid = self ::stripExtras ($ uuid );
254+ $ version = self ::getVersion ($ uuid );
255+ $ timehex = '0 ' . substr ($ uuid , 0 , 12 ) . substr ($ uuid , 13 , 3 );
256+ $ retval = null ;
257+ if ($ version === 6 ) {
258+ $ retval = substr_replace (strval (hexdec ($ timehex ) - self ::TIME_OFFSET_INT ), '. ' , -7 , 0 );
259+ } elseif ($ version === 7 ) {
260+ $ unixts = hexdec (substr ($ timehex , 0 , 10 ));
261+ $ subsec = str_pad (strval (hexdec (substr ($ timehex , 10 ))), 7 , '0 ' , \STR_PAD_LEFT );
262+ $ retval = $ unixts . '. ' . $ subsec ;
263+ }
264+ return $ retval ;
265+ }
266+
245267 /**
246268 * Returns the UUID version.
247269 *
0 commit comments