public static function generateMongoId ($iTimestamp = 0, $bBase32 = true)
    {
        static $inc = 0;
        if ($inc === 0) {
            // set with microseconds:
            $inc = (int)substr(microtime(), 2, 6);
        }

        $bin = sprintf(
            '%s%s%s%s',
            pack('N', $iTimestamp ?: time()),
            substr(md5(gethostname()), 0, 3),
            pack('n', posix_getpid()),
            substr(pack('N', $inc++), 1, 3)
        );

        return $bBase32 ? strtolower(Base32::encodeByteStr($bin, true)) : bin2hex($bin);
    }
public static function generateMongoId ($iTimestamp = 0, $bBase32 = true)
    {
        static $inc = 0;
        if ($inc === 0) {
            // set with microseconds:
            $inc = (int)substr(microtime(), 2, 6);
        }

        $bin = sprintf(
            '%s%s%s%s',
            pack('N', $iTimestamp ?: time()),
            substr(md5(gethostname()), 0, 3),
            pack('n', posix_getpid()),
            substr(pack('N', $inc++), 1, 3)
        );

        return $bBase32 ? strtolower(Base32::encodeByteStr($bin, true)) : bin2hex($bin);
    }
public static function generateMongoId ( $ iTimestamp = 0 , $ bBase32 = true ) { static $ inc = 0 ; if ( $ inc === 0 ) { // set with microseconds: $ inc = ( int ) substr ( microtime ( ) , 2 , 6 ) ; } $ bin = sprintf ( '%s%s%s%s' , pack ( 'N' , $ iTimestamp ? : time ( ) ) , substr ( md5 ( gethostname ( ) ) , 0 , 3 ) , pack ( 'n' , posix_getpid ( ) ) , substr ( pack ( 'N' , $ inc ++ ) , 1 , 3 ) ) ; return $ bBase32 ? strtolower ( Base32 :: encodeByteStr ( $ bin , true ) ) : bin2hex ( $ bin ) ; }
Generates a globally unique id generator using Mongo Object ID algorithm.

The 12-byte ObjectId value consists of:
- a 4-byte value representing the seconds since the Unix epoch,
- a 3-byte machine identifier,
- a 2-byte process id, and
- a 3-byte counter, starting with a random value.
@see https://docs.mongodb.org/manual/reference/method/ObjectId/

Uses SKleeschulte\Base32 because base_convert() may lose precision on large numbers due to properties related
to the internal "double" or "float" type used.
@see http://php.net/manual/function.base-convert.php

@param  int    $iTimestamp Default: time()
@param  bool   $bBase32 Base32 (RFC 4648) or hex output?
@return string 20 base32-char or 24 hex-car MongoId.

@see https://www.ietf.org/rfc/rfc4648.txt
@see http://stackoverflow.com/questions/14370143/create-mongodb-objectid-from-date-in-the-past-using-php-driver
Generates a globally unique id generator using Mongo Object ID algorithm .