Skip to content

Instantly share code, notes, and snippets.

@lsloan
Last active March 10, 2016 15:58
Show Gist options
  • Save lsloan/15db975d5b582d196676 to your computer and use it in GitHub Desktop.
Save lsloan/15db975d5b582d196676 to your computer and use it in GitHub Desktop.
PHP DateTime Posix timestamp handling examples

This set of examples was inspired by a novice colleague wrote the code shown in example 1. The code was intended to convert a string representation of a Posix timestamp into a DateTime object, but does it very inefficiently. It converts the timestamp to a formatted date string, then to a timestamp integer, and finally to a DateTime object, with the call to the DateTime constructor converting the integer into a string prefixed with "@".

Examples 2 and 3 show the conversion can be done in fewer steps.

Because the original code wrapped these conversions in a test for is_string($startTime) test, example 4 demonstrates what would happen if the string doesn't contain what was expected. For example, a human-readable date format. Exceptions are thrown.

Example 5 shows how the code could deal with the Posix vs. human-readable timestamps by attempting to use the first format, then falling back to use the other. I'm still unsure whether it's proper to just drop the first exception if the first attempt fails. Instead, should it be thrown if the second attempt fails?

Example 6 resolves the multiple exception question by attempting to use a single DateTime constructor call to handle multiple formats of the timestamp input. It handles the possibility of the input being a Posix timestamp, either as a string or an integer, by checking it with is_numeric() and prefixing an "@" if needed. Either way, the input is converted to a string, to be safe.

<?php
ini_set('date.timezone', 'US/Eastern');
echo "1 - - - - - - - - - - - - - - - - - - -\n";
$startTime = '1456837032';
$strToTime = strtotime(date('Y-m-d H:i:s', $startTime));
$startTime = new DateTime("@$strToTime");
echo $startTime->format(DateTime::ATOM) . "\n"; // 2016-03-01T12:57:12+00:00
echo "2 - - - - - - - - - - - - - - - - - - -\n";
$startTime = '1456837032';
$strToTime = strtotime("@$startTime");
$startTime = new DateTime("@$strToTime");
echo $startTime->format(DateTime::ATOM) . "\n"; // 2016-03-01T12:57:12+00:00
echo "3 - - - - - - - - - - - - - - - - - - -\n";
$startTime = '1456837032';
$startTime = new DateTime("@$startTime");
echo $startTime->format(DateTime::ATOM) . "\n"; // 2016-03-01T12:57:12+00:00
echo "4 - - - - - - - - - - - - - - - - - - -\n";
$startTime = '2016-03-01T12:57:12+00:00';
try {
$newTime = new DateTime("@$startTime");
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n"; // This is shown
}
if ($newTime instanceof DateTime) {
echo $newTime->format(DateTime::ATOM) . "\n"; // Exception prevents output
}
echo "5 - - - - - - - - - - - - - - - - - - -\n";
$echoTime = function($startTime)
{
if (is_string($startTime)) {
try {
$startTime = new DateTime("@$startTime"); // Assume it's Posix
} catch (Exception $e) {
try {
$startTime = new DateTime($startTime); // Assume it's parseable
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
return;
}
}
}
if ($startTime instanceof DateTime) {
echo 'DateTime object: ' . $startTime->format(DateTime::ATOM) . "\n";
} else {
echo 'Other: ';
var_dump($startTime);
}
};
$echoTime('1456837032'); // DateTime object: 2016-03-01T12:57:12+00:00
$echoTime('2016-03-01T12:57:12+00:00'); // DateTime object: 2016-03-01T12:57:12+00:00
$echoTime(1456837032); // Other: int(1456837032)
$echoTime('fubar'); // Exception thrown
echo "6 - - - - - - - - - - - - - - - - - - -\n";
$echoTime = function($startTime)
{
try {
$startTime = new DateTime((is_numeric($startTime) ? '@' : null) . strval($startTime));
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
return;
}
if ($startTime instanceof DateTimeInterface) {
echo 'DateTime object: ' . $startTime->format(DateTime::ATOM) . "\n";
} else {
echo 'Other: ';
var_dump($startTime);
}
};
$echoTime('1456837032'); // DateTime object: 2016-03-01T12:57:12+00:00
$echoTime('2016-03-01T12:57:12+00:00'); // DateTime object: 2016-03-01T12:57:12+00:00
$echoTime(1456837032); // DateTime object: 2016-03-01T12:57:12+00:00
$echoTime('fubar'); // Exception thrown
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment