Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active August 29, 2015 14:16
Show Gist options
  • Save fwolf/b5b10173b00086d5f33c to your computer and use it in GitHub Desktop.
Save fwolf/b5b10173b00086d5f33c to your computer and use it in GitHub Desktop.
With given timestamp start date, compute same length base36 encoded string can be start from or end to which date.
<?php
/**
* Compute of lifetime range for base36 encoded timestamp string
*
* Notice: Need to run in 64-bit environment
*
* @see http://3v4l.org/qDkDE
*/
$lengthOfSecond = 6;
$lengthOfMicroSecond = 4;
$precisionOfMicroSecond = 6; // 0.123456
$offsetDate = '2012-07-11';
$offsetTimestamp = strtotime($offsetDate);
echo "Offset time: $offsetDate, $offsetTimestamp" . PHP_EOL;
$length = $lengthOfSecond + $lengthOfMicroSecond - 1;
$startTimestamp = base_convert('1' . str_repeat('0', $length), 36, 10) /
pow(10, $precisionOfMicroSecond) +
$offsetTimestamp;
$startTimestamp = round($startTimestamp);
$startDate = date('Y-m-d H:i:s', $startTimestamp);
echo "Lifetime start: base36 of 10{" . "$lengthOfSecond + $lengthOfMicroSecond - 1} /" .
" 10^$precisionOfMicroSecond + offset timestamp" . PHP_EOL .
" = $startTimestamp = $startDate" . PHP_EOL;
$length = $lengthOfSecond + $lengthOfMicroSecond;
$endTimestamp = base_convert(str_repeat('z', $length), 36, 10) /
pow(10, $precisionOfMicroSecond) +
$offsetTimestamp;
$endTimestamp = round($endTimestamp);
$endDate = date('Y-m-d H:i:s', $endTimestamp);
echo "Lifetime end: base36 of z{" . "$lengthOfSecond + $lengthOfMicroSecond} /" .
" 10^$precisionOfMicroSecond + offset timestamp" . PHP_EOL .
" = $endTimestamp = $endDate" . PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment