2

I can do it for days like this:

$d1 = new DateTime('2000-01-01 12:00:00');
$d2 = new DateTime('2020-01-01 12:00:00');
$diff = $d2->diff($d1);
echo $diff->days;

In other words, it works for days. However, the DateTime/DateInterval class has only a $days variable -- these are expected but don't exist:

$diff->weeks;
$diff->months;
$diff->years;

Reading the manual, you might at first glance be deceived into thinking that it does have these attributes: https://www.php.net/manual/en/class.dateinterval.php

public integer $y ;
public integer $m ;
public integer $d ;
public integer $h ;
public integer $i ;
public integer $s ;
public float $f ;
public integer $invert ;
public mixed $days ;

The y, m, d, h, i, s there are not "individual totals", but depend on each other. For example, if the time span is exactly one year, the $y will be 1, but all of the other ones will be 0, instead of their respective representations (12 months, 52 weeks, etc.).

They treat days specially for some reason by including the $days variable, which does show the actual total number of days. I want that for weeks, months and years too.

I already know how to "estimate" the number of weeks/months/years between two timestamps, by using simple math and fixed variables representing the average number of seconds in each time unit. Since this doesn't take into consideration all the complexities of "traversing" the calendar format(s), such as leap years, varying days in different months, and many other small/complex details, you don't get the exact number that way.

I want to know the exact total number of weeks between two timestamps, and the same thing for years and months, independent of each other.

  • 1
    So do you want floating point months e.g. if the dates are `2020-01-04` and `2020-02-19` do you want 1 or 1.5? – Nick Mar 04 '20 at 06:47
  • And how accurate does that need to be? How many decimal points do you want to display to the user? – Andreas Mar 04 '20 at 06:54
  • Since the number of weeks that you get through simple math is not an estimate - a week can never have a value different from 7 days - does this mean that if you have a date in one week and a date in the following week, you want the value of weeks to be two, since the days span throughout 2 weeks? – El_Vanja Mar 04 '20 at 07:49
  • Only expect full numbers, but decimals would be an added bonus. – user13003958 Mar 04 '20 at 09:23

2 Answers2

0

This will return the exact difference between two days hope this will help you.

$time_diffrence=getDiffrenceBetweenTwoDays($date1,$date2); 

function getDiffrenceBetweenTwoDays($date1,$date2){
    $etime = strtotime($date1) - strtotime($date2;

    if ($etime < 1)
    {
        return '0 seconds';
    }

    $a = array( 365 * 24 * 60 * 60  =>  'year',
                 30 * 24 * 60 * 60  =>  'month',
                      24 * 60 * 60  =>  'day',
                           60 * 60  =>  'hour',
                                60  =>  'minute',
                                 1  =>  'second'
                );
    $a_plural = array( 'year'   => 'years',
                       'month'  => 'months',
                       'day'    => 'days',
                       'hour'   => 'hours',
                       'minute' => 'minutes',
                       'second' => 'seconds'
                );

    foreach ($a as $secs => $str)
    {
        $d = $etime / $secs;
        if ($d >= 1)
        {
            $r = round($d);
            return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) .''  ;
        }
    }
}
Shubham Azad
  • 786
  • 2
  • 10
  • 25
-1

Replace %a with any of the following at this link:

FORMATS

$d1 = date_create('2000-01-01 12:00:00');
$d2 = date_create('2020-01-01 12:00:00');
$diff = date_diff($d1, $d2);

$days = $diff->format('%a');
echo $days; // 7305
Mielie007
  • 126
  • 6
  • This does not solve the OP's question. He has described that the values of the DateInterval class are not the thing he needs; he needs totals and the class only holds totals for days. – El_Vanja Mar 04 '20 at 07:44
  • As @El_Vanja said, and which I verified by testing it, your answer doesn't answer the question. – user13003958 Mar 04 '20 at 09:24