{Carbon} Convert time in HH:MM:SS format into seconds in Laravel?

20,152

Solution 1

you could use

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->diffInMinutes()

to display the time in minute or

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->diffInSeconds() 

to display it in second

Solution 2

private function convertTimeToSecond(string $time): int
{
    $d = explode(':', $time);
    return ($d[0] * 3600) + ($d[1] * 60) + $d[2];
}

Example, after 2 year...

Solution 3

You can try this it will return only the seconds

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)->second; or

$scheduleTime =\Carbon\Carbon::createFromTimestampUTC($scheduleTimestamp)-> diffInSeconds();

Share:
20,152
Admin
Author by

Admin

Updated on March 27, 2020

Comments

  • Admin
    Admin about 4 years

    I want to convert the time which I got from $scheduleTime = Carbon::createFromTimestampUTC($scheduleTimestamp)->toTimeString(); which is currently giving me 07:32:40 now I want to convert it into seconds only using Carbon library is this possible to do so if yes then how?

  • Admin
    Admin almost 7 years
    looks like I need to go for that core thing nothing in carbon for me?
  • Sapnesh Naik
    Sapnesh Naik almost 7 years
    @BhavikBamania I'm sorry I don't have any idea on how to do it using Carbon library
  • Admin
    Admin almost 7 years
    it's fine I got the answer check my answer :)