简体   繁体   English

在 PHP 中,如何获得两个时间戳之间准确(而非近似)的周数、月数和年数?

[英]In PHP, how do I get the accurate (not approximate) total individual number of weeks, months and years between two timestamps?

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 :但是,DateTime/ DateInterval类只有一个 $days 变量——这些是预期的,但不存在

$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阅读手册,您乍一看可能会误以为它确实具有以下属性: 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. y, m, d, h, i, s不是“个体总数”,而是相互依赖。 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.).例如,如果时间跨度正好是一年,则 $y 将为 1,但所有其他的将为 0,而不是它们各自的表示(12 个月、52 周等)。

They treat days specially for some reason by including the $days variable, which does show the actual total number of days.由于某些原因,他们通过包含 $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.我想知道两个时间戳之间的确切总数,以及年和月相同的事情,彼此独立。

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) .''  ;
        }
    }
}

Replace %a with any of the following at this link:在此链接中将 %a 替换为以下任何一项:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM