Skip to content

Instantly share code, notes, and snippets.

@mahdi-alavi
Last active September 29, 2018 09:53
Show Gist options
  • Save mahdi-alavi/30b35994013e0c803674ee81d7245d2c to your computer and use it in GitHub Desktop.
Save mahdi-alavi/30b35994013e0c803674ee81d7245d2c to your computer and use it in GitHub Desktop.
wordpress: Convert string duration to ISO 8601 duration format
/* Convert string duration to ISO 8601 duration format.
=========================================================================== */
function itl_iso8601_duration( $seconds ) {
$hours = floor( $seconds / 3600 );
$seconds = $seconds % 3600;
$minutes = floor( $seconds / 60 );
$seconds = $seconds % 60;
$iso8601 = sprintf( 'PT%s%s%s',
$hours > 0 ? ( $hours < 10 ? '0' . $hours : $hours ) . 'H' : '',
$minutes > 0 ? ( $minutes < 10 ? '0' . $minutes : $minutes ) . 'M' : '',
( $seconds < 10 ? '0' . $seconds : $seconds ) . 'S'
);
return $iso8601;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment