php 如何找到两个日期之间的天数差异

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4502832/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 13:13:27  来源:igfitidea点击:

How to find the difference in days between two dates

phpdate

提问by Xeoncross

Possible Duplicate:
How to find number of days between two dates using php

可能的重复:
如何使用 php 查找两个日期之间的天数

If I have two dates - how do I find the real difference in days between two dates? You must take things like leap years and the number of days in each month into account.

如果我有两个日期 - 我如何找到两个日期之间的实际天数差异?您必须考虑闰年和每个月的天数。

How many days are between something like 2010-03-29and 2009-07-16?

2010-03-29和之间有多少天2009-07-16

回答by netcoder

strtotimeand simple math:

strtotime和简单的数学:

   $daylen = 60*60*24;

   $date1 = '2010-03-29';
   $date2 = '2009-07-16';

   echo (strtotime($date1)-strtotime($date2))/$daylen;

回答by dnagirl

check out PHP DateTimeclass. It deals with all the gory details so you can just do regular subtraction.

查看 PHP DateTime类。它处理所有血腥细节,因此您只需进行常规减法即可。

$d1=date_create('1999-10-23');
$d2=date_create('2004-04-17');

$i=date_diff($d2,$d1);
echo $i->format('%a');

回答by THE DOCTOR

Here you go:

干得好:

<?php
$date1 = strtotime("2010-03-29");
$date2 = strtotime("2009-07-16");
$dateDiff = $date1 - $date2;
$fullDays = floor($dateDiff/(60*60*24));
echo "Differernce is $fullDays days";
?>