0

I would like to find total time spent on a project given that the user enters the "start time" and "stop time". I have been able to access the time spent but it comes as an array of Date Interval.

I want to just format the results to "H:i:s" (Hour:Minute:Seconds)

Here's the code from my controller

I have already declare the use of Carbon Class(use Carbon/Carbon;) at the top of the controller

    $start = Carbon::parse($request->strt_time);
    $end = Carbon::parse($request->stp_time);
    $time_spent = $end->diff($start);

    $spent_time = $time_spent->format('H:i:s');

I expect the output to be 00:00:00 but I am getting a string "H:i:s"

Micsedinam
  • 31
  • 1
  • 1
  • 6

2 Answers2

1

From the Carbon documentation:

Difference

As Carbon extends DateTime it inherit its methods such as diff() that take a second date object as argument and returns a DateInterval instance.

We also provide diffAsCarbonInterval() act like diff() but returns a CarbonInterval instance. Check CarbonInterval chapter for more information.

So, as Akash suggested, you could do:

$spent_time = $end->diff($start)->format('%H:%i:%s');

Why the % is prefixed in every variable? As @aynber pointed out, the documentation states:

Each format character must be prefixed by a percent sign (%).

Another option is to make use of the gmdate() helper:

$duration = $end->diffInSeconds($start);
$spent_time = gmdate('H:i:s', $duration);

or just:

$spent_time = gmdate('H:i:s', $end->diffInSeconds($start));
Kenny Horna
  • 13,485
  • 4
  • 44
  • 71
  • It's just `H:i:s` in `\Carbon\Carbon`. ie. `\Carbon\Carbon::parse($event['start'])->timezone('Australia/Perth')->format('H:i:s');` – Grant May 10 '22 at 11:30
0

diff() method gives CarbonInterval, which inherits the format function from DateInterval. The documentation states that Each format character must be prefixed by a percent sign (%)

 DateInterval::format ( string $format ) : string


 $january = new DateTime('2010-01-01');
 $february = new DateTime('2010-02-01');
 $interval = $february->diff($january);

 // %a will output the total number of days.
 echo $interval->format('%a total days')."\n";

 // While %d will only output the number of days not already covered by the
 // month.
 echo $interval->format('%m month, %d days');

so final solution is

$end->diff($start)->format('%H:%i:%s');
Akash Kumar Verma
  • 3,185
  • 2
  • 16
  • 32
  • 2
    Please add some explanation to your code such that others can learn from it. I see no difference to the original code – Nico Haase May 09 '19 at 16:12
  • 2
    This answer is correct, but lacks the explanation. `$time_spent` is a CarbonInterval, which inherits the format function from DateInterval. [The documentation](https://www.php.net/manual/en/dateinterval.format.php) states that `Each format character must be prefixed by a percent sign (%).` – aynber May 09 '19 at 16:15
  • Many thanks everyone... It worked now... I really appreciate... – Micsedinam May 09 '19 at 20:46
  • @Micsedinam if it works please accept this answer. – Akash Kumar Verma May 10 '19 at 04:48
  • 1
    @AkashKumarVerma Please add some explanation to your code such that others can learn from it – Nico Haase May 10 '19 at 12:11