Laravel将几秒钟转换为几小时和几分钟
我必须使用时间计算实现一些功能,并且我的应用具有以下类型的代码类型。
date_default_timezone_set(auth()->user()->timezone);
$t_now = \Carbon\Carbon::parse(date('Y-m-d H:i:s'));
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time) ;
@endphp
@php
$check = $t_allowed->diffForHumans($t_now);
$search = 'after';
$dff_min = $t_allowed->diffInSeconds($t_now, true);
$init = $dff_min;
$day = floor($init / 86400);
$hours = floor(($init - $day * 86400) / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
$late_not_late = $hours . ' hours ' . $minutes . ' minutes ' . $seconds . ' seconds ';
首先,我想确认$ dff_min = $ t_lowered-> diffinseconds($ t_now,true);
是返回分钟或秒吗?符合我的知识$ dff_min
包含秒 我知道可以使用(INIT/3600)
计算时间,但是以下语句是什么意思是
$hours = floor(($init - $day * 86400) / 3600);
为什么开发人员减去$ DAY * 86400
来自$ $ $ init ?
相似我们也可以计算秒$ in Init/60,因为一分钟内有60秒,但是 跟随行的意思是什么
$minutes = floor(($init / 60) % 60);
,以及为什么他在这里使用Modulo
$seconds = $init % 60;
I have to implement some functionality using time calculation and my app has following type of code.
date_default_timezone_set(auth()->user()->timezone);
$t_now = \Carbon\Carbon::parse(date('Y-m-d H:i:s'));
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time) ;
@endphp
@php
$check = $t_allowed->diffForHumans($t_now);
$search = 'after';
$dff_min = $t_allowed->diffInSeconds($t_now, true);
$init = $dff_min;
$day = floor($init / 86400);
$hours = floor(($init - $day * 86400) / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;
$late_not_late = $hours . ' hours ' . $minutes . ' minutes ' . $seconds . ' seconds ';
first i want to confirm that $dff_min = $t_allowed->diffInSeconds($t_now, true);
is returning minutes or seconds? Acording to my knowledge $dff_min
contain seconds
i know that hours could be calculate using (init /3600)
but what is the meaning of following statement
$hours = floor(($init - $day * 86400) / 3600);
why developer subtracting $day * 86400
from $init
?
similary we also can calculate seconds by $init/60 since in one minute there are 60 seconds but
what is meaning of following line
$minutes = floor(($init / 60) % 60);
and also why he is using Modulo here
$seconds = $init % 60;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
\ Carbon \ Carbon :: Parse(date('ymd H:i:s'))
将两次致电Timelib并将失去微秒,只需做\ Carbon \ Carbon ::现在()
,并且您无需重新发明轮子,您可以使用以下方式获得此精确的字符串:$ late_not_late将包含
2天9小时20分钟
\Carbon\Carbon::parse(date('Y-m-d H:i:s'))
will call twice the timelib and will loose the microseconds, just do\Carbon\Carbon::now()
and you don't need to reinvent the wheel, you can get this exact string with:$late_not_late will contain
2 days 9 hours 20 minutes