php 在php中减去两个日期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10469037/
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
Subtracting two dates in php
提问by Dmitry Makovetskiyd
I have got two dates in php
我在 php 中有两个日期
$date1 = 'May 3, 2012 10:38:22 GMT'
$date2 = '06 Apr 2012 07:22:21 GMT'
Then I subtract both of them
然后我减去他们两个
$date2 - $date1
, and get
,并得到
Result:6
Why is the result 6 and not 27? ... ? How can I subtract the two dates, and make it return me a result based on month differences while subtracting the years & days & time ?
为什么结果是 6 而不是 27?……?如何减去两个日期,并使其根据月份差异返回结果,同时减去年、日和时间?
回答by evan
Part 1: Why is the result 6?
第 1 部分:为什么结果是 6?
The dates are simply strings when you first subtract them. PHP attempts to convert them to integers. It does this by converting until the first non-number. So, date2 become 6 and date1 becomes 0.
当您第一次减去它们时,日期只是字符串。PHP 尝试将它们转换为整数。它通过转换直到第一个非数字来做到这一点。因此,date2 变为 6,date1 变为 0。
Part 2: How do you get it to work?
第 2 部分:您如何让它发挥作用?
$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;
Convert as appropriate.
适当转换。
回答by Shiplu Mokaddim
Using DateTimeand DateInterval,
$date1 = new DateTime("May 3, 2012 10:38:22 GMT");
$date2 = new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->format("%d");
回答by ajmal iqbal
There is one way to use mktime n make the date in timestamp and then subtract and then use date function to show in the way u want....
有一种方法可以使用 mktime n 在时间戳中创建日期,然后减去,然后使用日期函数以您想要的方式显示....
Other way is that format both of dates in the same format then subtract....
另一种方法是将两个日期格式化为相同的格式,然后减去....
Third way
第三种方式
$date1= new DateTime("May 3, 2012 10:38:22 GMT");
$date2= new DateTime("06 Apr 2012 07:22:21 GMT");
echo $date1->diff($date2)->("%d");
forth way
前路
$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == return sec in difference
$days = $secs / 86400;
回答by Romancha KC
$todate= strtotime('May 3, 2012 10:38:22 GMT');
$fromdate= strtotime('06 Apr 2012 07:22:21 GMT');
$calculate_seconds = $todate- $fromdate; // Number of seconds between the two dates
$days = floor($calculate_seconds / (24 * 60 * 60 )); // convert to days
echo($days);
This code will find the date difference between two dates..
此代码将查找两个日期之间的日期差异。
Here output is 27
这里的输出是 27
回答by ex3v
Most of presented solutions seems to be working, but everyone forgets about one thing: time.
大多数提出的解决方案似乎都有效,但每个人都忘记了一件事:时间。
Taking evanexample:
以伊万为例:
$datetime1 = strtotime('May 3, 2012 10:38:22 GMT');
$datetime2 = strtotime('06 Apr 2012 07:22:21 GMT');
$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;
When you don't trim time part, what might lead to milscalculations. For example: Interval between 2014-05-01 14:00:00(Y-m-d) and 2014-05-02 07:00:00will be 0,xxx, not 1. You should trim time part of every date.
当您不修剪时间部分时,可能会导致 milscalculations。例如:2014-05-01 14:00:00(Ymd) 和之间的间隔2014-05-02 07:00:00将是 0,xxx,而不是 1。您应该修剪每个日期的时间部分。
So it should be:
所以应该是:
$datetime1 = strtotime(date('Y-m-d', strtotime('May 3, 2012 10:38:22 GMT')));
$datetime2 = strtotime(date('Y-m-d', strtotime('06 Apr 2012 07:22:21 GMT')));
$secs = $datetime2 - $datetime1;// == <seconds between the two times>
$days = $secs / 86400;
回答by naveen kumar
echo 'time'.$notification_time= "2008-12-13 10:42:00";
date_default_timezone_set('Asia/Kolkata');
echo 'cureen'.$currenttime=date('Y-m-d H:i:s');
$now = new DateTime("$notification_time");
$ref = new DateTime("$currenttime");
$diff = $now->diff($ref);
printf('%d days, %d hours, %d minutes', $diff->d, $diff->h, $diff->i);
回答by Daniel Nieto
If you want to use diff(it returns a Dateinterval object) method, the correct way is to format with %a. I mean:
如果要使用 diff(它返回一个 Dateinterval 对象)方法,正确的方法是使用 %a 进行格式化。我的意思是:
If you check http://php.net/manual/en/dateinterval.format.php
如果你检查http://php.net/manual/en/dateinterval.format.php
The correct way is:
正确的方法是:
echo $date1->diff($date2)->format("%a");
For getting all days
为了得到所有的日子

