-
Notifications
You must be signed in to change notification settings - Fork 0
/
snowflake_helper.php
25 lines (24 loc) · 1.13 KB
/
snowflake_helper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?php
//uses discord snowflake as a parameter
function snowflake_timestamp(string $snowflake)
{
if (\PHP_INT_SIZE === 4) { //x86
$binary = \str_pad(\base_convert($snowflake, 10, 2), 64, 0, \STR_PAD_LEFT);
$time = \base_convert(\substr($binary, 0, 42), 2, 10);
$timestamp = (float) ((((int) \substr($time, 0, -3)) + 1420070400).'.'.\substr($time, -3));
$workerID = (int) \base_convert(\substr($binary, 42, 5), 2, 10);
$processID = (int) \base_convert(\substr($binary, 47, 5), 2, 10);
$increment = (int) \base_convert(\substr($binary, 52, 12), 2, 10);
} else { //x64
$snowflake = (int) $snowflake;
$time = (string) ($snowflake >> 22);
$timestamp = (float) ((((int) \substr($time, 0, -3)) + 1420070400).'.'.\substr($time, -3));
$workerID = ($snowflake & 0x3E0000) >> 17;
$processID = ($snowflake & 0x1F000) >> 12;
$increment = ($snowflake & 0xFFF);
}
if ($timestamp < 1420070400 || $workerID < 0 || $workerID >= 32 || $processID < 0 || $processID >= 32 || $increment < 0 || $increment >= 4096) {
return null;
}
return $timestamp;
}