Divide duration in seconds by 30 second increments keeping track of increments and adding 1 for remainder less than 30

HI -

I have a feeling that this should be very straight forward for someone who know what they're doing - but am very new to PHP and -

Been trying to figure out how to make this work - can get pieces but don't seem to be able to put the whole thing together - Can get the subtraction working but having a terrible time keeping track of the increments and handling the remainer -

The problem is as follows:

I have a field that contains the length of a phone call in seconds -
I need to divide this total duration into 30 second increments keeping track of how many increments and adding one additional increment for any remainder less than 30 seconds -

In other words if the duration is 63 seconds I somehow need to come up with 3 increments - 30+30+3

Any help gratefully appreciated -

Who is Participating?

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

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.

To get the number of full 30-second periods, use division and convert to an int, which drops the fraction.  The remainder is simply modulo 30.  If you want the total number of periods including the fractional part, you can conditionally add 1 if the remainder is greater than 0.

$full_periods = (int) ($seconds / 30);
$remainder = $seconds % 30;
$total_periods = $full_periods + (($remainder > 0) ? 1 : 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
Cornelia YoderArtistCommented:
if $seconds is your duration in seconds,then

$increments = floor($seconds / 30) + ($seconds % 30 > 0);
$time is the variable which stores the length of the phone call in seconds.  This will create an array consisting of the increments you want.


$time = 346;

$num_increments = (int) ($time/30);

$increments = array();
if ($num_increments > 0)
    $increments = array_fill(0, $num_increments, 30);

if (($time % 30) > 0)
    $increments[] = ($time % 30);

rwinnickAuthor Commented:
Hi Everyone -
This really helps -
All answers really help me understand what's going on and give me something to think about -
Split points by order of answer -
Hope this is OK -
Still new to using EE -
Thanks again -
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

From novice to tech pro — start learning today.