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?
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
0
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));
}
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

0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Randy PooleCommented:
yours will not process a negative time correctly btw..
0
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.
0
Randy PooleCommented:
No problem at all.
0
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

0
Randy PooleCommented:
that should work.
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.