php 获取PHP中两个日期时间之间的间隔秒数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1519228/
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
Get interval seconds between two datetime in PHP?
提问by Misier
2009-10-05 18:11:08
2009-10-05 18:11:08
2009-10-05 18:07:13
2009-10-05 18:07:13
This should generate 235,how to do it ?
这应该生成235,怎么做?
回答by JCM
With DateTime objects, you can do it like this:
使用 DateTime 对象,您可以这样做:
$date = new DateTime( '2009-10-05 18:07:13' );
$date2 = new DateTime( '2009-10-05 18:11:08' );
$diffInSeconds = $date2->getTimestamp() - $date->getTimestamp();
回答by Paul Dixon
You can use strtotime()to do that:
您可以使用strtotime()来做到这一点:
$diff = strtotime('2009-10-05 18:11:08') - strtotime('2009-10-05 18:07:13')
A similar approach is possible with DateTime objects, e.g.
DateTime 对象可以使用类似的方法,例如
$date = new DateTime( '2009-10-05 18:07:13' );
$date2 = new DateTime( '2009-10-05 18:11:08' );
$diff = $date2->getTimestamp() - $date->getTimestamp();
回答by Andrew
PHP Date Time reference is helpful for things like this: PHP Date Time Functions
PHP 日期时间参考对这样的事情很有帮助:PHP 日期时间函数
strtotime()is probably the best way.
strtotime()可能是最好的方法。
$seconds = strtotime('2009-10-05 18:11:08') - strtotime('2009-10-05 18:07:13')
回答by MilkThief
Because of unix epoch limitations, you could have problems compairing dates before 1970 and after 2038. I choose to loose precision (=don't look at the single second) but avoid to pass trough unix epoch conversions (getTimestamp). It depends on what you are doing to do...
由于 Unix 纪元的限制,比较 1970 年之前和 2038 年之后的日期可能会出现问题。我选择放宽精度(=不看一秒)但避免通过 Unix 纪元转换 (getTimestamp)。这取决于你在做什么...
In my case, using 365 instead (12*30) and "30" as mean month lenght, reduced the error in an usable output.
在我的情况下,使用 365 (12*30) 和“30”作为平均月长,减少了可用输出中的错误。
function DateIntervalToSec($start,$end){ // as datetime object returns difference in seconds
$diff = $end->diff($start);
$diff_sec = $diff->format('%r').( // prepend the sign - if negative, change it to R if you want the +, too
($diff->s)+ // seconds (no errors)
(60*($diff->i))+ // minutes (no errors)
(60*60*($diff->h))+ // hours (no errors)
(24*60*60*($diff->d))+ // days (no errors)
(30*24*60*60*($diff->m))+ // months (???)
(365*24*60*60*($diff->y)) // years (???)
);
return $diff_sec;
}
Note that the error could be 0, if "mean" quantities are intended for diff. The PHP docs don't speaks about this... In a bad case, error could be:
请注意,如果“平均”数量用于差异,则错误可能为 0。PHP 文档没有谈到这个......在糟糕的情况下,错误可能是:
- 0 seconds if diff is applied to time gaps < 1 month
- 0 to 3 days if diff is applied to time gaps > 1 month
- 0 to 14 days if diff is applied to time gaps > 1 year
- 如果 diff 应用于时间间隔 < 1 个月,则为 0 秒
- 如果将差异应用于时间间隔 > 1 个月,则为 0 到 3 天
- 如果将差异应用于时间间隔 > 1 年,则为 0 到 14 天
I prefer to suppose that somebody decided to consider "m" as 30 days and "y" as 365, charging "d" with the difference when "diff" walk trough non-30-days months...
我更愿意假设有人决定将“m”视为 30 天,将“y”视为 365 天,当“diff”走过非 30 天的月份时,向“d”收取差异...
If somebody knows something more about this and can provide official documentation, is welcome!
如果有人对此有更多了解并可以提供官方文档,欢迎!
回答by lxg
If you need the real local time difference and want to work with getTimestamp, you must take DST switches during the calculated period into account. Therefore, the local offset to UTC must be included in the equation.
如果您需要真实的本地时差并希望使用getTimestamp,则必须考虑计算期间的夏令时切换。因此,等式中必须包含相对于 UTC 的本地偏移量。
Take, for instance, the following dates:
以下列日期为例:
$tz = new \DateTimeZone("Europe/Berlin");
$start = new \DateTime("2018-02-01 12:00:00", $tz);
$end = new \DateTime("2018-04-01 12:00:00", $tz);
$startis "2018-02-01 11:00:00" in UTC, while $endis "2018-04-01 10:00:00" in UTC. Note that, while the time of day is the same in the Berlin timezone, it is different in UTC. The UTC offset is one hour in $startand 2 hours in $end.
$startUTC 时间为“2018-02-01 11:00:00”,UTC 时间$end为“2018-04-01 10:00:00”。请注意,虽然柏林时区中的时间相同,但 UTC 中的时间不同。UTC 偏移量是 1 小时$start和 2 小时$end。
Keep in mind that getTimestampalways returns UTC! Therefore you must subtract the offset from the timestamp when looking for the actual local difference.
请记住,getTimestamp始终返回 UTC!因此,在查找实际本地差异时,您必须从时间戳中减去偏移量。
// WRONG! returns 5094000, one hour too much due to DST in the local TZ
echo $end->getTimestamp() - $start->getTimestamp();
// CORRECT: returns 5090400
echo ($end->getTimestamp() - $end->getOffset()) - ($start->getTimestamp() - $start->getOffset());
回答by jcoder
strtotime("2009-10-05 18:11:08") - strtotime("2009-10-05 18:07:13")
回答by Arfon
A simple and exact solution (exemplifying Nilz11's comment):
一个简单而准确的解决方案(以 Nilz11 的评论为例):
$hiDate = new DateTime("2310-05-22 08:33:26");
$loDate = new DateTime("1910-11-03 13:00:01");
$diff = $hiDate->diff($loDate);
$secs = ((($diff->format("%a") * 24) + $diff->format("%H")) * 60 +
$diff->format("%i")) * 60 + $diff->format("%s");

