Zeitberechnung in PHP?

Hallo.

Ich versuche gerade eine Zeitberechnung in PHP. Gegeben sind 2 Input felder mit DateTime date(“Y-m-d H:i”)

Folgendes habe ich

$begin = new DateTime($start, new \DateTimeZone('UTC'));                                // Start Umwandlung in DateTime
$end = new DateTime($end, new \DateTimeZone('UTC'));                                    // Ende Umwandlung in DateTime
$end = $end->sub(new DateInterval('PT0H30M'));     // Ziehe Pause ab

$differenz = $begin->diff($end);                                // Berechne Differenz
$arbeitszeit = $differenz->format('%H:%I');         // Ausgabe in HH:ii

Damit bekomme ich die Arbeitszeit in Std:Min
Nun brauche ich aber noch die + oder – Stunden zu unserer Richtarbeitszeit von 7,5 Std.
Also bei 6:30 brauche ich – 1,00 oder bei 7 Stunden – 0,30
Wie mache ich das am besten. bin schon die ganze Zeit am Probieren mit ->sub und ->diff, aber bekomme immer falsche Werte

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
2 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
 Mirko Marek
1 month ago

Hi,

this goes relatively relaxed when you convert the initial time and the final time into Unix time, then calculate the difference which is converted in turn into hours and minutes. Here my example:


Good success 😉

regex9
1 month ago

First, revise the actual working time in minutes:

$totalMinutesDiff = $differenz->h * 60 + $differenz->i;

And then you can subtract this value with 450 (7*60 + 30).

$totalMinutesDiff - 450;

Then you have the number of minutes that have either been missing or added.