Google Cloud PHP shared dependency, providing functionality useful to all components. Client - Class Timestamp (1.48.1)

Reference documentation and code samples for the Google Cloud PHP shared dependency, providing functionality useful to all components. Client class Timestamp.

Represents a Timestamp value.

Nanosecond precision is preserved by passing nanoseconds as a separate argument to the constructor. If nanoseconds are given, any subsecond precision in the timestamp will be overridden when encoding the timestamp as a string.

Example:

use Google\Cloud\Core\Timestamp;

$timestamp = new Timestamp(new \DateTime('2003-02-05 11:15:02.421827Z'));
// Timestamps can be cast to strings.
echo (string) $timestamp;

Methods

__construct

Parameters
NameDescription
value DateTimeInterface

The timestamp value. Use of DateTimeImmutable is highly recommended over DateTime in order to avoid side effects.

nanoSeconds int

[optional] The number of nanoseconds in the timestamp. If omitted, subsecond precision will be obtained from the instance of \DateTimeInterface provided in the first argument. If provided, any precision in $value below seconds will be disregarded.

get

Get the underlying \DateTimeInterface implementation.

Please note that if you provided nanoseconds when creating the timestamp, they will not be included in this value.

Example:

$dateTime = $timestamp->get();
Returns
TypeDescription
DateTimeInterface

nanoSeconds

Return the number of nanoseconds.

Example:

$nanos = $timestamp->nanoSeconds();
Returns
TypeDescription
int

formatAsString

Format the value as a string.

Example:

$value = $timestamp->formatAsString();
Returns
TypeDescription
string

__toString

Format the value as a string.

Returns
TypeDescription
string

formatForApi

Format a timestamp for the API with nanosecond precision.

Returns
TypeDescription
array

jsonSerialize

Implement JsonSerializable by returning a ISO 8601 formatted string

Returns
TypeDescription
string

Constants

FORMAT

Value: 'Y-m-d\\TH:i:s.u\\Z'

FORMAT_NO_MS

Value: 'Y-m-d\\TH:i:s\\Z'

FORMAT_INTERPOLATE

Value: 'Y-m-d\\TH:i:s.%\\s\\Z'