php 中的日期差异?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1940338/
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
Date Difference in php on days?
提问by mysqllearner
Is there a quick way to calculate date difference in php? For example:
有没有一种快速的方法来计算 php 中的日期差异?例如:
$date1 = '2009-11-12 12:09:08';
$date2 = '2009-12-01 08:20:11';
And then do a calculation, $date2 minus $date1
然后做一个计算,$date2减去$date1
I read php.net documentation, but no luck. Is there a quick way to do it?
我阅读了 php.net 文档,但没有运气。有没有快速的方法来做到这一点?
采纳答案by txyoji
strtotime will convert your date string to a unix time stamp. (seconds since the unix epoch.
strtotime 会将您的日期字符串转换为 unix 时间戳。(自 Unix 时代以来的秒数。
$ts1 = strtotime($date1);
$ts2 = strtotime($date2);
$seconds_diff = $ts2 - $ts1;
回答by adam
I would recommend to use date->difffunction, as in example below:
我建议使用date->diff函数,如下例所示:
$dStart = new DateTime('2012-07-26');
$dEnd = new DateTime('2012-08-26');
$dDiff = $dStart->diff($dEnd);
echo $dDiff->format('%r%a'); // use for point out relation: smaller/greater
回答by Anirudh Sood
Below code will give the output for number of days, by taking out the difference between two dates..
下面的代码将通过取出两个日期之间的差异来给出天数的输出..
$str = "Jul 02 2013";
$str = strtotime(date("M d Y ")) - (strtotime($str));
echo floor($str/3600/24);

