Skip to content

Instantly share code, notes, and snippets.

@fwolf
Last active August 29, 2015 14:16
Show Gist options
  • Save fwolf/5f3e44343a3bebf36953 to your computer and use it in GitHub Desktop.
Save fwolf/5f3e44343a3bebf36953 to your computer and use it in GitHub Desktop.
Given length of second and microsecond, use base36, from current time, which start date will fill the full length ?
<?php
// @see http://3v4l.org/YPTHo
$lengthOfSecond = 6;
$lengthOfMicroSecond = 4;
$length = $lengthOfSecond + $lengthOfMicroSecond;
// From start date to now, the encoded value need fill length
$minTimestamp36 = '1' . str_repeat('0', $length - 1);
$minTimestamp10 = base_convert($minTimestamp36, 36, 10);
// When compute date, remove microsecond
$startTimestamp = time() - round($minTimestamp10 / 1000000);
$startDate = date('Y-m-d H:i:s', $startTimestamp);
// End date start from that
$maxTimestamp36 = str_repeat('z', $length);
$maxTimestamp10 = base_convert($maxTimestamp36, 36, 10);
$endTimestamp = $startTimestamp + round($maxTimestamp10 / 1000000);
$endDate = date('Y-m-d H:i:s', $endTimestamp);
var_dump($maxTimestamp10);
echo "Start date: $startDate" . PHP_EOL;
echo " End date: $endDate" . PHP_EOL;
echo PHP_INT_MAX . PHP_EOL; // Need 64-bit environment for not overflow
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment