Last active
September 29, 2018 09:53
-
-
Save mahdi-alavi/30b35994013e0c803674ee81d7245d2c to your computer and use it in GitHub Desktop.
wordpress: Convert string duration to ISO 8601 duration format
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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