note 110877 deleted from function.strtotime by aharvey
| From: | aharvey@php.net | Date: | Wed, 19 Dec 2012 01:18:17 +0000 |
| Subject: | note 110877 deleted from function.strtotime by aharvey | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-192590@lists.php.net to get a copy of this message | ||
Note Submitter: jimmetry at gmail dot com
----
Comparing two simple times (no date) is way too much effort with strtotime, especially with the new
epoch and timezone warnings. I got tired of making the problem overcomplicated, and made this little
function instead:
<?php
/* Converts a string time duration into seconds. Supports
* hours:minutes:seconds, hours:minutes:seconds.fraction,
* hours:minutes and minutes:seconds.fraction formats.
*/
function durationInSeconds($duration) {
// Split components
$pieces = explode(':', $duration);
$numPieces = count($pieces);
// Check for hours:minutes anomaly
if ($numPieces == 3 || strpos($pieces[$numPieces - 1], '.'))
$offset = 0;
else $offset = 1;
// Sum components
$seconds = 0;
for ($i = 0, $j = $numPieces - 1; $j >= 0; $i++, $j--)
$seconds += $pieces[$i] * pow(60, $j + $offset);
return $seconds;
}
?>