[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 288
  • Last Modified:

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 -

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

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 -

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now