Calculating total hours

If you're a new user of AppGini, feel free to ask general usage questions, or look for answers here.
Post Reply
mekin
Veteran Member
Posts: 40
Joined: 2019-04-22 17:09

Calculating total hours

Post by mekin » 2019-04-24 11:39

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:

pbottcher
AppGini Super Hero
AppGini Super Hero
Posts: 1635
Joined: 2018-04-01 10:12

Re: Calculating total hours

Post by pbottcher » 2019-04-24 12:57

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
Any help offered comes with the best of intentions. Use it at your own risk. In any case, please make a backup of your existing environment before applying any changes.

mekin
Veteran Member
Posts: 40
Joined: 2019-04-22 17:09

Re: Calculating total hours

Post by mekin » 2019-04-24 13:08

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 :)

Post Reply