如何使用PHP找到两个日期之间的天数?
当前回答
易于使用date_diff
$from=date_create(date('Y-m-d'));
$to=date_create("2013-03-15");
$diff=date_diff($to,$from);
print_r($diff);
echo $diff->format('%R%a days');
详见:https://blog.devgenius.io/how-to-find-the-number-of-days-between-two-dates-in-php-1404748b1e84
其他回答
最简单的方法来找出两个日期之间的天数差
$date1 = strtotime("2019-05-25");
$date2 = strtotime("2010-06-23");
$date_difference = $date2 - $date1;
$result = round( $date_difference / (60 * 60 * 24) );
echo $result;
我阅读了所有以前的解决方案,没有一个使用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);
如果你有以秒为单位的时间(即unix时间戳),那么你可以简单地减去时间并除以86400(秒/天)
面向对象的风格:
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
程序上的风格:
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%a days');
function get_daydiff($end_date,$today)
{
if($today=='')
{
$today=date('Y-m-d');
}
$str = floor(strtotime($end_date)/(60*60*24)) - floor(strtotime($today)/(60*60*24));
return $str;
}
$d1 = "2018-12-31";
$d2 = "2018-06-06";
echo get_daydiff($d1, $d2);
推荐文章
- MySQL中两个日期之间的差异
- Sql Server字符串到日期的转换
- 阻止人们入侵基于php的Flash游戏高分表的最佳方法是什么
- PHP子字符串提取。获取第一个'/'之前的字符串或整个字符串
- __construct函数的作用是什么?
- PHP中的异步shell执行器
- Laravel 5 -如何访问在视图存储上传的图像?
- 为什么在PHP中使用sprintf函数?
- “质量分配”在Laravel中是什么意思?
- 在逗号上爆炸字符串,并修剪每个值的潜在空格
- 设置NOW()为datetime数据类型的默认值?
- 在MySQL中Datetime等于或大于今天
- PHP与MySQL 8.0+错误:服务器请求身份验证方法未知的客户端
- 如何改变日期时间格式在熊猫
- laravel5“LIKE”对等物(雄辩的)