php 计算php中2个时间戳之间的差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40905174/
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
calculate the difference between 2 timestamps in php
提问by user6582683
I am using 2 timestamp in my table which is starttime datatype- timestamp and as current timestamp. endtime datatype-timestamp and default as 0000-00-00 00:00:00
我在我的表中使用了 2 个时间戳,它是 starttime 数据类型时间戳和当前时间戳。结束时间数据类型时间戳和默认为 0000-00-00 00:00:00
how to calculate the difference between 2 timestamps in php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06
如何计算php starttime:2016-11-30 03:55:06 endtimetime: 2016-11-30 11:55:06中2个时间戳之间的差异
回答by Learner
Any procedural way should be avoided. Use OOP method for date time difference:
应避免任何程序方式。使用 OOP 方法获取日期时差:
$datetime1 = new DateTime('2016-11-30 03:55:06');//start time
$datetime2 = new DateTime('2016-11-30 11:55:06');//end time
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y years %m months %d days %H hours %i minutes %s seconds');//00 years 0 months 0 days 08 hours 0 minutes 0 seconds
You can setup difference format as per your needs.
您可以根据需要设置差异格式。
%Y - use for difference in year %m - use for difference in months %d - use for difference in days %H - use for difference in hours %i - use for difference in minutes %s - use for difference in seconds
%Y - use for difference in year %m - use for difference in months %d - use for difference in days %H - use for difference in hours %i - use for difference in minutes %s - use for difference in seconds
You can remove any of above values as per your need. For example if you only are interested in hour difference and you know that difference cant be more than 24 hours then use only %H
.
您可以根据需要删除上述任何值。例如,如果您只对时差感兴趣,并且您知道时差不能超过 24 小时,则仅使用%H
.
If you want to have total difference in seconds then you can use:
如果你想在几秒钟内有完全的差异,那么你可以使用:
echo $difference_in_seconds = strtotime('2016-11-30 11:55:06') - strtotime('2016-11-30 03:55:06');//28800
Depends upon your need and the final format in which you want to have time difference.
取决于您的需要和您想要具有时差的最终格式。
For reference check: http://php.net/manual/en/datetime.diff.php
参考检查:http: //php.net/manual/en/datetime.diff.php
I hope it helps
我希望它有帮助
回答by bio_sprite
You can convert your timestamps to unix timestamp (time in seconds) using php strtotime and then take the difference. You now have the difference in time in seconds and can convert to what you need...hours, min, days
您可以使用 php strtotime 将时间戳转换为 unix 时间戳(以秒为单位的时间),然后取差异。您现在拥有以秒为单位的时间差异,并且可以转换为您需要的时间......小时、分钟、天
http://php.net/manual/en/function.strtotime.php
http://php.net/manual/en/function.strtotime.php
ex:
前任:
$ts1 = strtotime($start);
$ts2 = strtotime($end);
$seconds_diff = $ts2 - $ts1;
$time = ($seconds_diff/3600);
回答by AlexKh
The simplest answer(for me of course) is here
最简单的答案(当然对我来说)在这里
function dateDifference($date_1 , $date_2 , $differenceFormat = '%a' )
{
$datetime1 = date_create($date_1);
$datetime2 = date_create($date_2);
$interval = date_diff($datetime1, $datetime2);
return $interval->format($differenceFormat);
}
In this case date_create() function creates the DateTime object
在这种情况下 date_create() 函数创建 DateTime 对象
回答by Praveen Bhagat
try this code, tested on phpfiddle.org :-
试试这个代码,在 phpfiddle.org 上测试过:-
function timestampdiff($qw,$saw)
{
$datetime1 = new DateTime("@$qw");
$datetime2 = new DateTime("@$saw");
$interval = $datetime1->diff($datetime2);
return $interval->format('%Hh %Im');
}
echo timestampdiff('1524794340', '1524803100');