如何在Laravel中将秒转换为几小时分钟

时间:2017-09-14 04:41:40

标签: php laravel php-carbon

我想将时间间隔(以秒为单位)转换为天数小时。我试过这个。

[FormBody] string value

我得到了输出

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

我的预期输出是

90060 seconds

如何使用1 day 1 hour 1 minute Carbon

获取输出

5 个答案:

答案 0 :(得分:3)

我得到了一个解决方案。

$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();

更新了解决方案

CarbonInterval::seconds(90060)->cascade()->forHumans();

答案 1 :(得分:1)

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";
}

答案 2 :(得分:0)

试试这个:

$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);

答案 3 :(得分:0)

尝试使用碳:

$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>';

答案 4 :(得分:-1)

试试这个

$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";