Second zu Zeitumwandlung robuster gegen ungültige Zahlen gemacht

This commit is contained in:
2025-10-29 11:58:00 +01:00
parent df620a4897
commit bc76739320
2 changed files with 39 additions and 8 deletions

View File

@@ -263,10 +263,21 @@ foreach ($data_output as $packingjob) {
}
}
function getTimeFromSeconds(string $timestring) {
$hours = floor($timestring / 3600);
$mins = floor($timestring / 60 % 60);
$secs = floor($timestring % 60);
$timeFormat = sprintf('%02d:%02d', $hours, $mins);
return $timeFormat;
function getTimeFromSeconds($seconds): string {
if ($seconds === null || $seconds === '') {
return '';
}
// robust normalisieren: String/Float -> Float -> gerundet -> Int
$seconds = (int) round((float) $seconds);
if ($seconds < 0) {
$seconds = 0; // oder: return ''; je nach gewünschtem Verhalten
}
// reine Integer-Arithmetik, keine impliziten float->int Casts
$hours = intdiv($seconds, 3600);
$mins = intdiv($seconds % 3600, 60);
return sprintf('%02d:%02d', $hours, $mins);
}

View File

@@ -295,15 +295,35 @@ function echoMarkedTimeLine(?string $dateStr, ?int $timeSeconds, DateTimeImmutab
echo $cls ? "</span>" : "";
}
/*
function getTimeFromSeconds(string $timestring) {
$hours = floor($timestring / 3600);
$mins = floor($timestring / 60 % 60);
$secs = floor($timestring % 60);
$mins = floor(round($timestring / 60 % 60));
$secs = floor(round($timestring % 60));
$timeFormat = sprintf('%02d:%02d', $hours, $mins);
return $timeFormat;
}*/
function getTimeFromSeconds($seconds): string {
if ($seconds === null || $seconds === '') {
return '';
}
// robust normalisieren: String/Float -> Float -> gerundet -> Int
$seconds = (int) round((float) $seconds);
if ($seconds < 0) {
$seconds = 0; // oder: return ''; je nach gewünschtem Verhalten
}
// reine Integer-Arithmetik, keine impliziten float->int Casts
$hours = intdiv($seconds, 3600);
$mins = intdiv($seconds % 3600, 60);
return sprintf('%02d:%02d', $hours, $mins);
}
?>