Timestamp Templating in PHP

The Timestamp field allows content writers to add a date and time.

Get the Timestamp value

Timestamp value is a string format as YYYY-MM-DDTHH:MM:SS+0000
(example: 2017-02-17T15:45:00+0000).

Here is an example that retrieves the value of a Timestamp field with the API ID of timestamp.

  • php (sdk v4 or later)
  • php (sdk v3 or earlier)
Copy
<time><?= $document->data->timestamp ?></time>
// Outputs: 2017-02-17T15:45:00+0000
Copy
<time><?= $document->getTimestamp('page.timestamp')->asText() ?></time>
// Outputs: 2017-02-17T15:45:00+0000

Format the Timestamp

You can also output the Timestamp in custom formats.

Here is an example that outputs a custom format for a Timestamp field with the API ID of date_and_time.

  • php (sdk v4 or later)
  • php (sdk v3 or earlier)
Copy
<?php
use Prismic\Dom\Date;
$timestamp = Date::asDate($document->data->date_and_time);
?>

<time><?= $timestamp->format('H:i:s - d/m/Y') ?></time>
// Outputs: 15:45:00 - 17/02/2017
Copy
<?php
$timestamp = $document->getTimestamp('page.date_and_time')->asDateTime();
?>

<time><?= $timestamp->format('H:i:s - d/m/Y') ?></time>
// Outputs: 15:45:00 - 17/02/2017