Page 1 of 1

Calculating total hours

Posted: 2019-04-24 11:39
by mekin
Hello, I'm Mustafa and I'm from The Netherlands, first introduction for you all.
I played with AppGini Demo for several days and finally bought it and it feels great!
Ahmed is very kind and his service on replying mails is 100% fine.

I'm working on a Timesheet project, just to play around with PHP and MySQL.
I got 5 tables, one of them is workinghours, most important fields are: workingday (datum), starttime (starttijd), lunchtime (pauze), endtime (eindtijd), weeknumber (weeknummer) and totalhours (totaaltijd).
With the hooks-function I got it so far that the weeknumber is calculated, based on the workingday:

Code: Select all

 $date = new DateTime($data['Datum']);
$data['Weeknummer'] = $date->format("W");
If I calculate the totaltime without lunchtime calculation, the totaltime is fine:

Code: Select all

date_default_timezone_set("Europe/Amsterdam");
$data['Totaaltijd'] = $data['Starttijd'] && $data['Eindtijd'] ? gmdate('H:i:s', (strtotime($data['Eindtijd'])-strtotime($data['Starttijd']))) : '';
If I calculate the totaltime minus the lunchtime, then the output is always 11:19:32, based on 8 hours work minus any lunchtime:

Code: Select all

date_default_timezone_set("Europe/Amsterdam");
$data['Totaaltijd'] = $data['Starttijd'] && $data['Eindtijd'] && $data['Pauze'] ? gmdate('H:i:s' , (strtotime($data['Eindtijd'])-strtotime($data['Starttijd']))-strtotime($data['Pauze'])) : '';
Can somebody tell me what I'm doing wrong, I'm not a programmer, mostly I search and adjust code of someone else :roll:

Re: Calculating total hours

Posted: 2019-04-24 12:57
by pbottcher
Hi,
I think this is a mathematical problem. You convert the times to seconds since 1.1.1970. So if you calculate your first entry without the pauze, it works fine, but if you want to remove the pauze (which is again seconds since 1970) you remove a big number from a small number.

You would need to add the day at "00:00:00" to your result to get the correct entry. so you could add +strtotime('00:00:00') to get the correct result

Re: Calculating total hours

Posted: 2019-04-24 13:08
by mekin
Hi, thanks for the explaining, it works good now!

Code: Select all

$data['Totaaltijd'] = $data['Starttijd'] && $data['Eindtijd'] && $data['Pauze'] ? gmdate('H:i:s' , (strtotime($data['Eindtijd'])-strtotime($data['Starttijd']))-strtotime($data['Pauze'])+strtotime('00:00:00')) : '';
I must sleep over it a night to really get the point, so I understand why it works now :)