PHP: Convert Hours:Minutes to Seconds

Using PHP, how can I convert the following?
echo offset_seconds('01:00'); // 3600
echo offset_seconds('-02:30'); // -9000
echo offset_seconds('11:15'); // 40500

Open in new window

LVL 10
skijAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rgonzo1971Commented:
Hi,

maybe

function seconds($time){
$time = explode(':', $time);
return ($time[0]*3600) + ($time[1]*60) + $time[2];
}

Open in new window

Regards
Randy PooleCommented:
echo offset_seconds('01:00').'<hr>'; // 3600
echo offset_seconds('-02:30').'<hr>'; // -9000
echo offset_seconds('11:15'); // 40500

function offset_seconds($time){
$time = explode(':', $time);
//determine if the initial value is negative
$neg=intval($time[0])<0;
//calculate absolute value for each section of the array if it exists, then if it was originally a negative value, convert it to such
return ($neg?-1:1) * (abs(intval($time[0])*3600) + (sizeof($time)>1?(intval($time[1])*60):0) + (sizeof($time)>2?(intval($time[2])):0));
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
skijAuthor Commented:
Thanks.  
Rgonzo1971, your concept is good but there was a bug (undefined offset).

Randy, you code works, but was not as simple.  Here is what I will use:
function offset_seconds($time){
$time = explode(':', $time);
return ($time[0]*3600) + ($time[1]*60);
}

Open in new window

Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Randy PooleCommented:
yours will not process a negative time correctly btw..
skijAuthor Commented:
Randy, thanks for pointing that out.  My code erroneously treats the minutes as positive even when the minutes should be negative, along with the hour.    Your code, as posted, is the solution.  Thanks for circling back and pointing that out.
Randy PooleCommented:
No problem at all.
skijAuthor Commented:
Here is what I will use, based on your ideas:
<?php

echo offset_seconds('01:00'); // 3600
echo '<hr>';
echo offset_seconds('-02:30'); // -9000
echo '<hr>';
echo offset_seconds('11:15'); // 40500

function offset_seconds($time){
 $time = explode(':', $time);
 return ( ($time[0]*3600) + ($time[1]*60*( $time[0]>0?1:-1)));
}

?>

Open in new window

Randy PooleCommented:
that should work.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.