Skip to content

Instantly share code, notes, and snippets.

@Shagshag
Created September 15, 2021 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Shagshag/e3506054121600588ea3069eb538663a to your computer and use it in GitHub Desktop.
Save Shagshag/e3506054121600588ea3069eb538663a to your computer and use it in GitHub Desktop.
Convert GLS track ID to GLS reference
<?php
// based on https://stackoverflow.com/questions/10471991/convertions-between-decimal-and-base-36#10472259
function convertReferenceToTrackID($number) {
$offset = 2721109907456;
$base = 36;
$in = (string) ($number + $offset);
$out = '';
for ($i = strlen($in) - 1; $i >= 0; $i--) {
$out = base_convert(bcmod($in, $base), 10, $base) . $out;
$in = bcdiv($in, $base);
}
return strtoupper(preg_replace('/^0+/', '', $out));
}
function convertTrackIDToReference($trackID) {
$offset = 2721109907456;
$base = 36;
$out = '';
for ($i = 0, $l = strlen($trackID); $i < $l; $i++) {
$x = base_convert(substr($trackID, $i, 1), $base, 10);
$out = bcadd(bcmul($out, $base), $x);
}
$out = (int) $out;
return $out - $offset;
}
var_dump(convertReferenceToTrackID(71642028539));
var_dump(convertTrackIDToReference('ZMZ0E96J'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment