Skip to content

Instantly share code, notes, and snippets.

@themasch
Last active December 15, 2015 07:59
Show Gist options
  • Select an option

  • Save themasch/5227701 to your computer and use it in GitHub Desktop.

Select an option

Save themasch/5227701 to your computer and use it in GitHub Desktop.
stringify PHP DateInterval
<?php
function stringifyDateInterval(\DateInterval $inv)
{
$dat = 'P' . (($inv->y !== 0) ? $inv->y . 'Y' : '')
. (($inv->m !== 0) ? $inv->m . 'M' : '')
. (($inv->d !== 0) ? $inv->d . 'D' : '');
$tme = 'T' . (($inv->h !== 0) ? $inv->h . 'H' : '')
. (($inv->i !== 0) ? $inv->i . 'M' : '')
. (($inv->s !== 0) ? $inv->s . 'S' : '');
return $dat . ((strlen($tme) > 1) ? $tme : '');
}
// less code but more verbose output:
function stringifyDateInterval(\DateInterval $inv)
{
return $inv->format('P%yY%mM%dDT%hH%iM%sS');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment