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;
Namespace
Google \ Cloud \ Core
Methods
__construct
Parameters
Name
Description
value
DateTimeInterface
The timestamp value. Use ofDateTimeImmutableis highly recommended overDateTimein 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\DateTimeInterfaceprovided in the first
argument. If provided, any precision in$valuebelow seconds
will be disregarded.
get
Get the underlying\DateTimeInterfaceimplementation.
Please note that if you provided nanoseconds when creating the timestamp,
they will not be included in this value.
Example:
$dateTime = $timestamp->get();
Returns
Type
Description
DateTimeInterface
nanoSeconds
Return the number of nanoseconds.
Example:
$nanos = $timestamp->nanoSeconds();
Returns
Type
Description
int
formatAsString
Format the value as a string.
Example:
$value = $timestamp->formatAsString();
Returns
Type
Description
string
__toString
Format the value as a string.
Returns
Type
Description
string
formatForApi
Format a timestamp for the API with nanosecond precision.
Returns
Type
Description
array
jsonSerialize
Implement JsonSerializable by returning a ISO 8601 formatted string
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[],[],null,["# Google Cloud PHP shared dependency, providing functionality useful to all components. Client - Class Timestamp (1.64.2)\n\nVersion latestkeyboard_arrow_down\n\n- [1.64.2 (latest)](/php/docs/reference/cloud-core/latest/Timestamp)\n- [1.64.1](/php/docs/reference/cloud-core/1.64.1/Timestamp)\n- [1.63.0](/php/docs/reference/cloud-core/1.63.0/Timestamp)\n- [1.62.3](/php/docs/reference/cloud-core/1.62.3/Timestamp)\n- [1.61.0](/php/docs/reference/cloud-core/1.61.0/Timestamp)\n- [1.60.1](/php/docs/reference/cloud-core/1.60.1/Timestamp)\n- [1.59.1](/php/docs/reference/cloud-core/1.59.1/Timestamp)\n- [1.58.2](/php/docs/reference/cloud-core/1.58.2/Timestamp)\n- [1.53.0](/php/docs/reference/cloud-core/1.53.0/Timestamp)\n- [1.52.11](/php/docs/reference/cloud-core/1.52.11/Timestamp)\n- [1.51.2](/php/docs/reference/cloud-core/1.51.2/Timestamp)\n- [1.50.0](/php/docs/reference/cloud-core/1.50.0/Timestamp)\n- [1.49.3](/php/docs/reference/cloud-core/1.49.3/Timestamp)\n- [1.48.1](/php/docs/reference/cloud-core/1.48.1/Timestamp) \nReference documentation and code samples for the Google Cloud PHP shared dependency, providing functionality useful to all components. Client class Timestamp.\n\nRepresents a Timestamp value.\n\nNanosecond precision is preserved by passing nanoseconds as a separate\nargument to the constructor. If nanoseconds are given, any subsecond\nprecision in the timestamp will be overridden when encoding the timestamp\nas a string.\n\nExample: \n\n use Google\\Cloud\\Core\\Timestamp;\n\n $timestamp = new Timestamp(new \\DateTime('2003-02-05 11:15:02.421827Z'));\n\n // Timestamps can be cast to strings.\n echo (string) $timestamp;\n\nNamespace\n---------\n\nGoogle \\\\ Cloud \\\\ Core\n\nMethods\n-------\n\n### __construct\n\n### get\n\nGet the underlying `\\DateTimeInterface` implementation.\n\nPlease note that if you provided nanoseconds when creating the timestamp,\nthey will not be included in this value.\n\nExample: \n\n $dateTime = $timestamp-\u003eget();\n\n### nanoSeconds\n\nReturn the number of nanoseconds.\n\nExample: \n\n $nanos = $timestamp-\u003enanoSeconds();\n\n### formatAsString\n\nFormat the value as a string.\n\nExample: \n\n $value = $timestamp-\u003eformatAsString();\n\n### __toString\n\nFormat the value as a string.\n\n### formatForApi\n\nFormat a timestamp for the API with nanosecond precision.\n\n### jsonSerialize\n\nImplement JsonSerializable by returning a ISO 8601 formatted string\n\nConstants\n---------\n\n### FORMAT\n\n Value: 'Y-m-d\\TH:i:s.u\\Z'\n\n### FORMAT_NO_MS\n\n Value: 'Y-m-d\\TH:i:s\\Z'\n\n### FORMAT_INTERPOLATE\n\n Value: 'Y-m-d\\TH:i:s.%\\s\\Z'"]]