Solved

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

Posted on 2006-04-13
284 Views
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 -

Richard
0
Question by:rwinnick

LVL 5

Accepted Solution

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);

0

LVL 27

Assisted Solution

if \$seconds is your duration in seconds,then

\$increments = floor(\$seconds / 30) + (\$seconds % 30 > 0);
0

LVL 2

Assisted Solution

\$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.

<?php

\$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);

?>
0

Author Comment

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 -
Richard
0

## Featured Post

### Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.