如何使用PHP找到两个日期之间的天数?
当前回答
选择的答案不是最正确的答案,因为它将在UTC之外失败。 根据时区(列表),可能会有时间调整创建“没有”24小时的日子,这将使计算(60*60*24)失败。
这里有一个例子:
date_default_timezone_set('europe/lisbon');
$time1 = strtotime('2016-03-27');
$time2 = strtotime('2016-03-29');
echo floor( ($time2-$time1) /(60*60*24));
^-- the output will be **1**
因此,正确的解决方案是使用DateTime
date_default_timezone_set('europe/lisbon');
$date1 = new DateTime("2016-03-27");
$date2 = new DateTime("2016-03-29");
echo $date2->diff($date1)->format("%a");
^-- the output will be **2**
其他回答
使用这个:)
$days = (strtotime($endDate) - strtotime($startDate)) / (60 * 60 * 24);
print $days;
现在起作用了
我阅读了所有以前的解决方案,没有一个使用PHP 5.3工具:DateTime::Diff和DateInterval::Days
DateInterval::Days精确地包含日期之间的天数。没有必要创造一些特别和奇异的东西。
/**
* We suppose that PHP is configured in UTC
* php.ini configuration:
* [Date]
* ; Defines the default timezone used by the date functions
* ; http://php.net/date.timezone
* date.timezone = UTC
* @link http://php.net/date.timezone
*/
/**
* getDaysBetween2Dates
*
* Return the difference of days between $date1 and $date2 ($date1 - $date2)
* if $absolute parameter is false, the return value is negative if $date2 is after than $date1
*
* @param DateTime $date1
* @param DateTime $date2
* @param Boolean $absolute
* = true
* @return integer
*/
function getDaysBetween2Dates(DateTime $date1, DateTime $date2, $absolute = true)
{
$interval = $date2->diff($date1);
// if we have to take in account the relative position (!$absolute) and the relative position is negative,
// we return negatif value otherwise, we return the absolute value
return (!$absolute and $interval->invert) ? - $interval->days : $interval->days;
}
echo '<h3>2020-03-01 - 2020-02-01: 29 days as it\'s a standard leap year</h3>';
echo getDaysBetween2Dates(new DateTime("2020-03-01"), new DateTime("2020-02-01"), false);
echo '<h3>1900-03-01 - 1900-02-01: 28 days as it\'s a "standard" century</h3>';
echo getDaysBetween2Dates(new DateTime("1900-03-01"), new DateTime("1900-02-01"), false);
echo '<h3>2000-03-01 - 2000-02-01: 29 days as it\'s a century multiple of 400: 2000=400x5</h3>';
echo getDaysBetween2Dates(new DateTime("2000-03-01"), new DateTime("2000-02-01"), false);
echo '<h3>2020-03-01 - 2020-04-01: -28 days as 2020-03-01 is before 2020-04-01</h3>';
echo getDaysBetween2Dates(new DateTime("2020-02-01"), new DateTime("2020-03-01"), false);
$now = time(); // or your date as well
$your_date = strtotime("2010-01-31");
$datediff = $now - $your_date;
echo round($datediff / (60 * 60 * 24));
// Change this to the day in the future
$day = 15;
// Change this to the month in the future
$month = 11;
// Change this to the year in the future
$year = 2012;
// $days is the number of days between now and the date in the future
$days = (int)((mktime (0,0,0,$month,$day,$year) - time(void))/86400);
echo "There are $days days until $day/$month/$year";
从PHP 5.3及以上版本开始,添加了新的日期/时间函数来获得不同:
$datetime1 = new DateTime("2010-06-20");
$datetime2 = new DateTime("2011-06-22");
$difference = $datetime1->diff($datetime2);
echo 'Difference: '.$difference->y.' years, '
.$difference->m.' months, '
.$difference->d.' days';
print_r($difference);
结果如下:
Difference: 1 years, 0 months, 2 days
DateInterval Object
(
[y] => 1
[m] => 0
[d] => 2
[h] => 0
[i] => 0
[s] => 0
[invert] => 0
[days] => 367
)
希望能有所帮助!
推荐文章
- 输入对象的datetime。Datetime没有Datetime属性
- GCM与PHP(谷歌云消息传递)
- 如何在Symfony 2中获得请求参数?
- 我如何在PHP中输出一个UTF-8 CSV, Excel将正确读取?
- 如何初始化静态变量
- 如何将PDF文档转换为PHP预览图像?
- 如何插入元素到数组在特定的位置?
- 使用PHP加密和解密密码的最佳方法是什么?
- 如何实现一个好的脏话过滤器?
- PHP中的三个点(…)是什么意思?
- Guzzlehttp -如何从guzzle6得到响应的正文?
- 移动一个文件到服务器上的另一个文件夹
- Laravel中使用雄辩的ORM进行批量插入
- PHP 5.4调用时引用传递-容易修复可用吗?
- 格式化字节到千字节,兆字节,千兆字节