Adding 24hr time to get decimal result

Ok 1 more question for you bofins today :slight_smile:

A form I have has the following fields

Start Time {st} “time”
Finish Time {ft} “time”
Overtime {ot} “int”
Overtime x2 {ot2} “int”
Total {t} “int”

The start and finish times use the timepicker function which is saved as 23:30 (11:30pm) so 24hr or mitilary time whatever you want to call it and the other fields are just standard 2 decimal place numbers eg: ot=2.25, I may look at making this a dropdown or similar formatted field???

What I need to work out is how to convert this military time to a decimal and add the final 2 fields together before giving a final result all on the fly if possible… I currently have a onchange {t}={ot2}+{ot} working but the time conversion part is confusing.

I’m not that experianced in any form of programming so a laymans explanation would be appreciated.

First of all, your fields {t}, {ot} and {ot2} have to be of type decimal with precision 2, NOT integer.
There is not much of a conversion, just a division of the minutes by 60 to get the decimal value of an hour.
My suggestion is using the php date_diff() function (please read!).

http://php.net/manual/en/function.date-diff.php

Here we go:


$start = date_create({st}); //create a php datetime object
$finish = date_create({ft});
$t_diff = date_diff($start, $finish); //calculate the difference
{t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up

jsb

thats awesome mate thanks, exactly what I was after… the only issue I have is adding up past midnight, for example if a employee starts at 23:00 11pm and finished at 03:00 = 4 hrs yet the code returns 20, would this need a if statement for this condition?

Format your start and finish fields as datetime not just time.
Or you can do something like:


if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}

But this only works on time differences < 24:00 hours
jsb

[QUOTE=jsbinca;37341]Format your start and finish fields as datetime not just time.
Or you can do something like:


if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}

But this only works on time differences < 24:00 hours
jsb[/QUOTE]

Thank again mate but neither work,

  1. setting as datetime is ok for the db but not to reflect it on the form via onchange, besides I like the timepicker!

if($finish < $start)
{
$finish->add(new DateInterval(‘P1D’));
}

dosent work as P1D = 24hrs so we’re back at the original problem, I did find “PT12H” but that didn’t work either.

I hate this time thing

Why? It’s nothing different than any other stuff. Just keep a cool head.
This does work.


$start = date_create({t1});
$finish = date_create({t2});
if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}
$t_diff = date_diff($start, $finish); //calculate the difference
{t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up

jsb

[QUOTE=jsbinca;37346]Why? It’s nothing different than any other stuff. Just keep a cool head.
This does work.


$start = date_create({t1});
$finish = date_create({t2});
if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}
$t_diff = date_diff($start, $finish); //calculate the difference
{t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up

jsb[/QUOTE]

Nope it’s not working as I dont get a result at all, try adding up 23:00 - 01:00, works without the if statement apart from not adding up past 24hrs

Er…Um… Sorry

It does work, I forgot to modify the vars to suit my app on the if statement.

Thanks again JSB, lets hope I can figure the rest of the application out from now on

NVM I need to open my eyes… 12hrs of debugging

A little more help on this subject would be wonderfull, I’m not a programmer but understand a little.

This

$start = date_create({t1});
$finish = date_create({t2});
if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}
$t_diff = date_diff($start, $finish); //calculate the difference
{t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2}; //add everything up

Works perfectly but I’ve been asked if we can get it to round to the nearest 1/4hr interval in 100min clock format eg; 01:00, 01:25, 01:50, 01:75 etc, currently 09:05 and 15:25 = 6.33 where I’d like it to add up to 6.25

I’ve read the PHP Round function http://php.net/manual/en/function.round.php but just cant figure how it would best fit in the above.

Thanks again

Round({t}* 4) / 4

jsb

Thanks again JSB but I cant figure out wherte the above snippet fits within the code, I’ve tried several places I would expect it to go yet the result remains 6.33

It would be easier if I could modify the timepicker to only allow the selection of 15min increments 00, 15, 30, 45 on the minute side

Still need help with this… I cant for the life of me work out where that code goes

Right there


$start = date_create({t1});
$finish = date_create({t2});
if($finish < $start)
{
	$finish->add(new DateInterval('P1D'));
}
$t_diff = date_diff($start, $finish);
{t} = $t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2};
{t} = round({t}*4)/4;
// or as a one-liner {t} = round(($t_diff->h + round($t_diff->i/60,2) + {ot} + {ot2})*4)/4;

jsb