计算 PHP DateInterval 中的总秒数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3176609/
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 total seconds in PHP DateInterval
提问by efritz
What is the best way to calculate the total number of seconds between two dates? So far, I've tried something along the lines of:
计算两个日期之间总秒数的最佳方法是什么?到目前为止,我已经尝试了以下方面的内容:
$delta = $date->diff(new DateTime('now'));
$seconds = $delta->days * 60 * 60 * 24;
However, the daysproperty of the DateInterval object seems to be broken in the current PHP5.3 build (at least on Windows, it always returns the same 6015value). I also attempted to do it in a way which would fail to preserve number of days in each month (rounds to 30), leap years, etc:
但是,daysDateInterval 对象的属性在当前的 PHP5.3 版本中似乎被破坏了(至少在 Windows 上,它总是返回相同的6015值)。我还尝试以一种无法保留每个月的天数(四舍五入到 30)、闰年等的方式进行操作:
$seconds = ($delta->s)
+ ($delta->i * 60)
+ ($delta->h * 60 * 60)
+ ($delta->d * 60 * 60 * 24)
+ ($delta->m * 60 * 60 * 24 * 30)
+ ($delta->y * 60 * 60 * 24 * 365);
But I'm really not happy with using this half-assed solution.
但我真的不满意使用这种半途而废的解决方案。
回答by Ben
Could you not compare the time stampsinstead?
你不能比较时间戳吗?
$now = new DateTime('now');
$diff = $date->getTimestamp() - $now->getTimestamp()
回答by dave1010
This function allows you to get the total duration in seconds from a DateInterval object
此函数允许您从 DateInterval 对象中获取以秒为单位的总持续时间
/**
* @param DateInterval $dateInterval
* @return int seconds
*/
function dateIntervalToSeconds($dateInterval)
{
$reference = new DateTimeImmutable;
$endTime = $reference->add($dateInterval);
return $endTime->getTimestamp() - $reference->getTimestamp();
}
回答by xil3
You could do it like this:
你可以这样做:
$currentTime = time();
$timeInPast = strtotime("2009-01-01 00:00:00");
$differenceInSeconds = $currentTime - $timeInPast;
time() returns the current time in seconds since the epoch time (1970-01-01T00:00:00), and strtotime does the same, but based on a specific date/time you give.
time() 返回自纪元时间 (1970-01-01T00:00:00) 以来的当前时间(以秒为单位),而 strtotime 执行相同的操作,但基于您提供的特定日期/时间。
回答by hoofuz
static function getIntervalUnits($interval, $unit)
{
// Day
$total = $interval->format('%a');
if ($unit == TimeZoneCalc::Days)
return $total;
//hour
$total = ($total * 24) + ($interval->h );
if ($unit == TimeZoneCalc::Hours)
return $total;
//min
$total = ($total * 60) + ($interval->i );
if ($unit == TimeZoneCalc::Minutes)
return $total;
//sec
$total = ($total * 60) + ($interval->s );
if ($unit == TimeZoneCalc::Seconds)
return $total;
return false;
}
回答by Vadim Samokhin
It always feels nice when whatyou need is already present as a class. In your case, you need a total number of seconds. Why not make a class out of that? The code could look like that:
当你需要的东西已经作为一个班级出现时,感觉总是很好。在您的情况下,您需要总秒数。为什么不把它做成一门课呢?代码可能如下所示:
(new TotalFullSeconds(
new IntervalFromRange(
new DateTimeFromISO8601String('2017-07-03T14:27:39+00:00'),
new DateTimeFromISO8601String('2017-07-05T14:27:39+00:00')
)
))
->value()
Besides being intuitive, you can also benefit from autocompletion facility of your IDE.
除了直观之外,您还可以从 IDE 的自动完成功能中受益。
For more useful implementation, you can check out this library.
有关更有用的实现,您可以查看此库。
回答by Duniyadnd
You could just put in the hard numbers (instead of 60*60 - put in 3600) so it doesn't need to calculate them each time.
您可以只输入硬数字(而不是 60*60 - 输入 3600),这样就不需要每次都计算它们。
Edit - fixed the number based off your comment.
编辑 - 根据您的评论修正数字。

