Oihana PHP Arango

encodeRevision.php

Table of Contents

Functions

encodeRevision()  : string
Encodes a date into an ArangoDB `_rev` (revision) string.

Functions

encodeRevision()

Encodes a date into an ArangoDB `_rev` (revision) string.

encodeRevision(string $date[, int|null $count = null ][, bool $throwable = false ]) : string

This function is the inverse of decodeRevision(). It generates a 10–11 character _rev string from a given date and an optional counter (count). The _rev string encodes a 64-bit integer composed of:

  1. A 44-bit timestamp in milliseconds since epoch (UTC).
  2. A 20-bit counter (count) to ensure uniqueness for multiple revisions at the same millisecond.

If $count is null, the function automatically increments the counter for multiple calls within the same millisecond, mimicking ArangoDB's internal behavior.

Parameters
$date : string

The date to encode, in any format accepted by DateTimeImmutable. Must be UTC or will be converted to UTC.

$count : int|null = null

Optional counter for revisions within the same millisecond. If null, automatically incremented for multiple calls in the same millisecond. Must be between 0 and 1048575 (2^20 - 1).

$throwable : bool = false

If true, throws exceptions on error; if false, returns an empty string.

Tags
throws
RuntimeException

If the GMP extension is not loaded and $throwable is true.

InvalidArgumentException

If the date is invalid or $count is out of range and $throwable is true.

example

Example usage with explicit count:

$date = '2025-01-20T15:28:40.830Z';
$count = 0;
$rev = encodeRevision($date, $count);
echo $rev; // e.g. "_jGPSg12---"

Example usage with automatic count:

$date = '2025-10-25T12:00:00.000Z';
$rev1 = encodeRevision($date);
$rev2 = encodeRevision($date); // automatically increments count
$rev3 = encodeRevision($date); // increments again
see
https://docs.arangodb.com/3.11/aql/functions/miscellaneous/#decode_rev
decodeRevision()

For decoding a _rev string back to date and count.

author

Marc Alcaraz (eKameleon)

version
1.0.0
Return values
string

Returns the encoded _rev string (10–11 characters). Returns an empty string on error if $throwable is false.

On this page

Search results