PHP Date diff with a difference

寵の児 提交于 2019-12-06 12:02:21

The way to approach this is to extend DateTime and overide the add() and sub() methods to behave as you want them to. That, after all is one of the advantages of OOP.

The way to get your desired behaviours is to set the day of the month to the 1st before doing calling add() or sub() and then restoring the original or highest possible day afterwards.

My first attempt is below, not thoroughly tested, but adding 1 month to 31st Jan gave 28th Feb, which, I believe is your desired behaviour:-

class MyDateTime extends \DateTime
{
    public function add($interval)
    {
        $oldDay = (int)$this->format('d');
        $this->setDate((int)$this->format('Y'), (int)$this->format('m'), 1);
        parent::add($interval);
        $maxDay = (int)$this->format('t');
        if($oldDay > $maxDay){
            $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $maxDay);
        } else {
            $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $oldDay);
        }
        return $this;
    }

    public function sub($interval)
    {
        $oldDay = (int)$this->format('d');
        $this->setDate((int)$this->format('Y'), (int)$this->format('m'), 1);
        parent::sub($interval);
        $maxDay = (int)$this->format('t');
        if($oldDay > $maxDay){
            $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $maxDay);
        } else {
            $this->setDate((int)$this->format('Y'), (int)$this->format('m'), $oldDay);
        }
        return $this;
    }

    public function diff($dateTime2, $absolute = false)
    {
        if((int)$this->format('t') > (int)$dateTime2->format('t')){
            $this->setDate((int)$this->format('Y'), (int)$this->format('m'), (int)$dateTime2->format('t'));
        }
        if((int)$this->format('t') < (int)$dateTime2->format('t')){
            $dateTime2->setDate((int)$dateTime2->format('Y'), (int)$dateTime2->format('m'), (int)$this->format('t'));
        }
        return parent::diff($dateTime2, $absolute);
    }
}

Here is a working example using your exampe dates

Here is an example using the diff() method as you can see it gives a 3 month difference. You can also see that adding the resulting DateInterval to the original date results in the second date.

The sub() method may require a bit more thought, but I don't have time just now. I'll take a more thorough look if I get a few spare minutes later.

This way you would get the absolute difference between months without taking in account the day.

 <?php
function getMonthDiff($firstMonth, $secondMonth)
{
    $firstMonth  = $firstMonth->format("Y") * 12 + $firstMonth->format("m");
    $secondMonth = $secondMonth->format("Y") * 12 + $secondMonth->format("m");

    return abs($firstMonth - $secondMonth);
}


$datetime1 = new DateTime('2000-11-30');
$datetime2 = new DateTime('2001-02-28');
echo getMonthDiff($datetime1, $datetime2);
echo "<br />";
$datetime1 = new DateTime('2000-11-30');
$datetime2 = new DateTime('2001-03-02');
echo getMonthDiff($datetime1, $datetime2);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!