Page 1 of 1

Rounding off time

Posted: Wed Jan 03, 2007 1:50 pm
by dstefani
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello,

I am trying to round off a time value to the nearest half hour or hour.

I am calculating job times that may be anywhere from 1 to 6 hours.

For example a job may spec at 70 minutes long (4200 seconds), to make it to the next half hour I'd take it up to 5400 sec.

These intervals relate to an actual time on a job schedule as job times available.

How would I take any job duration interval and round it to the nearest half hour?

Code: Select all

$start_time = '13:00:00';    // 1:00pm
$work_time = 4200;   // need to get this to 5400

Code: Select all

SELECT SEC_TO_TIME(TIME_TO_SEC('$start_time') + '$work_time') AS 'job_end'
If I keep it to half hour increments, the starting time will always be a good calculation point, but I'll never know what the job duration time is. Somewhere down the line I need to get the job duration to equal a half hour time value before calculating the end of the job and the beginning of the next.

I think I've been looking at this too long! 8O

Thanks,

- dstefani


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Wed Jan 03, 2007 2:14 pm
by Weirdan
  1. calculate how much half hour intervals the job would take ($work_time / (60*30))
  2. round that number
  3. multiply it by 60*30

Posted: Wed Jan 03, 2007 2:26 pm
by Kieran Huggins
You need to know two things: your current amount and the size of the increment.

Code: Select all

$start_time = strtotime('13:00:00');
$end_time = strtotime('14:17:22');

$duration = $end_time - $start_time;

$increment = 30 * 60; // 30 min * 60 seconds

echo floor($duration / $increment)*$increment; // round down
echo '<hr/>';
echo ceil($duration / $increment)*$increment; // round up
echo '<hr/>';
echo round($duration / $increment)*$increment; // round to nearest

Posted: Wed Jan 03, 2007 2:28 pm
by dstefani
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Very cool, thanks.

This worked:

Code: Select all

$var1 = ceil(($work_time / 1800));
$var2 = ($var1 * (60*30));
I needed to round up in time.

Just what I needed.

Thanks again!

- dstefani
:lol:


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]