Rounding off time

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Rounding off time

Post 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]
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
User avatar
dstefani
Forum Contributor
Posts: 140
Joined: Sat Jan 11, 2003 9:34 am
Location: Meridian Idaho, USA

Post 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]
Post Reply