May Sale! All accounts are 40% off this week.

GobssRuiz's avatar

Form H:i:s time with date() method

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.

I'm doing it this way

$strtimer= strtotime($user->quizTime)-strtotime("00:00:00.000") + strtotime($request['time']);
$user->quizTime = date("H:i:s.v", $strtimer);
0 likes
4 replies
LaryAI's avatar
Level 58

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.

1 like
GobssRuiz's avatar

@LaryAI I love the answer and explain it but it didn't work for me :/

I copied the exact same code and it still doesn't calculate the milliseconds

Snapey's avatar

You can solve the issue with simple conversion of the time parts.

function time_to_seconds($time) {
    $parts = explode(':',$time);
    $seconds = ($parts[0] * 60*60) + ($parts[1] *60) + $parts[2];
    return $seconds;
}

function seconds_to_hisv($seconds)
{
  $hours = floor($seconds/60/60);
  $remainder = $seconds-$hours*60*60;
  $minutes = floor($remainder/60);
  $seconds = $remainder-$minutes*60;
  return sprintf('%02d:%02d:%03g', $hours, $minutes, $seconds);
}

$dbTime = '00:15:29.030';
$requestTime = '00:02:59.032';

$total = time_to_seconds($dbTime) + time_to_seconds($requestTime);

seconds_to_hisv($total);  // "00:18:28.062"

However, this has the problem of not affecting the date if the time happens to roll over to more than 24 hours.

I'm not sure if this is applicable without more context

The same problem solved with Carbon and CarbonInterval

$dbTime = '00:15:29.030';
$requestTime = '00:02:59.032';

$date1 = Carbon\Carbon::createFromFormat('H:i:s.v',$dbTime);
$date2 = Carbon\CarbonInterval::createFromFormat('H:i:s.v',$requestTime);

$final = $date1->add($date2); //00:18:28.062

Please or to participate in this conversation.