How to convert seconds into days hours minutes in Laravel [duplicate]
Asked Answered
P

5

24

I want to convert time interval in seconds into days hours minutes. I have tried this.

$value = '90060';
CarbonInterval::seconds($value)->forHumans();

I got the output

90060 seconds

My expected output is

1 day 1 hour 1 minute

How can I get the output using Carbon or CarbonInterval

Prut answered 14/9, 2017 at 4:41 Comment(5)
this may help #36685469Puckery
There's plenty of examples in the documentationSpermatogonium
Check this carbon.nesbot.com/docs/#api-differencePhalanstery
Try https://mcmap.net/q/86593/-convert-seconds-to-hour-minute-second/3918473Indican
{{ date('Y/m/d H:i:s',90060) }}, How about this ?Margay
P
51

I got a solution.

$value = '90060';
$dt = Carbon::now();
$days = $dt->diffInDays($dt->copy()->addSeconds($value));
$hours = $dt->diffInHours($dt->copy()->addSeconds($value)->subDays($days));
$minutes = $dt->diffInMinutes($dt->copy()->addSeconds($value)->subDays($days)->subHours($hours));
echo CarbonInterval::days($days)->hours($hours)->minutes($minutes)->forHumans();

Updated Solution

CarbonInterval::seconds(90060)->cascade()->forHumans();
Prut answered 14/9, 2017 at 6:55 Comment(5)
One line with: CarbonInterval::seconds(90060)->cascade();Cytologist
@Cytologist - Almost exactly what I was after! I used CarbonInterval::seconds(90060)->cascade()->forHumans(); to get the nice formattingSuccinctorium
glad it helps keeping your code clean and beautiful! ;)Cytologist
Another solution: $time = \Carbon\Carbon::now(); echo $time->diffForHumans($time->copy()->addSeconds(120), true);Praenomen
what if I don't want the seconds?Medicaid
C
5

try this

$init = 150065;
$day = floor($init / 86400);
$hours = floor(($init -($day*86400)) / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$day:$hours:$minutes:$seconds";
Curly answered 14/9, 2017 at 5:27 Comment(2)
I used this solution, i needed the next output format: d:H:i:s, thank'sFrown
You can use php function gmdate php.net/manual/en/function.gmdate.php gmdate("H:i:s", $seconds);Sere
E
2
function dates($value) {
    $s = $ss%60;
    $m = floor(($value %3600)/60);
    $h = floor(($value %86400)/3600);
    $d = floor(($value %2592000)/86400);
    $M = floor($value /2592000);

    return "$M months, $d days, $h hours, $m minutes, $s seconds";
}
Everywhere answered 14/9, 2017 at 6:43 Comment(0)
F
1

Try this, with carbon:

$seconds = 90060;
$dt = Carbon::now()->addSecond($seconds);
$dt_old = Carbon::now();
$days = $dt->diffInDays($dt_old);
$dt = $dt->subDays($days);
$hours = $dt->diffInHours($dt_old);
$dt = $dt->subHours($hours);
$minutes = $dt->diffInMinutes($dt_old);

echo $days.'<br>';
echo $hours.'<br>';
echo $minutes.'<br>';
Fenian answered 14/9, 2017 at 6:34 Comment(0)
S
0

Try this:

$value = '90060';

        $inAMinute = 60;
        $inAnHour = 60 * $secondsInAMinute;
        $inADay = 24 * $secondsInAnHour;

        $days = floor($value / $inADay);

        $hourSeconds = $value % $inADay;
        $hours = floor($hourSeconds / $inAnHour);

        // Extract minutes
        $minuteSeconds = $hourSeconds % $inAnHour;
        $minutes = floor($minuteSeconds / $inAMinute);


        $timeValues = [];
        $sections = [
            'day' => (int)$days,
            'hour' => (int)$hours,
            'minute' => (int)$minutes,

        ];

        foreach ($sections as $name => $value){
            if ($value > 0){
                $timeValues[] = $value. ' '.$name;
            }
        }

        $d= implode(' ', $timeValues);
        dd($d);
Samaria answered 14/9, 2017 at 6:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.