Laravel 5 时差

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34616429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:58:39  来源:igfitidea点击:

Laravel 5 Time Difference

phplaraveleloquentlaravel-5.1

提问by Dev.Wol

I have an application which on sign in records in time and on sign out records out time.

我有一个应用程序,它在登录时记录时间,在注销时记录退出时间。

My table has IN_TIME & OUT_TIME

我的桌子有 IN_TIME 和 OUT_TIME

Example of data within those columns:

这些列中的数据示例:

IN_TIME = 16:06:46

IN_TIME = 16:06:46

OUT_TIME = 16:08:07

OUT_TIME = 16:08:07

I have a controller which to my blade template file is showing all attedances for a given person, what I want to do is show the time difference between the two.

我有一个控制器,我的刀片模板文件显示给定人员的所有出席情况,我想要做的是显示两者之间的时间差。

See below my current code which currently shows 0

请参阅下面我当前显示为 0 的当前代码

**Time onsite:** {{ date('G:i', strtotime($attrec->out_time)) - date('G:i', strtotime($attrec->in_time)) }}

Is there a reason why I can't get the right figure?

有没有原因我无法得到正确的数字?

回答by Bogdan

You're converting the timestamps to formatted strings and trying to compute the difference between the strings. Instead you should compute the difference between the results of each datetotime(which return UNIX timestamps that are essentially seconds) and then format that result:

您正在将时间戳转换为格式化字符串并尝试计算字符串之间的差异。相反,您应该计算每个结果之间的差异datetotime(返回本质上是秒的 UNIX 时间戳),然后格式化该结果:

{{ date('G:i', strtotime($attrec->out_time) - strtotime($attrec->in_time)) }}

You can also use Carbonby doing the following:

您还可以Carbon通过执行以下操作来使用:

{{ (new Carbon($attrec->out_time))->diff(new Carbon($attrec->in_time))->format('%h:%I') }}

This uses method diffinherited from DateTime, which returns a DateIntervalinstance. The formatting characters for DateInterval::formatare different from the ones used by date. That's why the formatting string is %h:%I, which is the equivalent of G:i.

这使用diff继承自 的方法DateTime,该方法返回一个DateInterval实例。的格式化字符与 使用DateInterval::format的不同date。这就是格式化字符串是 的原因%h:%I,它相当于G:i.

The above code will output:

上面的代码会输出:

0:01

Because you chose to format the hour without leading zeros, and the difference between the two timestamps is one minute (and some spare seconds that are not shown because they are not included in the formatting string).

因为您选择格式化没有前导零的小时,并且两个时间戳之间的差异是一分钟(以及一些未显示的备用秒,因为它们未包含在格式字符串中)。

回答by Ieuan Stanley

I may be wrong, but it's probably that arithmetic operations are undefined on the output of date. I'm not familiar with PHP but bash for instance will apply a numeric value of "0" to anything that's not a number that you try to add. So if you did

我可能是错的,但很可能是算术运算在date. 我不熟悉 PHP,但例如 bash 会将数值“0”应用于任何不是您尝试添加的数字的内容。所以如果你做了

"hello" + 5

you'd get 5. In this case you'd have two objects evaluated as 0 giving 0 + 0 = 0

你会得到 5。在这种情况下,你有两个对象被评估为 0 给 0 + 0 = 0

There's a question Herethat addresses time arithmetic in PHP. Hopefully that will set you on the best way to achieve what you want!

有一个问题Here解决了 PHP 中的时间算法。希望这能让您找到实现您想要的最佳方式!