Home > Software design >  PHP (laravel) add minutes and second too a another minutes and seconds
PHP (laravel) add minutes and second too a another minutes and seconds

Time:11-30

I trying to make a kinda jukebox online. I wanna show people the duration of whole queue.

i want something like this:

foreach ($queuelist as $song) {
        $queuelistTime = $queuelistTime   $song->duration;
    }

the fomat in te database is 00:02:53 (H:i:s)

and it need come out like: 02:53 (i:s) or more likely: 2:53

CodePudding user response:

As you know the date format you can create a function which convert the song duration into seconds:

public function time_to_seconds($duration) {
    $seconds = round($seconds);

    $output = sprintf('d:d:d', ($seconds/ 3600),($seconds/ 60 % 
    60), $seconds% 60);
}

And then you'll write:

foreach ($queuelist as $song) {
    $queuelistTime = $queuelistTime   time_to_seconds($song->duration);
}

CodePudding user response:

I hope it will helpful for you.

$queuelistTime = Carbon::createFromFormat('H:i:s', $queuelistTime);

foreach ($queuelist as $song) {
        $quelistTime->addMinutes($song->duration);
    }
  • Related