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
Last Modified: 2008-02-07
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 -

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

    LVL 27

    Assisted Solution

    if $seconds is your duration in seconds,then

    $increments = floor($seconds / 30) + ($seconds % 30 > 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.


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


    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 -

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    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.

    732 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now