以天为单位获取 PHP DateTime 差异,将午夜视为一天的变化

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

Get a PHP DateTime difference in days, considering midnight as a day change

phpdatedatetimedateinterval

提问by marcv

What is the simplest way to get the difference in days between two PHP DateTimes, considering midnight as a day change (just like the DATEDIFF(DAY)SQL function does)?

DateTimes将午夜视为一天的变化(就像DATEDIFF(DAY)SQL 函数一样),获取两个 PHP 之间的天数差异的最简单方法是什么?

For example, between today at 13:00 and tomorrow at 12:00, I should get 1 (day), even though the interval is less than 24 hours.

例如,今天13:00到明天12:00之间,即使间隔小于24小时,我也应该得到1(天)。

$date1 = new DateTime("2013-08-07 13:00:00");
$date2 = new DateTime("2013-08-08 12:00:00");
echo $date1->diff($date2)->days; // 0

回答by MajorCaiger

You could ignore the time portion of the date string

您可以忽略日期字符串的时间部分

$date1 = new DateTime(date('Y-m-d', strtotime("2013-08-07 13:00:00")));
$date2 = new DateTime(date('Y-m-d', strtotime("2013-08-08 12:00:00")));
echo $date1->diff($date2)->days; // 1

回答by x4rf41

a simple solution to this to strip the time or set it to 00:00:00, that should always give you the desired result:

对此的一个简单解决方案是剥离时间或将其设置为00:00:00,这应该始终为您提供所需的结果:

$date1 = new DateTime("2013-08-07");
$date2 = new DateTime("2013-08-08");
echo $date1->diff($date2)->days;

or

或者

$date1 = new DateTime("2013-08-07 00:00:00");
$date2 = new DateTime("2013-08-08 00:00:00");
echo $date1->diff($date2)->days;

the time doesnt really matter here

时间在这里并不重要

回答by MoonLite

note that DateInterval->days is always positive. hence the use of ->invert.

请注意 DateInterval->days 始终为正数。因此使用 ->invert。

/**
 * return amount of days between dt1 and dt2 
 * (how many midnights pass going from dt1 to dt2)
 *  0 = same day, 
 * -1 = dt2 is 1 day before dt1, 
 *  1 = dt2 is 1 day after  dt1, etc.
 *
 * @param \DateTime $dt1
 * @param \DateTime $dt2
 * @return int|false 
 */
function getNightsBetween(\DateTime $dt1, \DateTime $dt2){
    if(!$dt1 || !$dt2){
        return false;
    }
    $dt1->setTime(0,0,0);
    $dt2->setTime(0,0,0);
    $dti = $dt1->diff($dt2);    // DateInterval
    return $dti->days * ( $dti->invert ? -1 : 1);   // nb: ->days always positive
}

usage examples:

用法示例:

$dt1 = \DateTime::createFromFormat('Y-m-d', '2014-03-03' );
$dt2 = \DateTime::createFromFormat('Y-m-d', '2014-02-20' );
getNightsBetween($dt1, $dt2);       // -11

$dt1 = \DateTime::createFromFormat('Y-m-d H:i:s', '2014-01-01 23:59:59' );
$dt2 = \DateTime::createFromFormat('Y-m-d H:i:s', '2014-01-02 00:00:01' );
getNightsBetween($dt1, $dt2);       // 1 (only 2 seconds later, but still the next day)

$dt1 = \DateTime::createFromFormat('Y-m-d', '2014-04-09' );
$dt2 = new \DateTime();
getNightsBetween($dt1, $dt2);       // xx (how many days (midnights) passed since I wrote this)

example of some text magic:

一些文本魔法的例子:

function getRelativeDay(\DateTime $dt2){
    if(!$dt2){
        return false;
    }
    $n = getNightsBetween( new \DateTime(), $dt2);
    switch($n){
        case  0: return "today";
        case  1: return "tomorrow";
        case -1: return "yesterday";
        default: 
            return $n . (abs($n)>1?"days":"day") . ($n<0?" ago":" from now");
    }
}

回答by sprutex

$date1 = new DateTime("2013-08-07 13:00:00");
$date2 = new DateTime("2013-08-08 12:00:00");
  1. Drop time in DateTime's objects:
  1. 在 DateTime 的对象中删除时间:
$date1->setTime(0, 0, 0);
$date2->setTime(0, 0, 0);
  1. Get difference of adapted objects:
  1. 获取适配对象的差异:
echo $date1->diff($date2)->days;

回答by Samira Khorshidi

this example can help you:

这个例子可以帮助你:

   $date1 = date_create($d1);
$date2 = date_create($d2);
//$FromFullDateTime=$from.$FromTime;

$date1_month = date_format($date1, 'd');
$date2_month = date_format($date2, 'd');
$dif = $date2_month - $date1_month;