Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 894
noufalcep Avatar asked Sep 14 '17 04:09

noufalcep


People also ask

How convert seconds to hours minutes and seconds in PHP?

php //PHP program to convert seconds into //hours, minutes, and seconds $seconds = 6530; $secs = $seconds % 60; $hrs = $seconds / 60; $mins = $hrs % 60; $hrs = $hrs / 60; print ("HH:MM:SS-> " . (int)$hrs .

How do you convert hours to seconds in laravel?

$time = round($seconds); echo sprintf('%02d:%02d:%02d', ($time/3600),($time/60%60), $time%60);

How do you convert work hours to days?

Total hours divided by the employee's hours per day. For eg, an employee works 8 hours per day. Leave balance for Sick Leave shows as 32 hours. The hours to days conversion will be calculated as 32 / 8 = 4 days.


Video Answer


2 Answers

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();
like image 125
noufalcep Avatar answered Sep 28 '22 11:09

noufalcep


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";
like image 22
Hamelraj Avatar answered Sep 28 '22 09:09

Hamelraj