Laravel将几秒钟转换为几小时和几分钟

发布于 2025-02-12 13:48:40 字数 1369 浏览 3 评论 0原文

我必须使用时间计算实现一些功能,并且我的应用具有以下类型的代码类型。

    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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

明明#如月 2025-02-19 13:48:40

\ Carbon \ Carbon :: Parse(date('ymd H:i:s'))将两次致电Timelib并将失去微秒,只需做\ Carbon \ Carbon ::现在(),并且您无需重新发明轮子,您可以使用以下方式获得此精确的字符串:

$t_now = \Carbon\Carbon::now();
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time);

$late_not_late = $t_allowed->diffForHumans($t_now, ['parts' => 3, 'syntax' => CarbonInterface::DIFF_ABSOLUTE]);

$ 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:

$t_now = \Carbon\Carbon::now();
$t_allowed = \Carbon\Carbon::parse($shift_details->start_time);

$late_not_late = $t_allowed->diffForHumans($t_now, ['parts' => 3, 'syntax' => CarbonInterface::DIFF_ABSOLUTE]);

$late_not_late will contain 2 days 9 hours 20 minutes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文