Skip to content

Instantly share code, notes, and snippets.

@wtnabe
Created October 2, 2008 20:27
Show Gist options
  • Save wtnabe/14435 to your computer and use it in GitHub Desktop.
Save wtnabe/14435 to your computer and use it in GitHub Desktop.
Convert RFC1123-style time differences to N-seconds (PHP)
/**
* Convert RFC1123-style time differences to N-seconds
*
* If unixtime is given, convert first it to RFC1123-style time
* differences by date( "O" ).
*
* @since 2008-10-03
* @param string
* @return int
*/
function td2sec( $str ) {
if ( preg_match( '/\A[0-9]+\z/', $str ) > 0 ) {
$str = date( "O", $str );
}
if ( preg_match( '/\A([+-])([0-9]{2})([0-9]{2})\z/', $str, $match ) > 0 ) {
return sprintf( "%s%d",
$match[1],
(int)$match[2] * 3600 + (int)$match[3] * 60 );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment