Page 1 of 1

Time Duration Calculation

Posted: 2016-08-14 22:01
by ronwill
Hi,

Probably an easy solution to this but beyond me, have enjoyed trying various solutions without success but really need to sort it out now LOL.

I want to add in my Table hooks folder something to calculate the difference between a start and end time:

I've done this, works fine for a larger end date but for example, returns a negative number if start = 22:00:00 and end is 08:00:00

function RotaInput_before_update(&$data, $memberInfo, &$args){

$data['HoursTot'] = $data['EndTime'] - $data['StartTime'];

return TRUE;

Can someone please put up the way to calculate a 'positive time difference between any 2 times (note the time differences will never exceed a 24 hour period... we might do long shift hours but not quite that long!)

Cheers, Ron

Re: Time Duration Calculation

Posted: 2016-08-15 14:01
by grimblefritz

Re: Time Duration Calculation

Posted: 2016-08-15 17:48
by ronwill
Hi,

Thanks for the response, I've looked at the same site link earlier. My problem is I've never done programming before (I might be a pro in excel but never used php etc. before). No matter how many attempts I've made I never get a result (most attempts leave me with a blank page). I'm willing to look for solutions and not looking just for someone else to solve a problem for me except when I really get stuck! I'm stuck now so would appreciate it if someone could just paste the code I need, in the way it should be written so I can copy it and try to understand it. All part of my learning curve!

Cheers, Ron

Re: Time Duration Calculation

Posted: 2016-08-16 02:01
by ronwill
Hi,

If anyone else having same problem, in showing a calculated date field on a time sheet (24hr format) I've solved it in this way (might not be the best but it works!):

My Fields: [StartTime] [EndTime] - both time fields
Hidden Field [Tot1] [Tot2] - decimal field
Result Field Visible: [HoursTot] - decimal field

In my hooks folder:
function RotaInput_before_update(&$data, $memberInfo, &$args){

$data['Tot1'] = strtotime($data['EndTime']) - strtotime($data['StartTime']);
$data['Tot2'] = strtotime($data['StartTime']) - strtotime($data['EndTime']);

$t = date("H/i/s");

if ($data['Tot1'] < 0) {
$data['HoursTot'] = 24 - $data['Tot2']/3600;
} else {
$data['HoursTot'] = $data['Tot1']/3600;
}
return TRUE;
}

Re: Time Duration Calculation

Posted: 2016-08-16 16:49
by grimblefritz
Ron,

See: http://grimblefritz.com/timediff/

Date and Time fields are of type Date and Time, respectively.

Elapsed field is Decimal 10,2

I used date+time fields. You might choose not to, but I prefer that have hard data rather than assumptions (such as same day, or no more than one trans-midnight event, etc.)

With a little jquery, it's possible to have the elapsed time update as the date/time fields change, but as-is it updates on save. I think that's adequate, but just want to point out that making it real time is possible.

All the source needed is available from the home page. You will, of course, have to massage the field and table names to suit your application. (You should be able to eliminate your Tot1 & Tot2 fields.)

Have fun!

Re: Time Duration Calculation

Posted: 2016-08-16 17:10
by ronwill
Am certainly having fun, well some days are .... won't go there LOL.

Thanks for your Brill example, help, guidance - first class & really appreciated.

I'll give it a go,

Cheers, Ron
P.S. sorry for double post above got a server error/busy message so clicked again & ended up sending 2x - can Admin: pls delete duplicate thks

Re: Time Duration Calculation

Posted: 2017-03-30 01:29
by ikatikah
Can you share with me how do you get the time to immediately show the current time?
I can't seem to get the timepicker to show the current time, it will show 1.00 am regardless of what I do.

Thanks in advance.