I'm using lumen and I'm not able to use Carbon. So I'm trying to do it with the date() method.
Next, I get a value like this: 00:00:00.000. Every time this value is saved in the db. When there is already such a value in the DB, I get it and add it to what is coming in the request.
For example:
In the database it has the following value - 00:10:29.000 - and what came in the request - 00:01:43.120. I need to sum these two values and output in the same H:i:s.v format. But guys, if you see a value of milliseconds, it doesn't calculate, it just inserts the zeros.
To add two time values in the format H:i:s.v, you can convert them to seconds and then add them. After that, you can convert the result back to the same format. Here's an example:
// Get the two time values
$dbTime = '00:10:29.000';
$requestTime = '00:01:43.120';
// Convert them to seconds
$dbSeconds = strtotime($dbTime) - strtotime('00:00:00.000');
$requestSeconds = strtotime($requestTime) - strtotime('00:00:00.000');
// Add the seconds
$totalSeconds = $dbSeconds + $requestSeconds;
// Convert back to the same format
$result = date('H:i:s.v', $totalSeconds);
// Output the result
echo $result; // Output: 00:12:12.120
In this example, we first convert the two time values to seconds using the strtotime() function. We subtract the value of 00:00:00.000 to get the number of seconds since midnight. We then add the two values to get the total number of seconds.
Finally, we convert the total number of seconds back to the same format using the date() function. The H:i:s.v format string specifies that we want the output to be in the format hours:minutes:seconds.milliseconds.