Sample code for 30+ languages & platforms
PHP

How to Parse a TimeStamp (such as 2016-11-11T14:32:17.0908971Z)

Timestamps are frequently used in REST API responses. This example demonstrates how to parse a timestamp string to get at the date/time components in the local timezone or in the GMT/UTC timezone.

Chilkat PHP Downloads

Chilkat2 PHP
<?php

use Chilkat2\DateTime;
use Chilkat2\DtObj;

// Let's say we have a timestamp string such as 2016-11-11T14:32:17.0908971Z
$strTimestamp = '2016-11-11T14:32:17.0908971Z';

$dateTime = new DateTime();
$dateTime->SetFromTimestamp($strTimestamp);

// Get a DtObj in the local timezone.
$bLocalTimezone = true;
$dt = new DtObj();
$dateTime->ToDtObj($bLocalTimezone, $dt);

// Get the individual date/time components
echo "-- Local Time --\n";
echo 'Year: ', $dt->Year, "\n";
echo 'Month: ', $dt->Month, "\n";
echo 'Day: ', $dt->Day, "\n";
echo 'Hour: ', $dt->Hour, "\n";
echo 'Minutes: ', $dt->Minute, "\n";
echo 'Seconds: ', $dt->Second, "\n";

// Get a DtObj in the GMT/UTC timezone.
$bLocalTimezone = false;
$dateTime->ToDtObj($bLocalTimezone, $dt);

// Get the individual date/time components
echo "-- UTC Time --\n";
echo 'Year: ', $dt->Year, "\n";
echo 'Month: ', $dt->Month, "\n";
echo 'Day: ', $dt->Day, "\n";
echo 'Hour: ', $dt->Hour, "\n";
echo 'Minutes: ', $dt->Minute, "\n";
echo 'Seconds: ', $dt->Second, "\n";