I'm trying to figure out how I can take two date time strings that are stored in our database and convert it to a difference in time format of hh:mm:ss.
I looked at diffForHumans
, but that does give the format I'd like and returns things like after
, ago
, etc; which is useful, but not for what I'm trying to do.
The duration will never span days, only a max of a couple hours.
$startTime = Carbon::parse($this->start_time);
$finishTime = Carbon::parse($this->finish_time);
$totalDuration = $finishTime->diffForHumans($startTime);
dd($totalDuration);
// Have: "21 seconds after"
// Want: 00:00:21
$totalDuration = $finishTime->diffForHumans($startTime, true); dd($totalDuration);
Should return "21 seconds". I know that still isn't the format you were looking for, but that is how you remove things like "after" and "ago", as referenced in your question. – Informant