php 用PHP计算两个日期之间的年数

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

Calculating number of years between 2 dates in PHP

phpdate

提问by Deepak

I need to get the number of years from 2 dates provided. Here's my code:

我需要从提供的 2 个日期中获取年数。这是我的代码:

function daysDifference($endDate, $beginDate)
{
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);

   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   $diff = $end_date - $start_date;
   echo $diff;
   $years = floor($diff / (365.25*60*60*24));
   return $years;
}

echo daysDifference('2011-03-12','2008-03-09');

The $diffgives a number output. When I return $years, am getting 0. What have I done wrong?

$diff给出了一些输出。当我回来时$years,我得到了0。我做错了什么?

回答by Matthew

$d1 = new DateTime('2011-03-12');
$d2 = new DateTime('2008-03-09');

$diff = $d2->diff($d1);

echo $diff->y;

回答by Nick de Kruijk

On a PHP 5.2 box (yeah really, they still exist) so without DateTime::diff() support I ended up using this:

在 PHP 5.2 机器上(是的,它们仍然存在)所以没有 DateTime::diff() 支持我最终使用了这个:

$dateString='10-05-1975';
$years = round((time()-strtotime($dateString))/(3600*24*365.25))

回答by Scrappy Cocco

Guys heres a good code i used to use.....its not my idea ....i just got it some where from the net...

伙计们,这是我曾经使用过的一个很好的代码.....这不是我的主意....我只是从网上得到了它...

It also have a variety of options hope this helps.........

它也有多种选择希望这会有所帮助.........

    function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
    /*
    $interval can be:
    yyyy - Number of full years
    q - Number of full quarters
    m - Number of full months
    y - Difference between day numbers
        (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
    d - Number of full days
    w - Number of full weekdays
    ww - Number of full weeks
    h - Number of full hours
    n - Number of full minutes
    s - Number of full seconds (default)
    */

    if (!$using_timestamps) {
        $datefrom = strtotime($datefrom, 0);
        $dateto = strtotime($dateto, 0);
    }
    $difference = $dateto - $datefrom; // Difference in seconds

    switch($interval) {

    case 'yyyy': // Number of full years

        $years_difference = floor($difference / 31536000);
        if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
            $years_difference--;
        }
        if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
            $years_difference++;
        }
        $datediff = $years_difference;
        break;

    case "q": // Number of full quarters

        $quarters_difference = floor($difference / 8035200);
        while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
            $months_difference++;
        }
        $quarters_difference--;
        $datediff = $quarters_difference;
        break;

    case "m": // Number of full months

        $months_difference = floor($difference / 2678400);
        while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
            $months_difference++;
        }
        $months_difference--;
        $datediff = $months_difference;
        break;

    case 'y': // Difference between day numbers

        $datediff = date("z", $dateto) - date("z", $datefrom);
        break;

    case "d": // Number of full days

        $datediff = floor($difference / 86400);
        break;

    case "w": // Number of full weekdays

        $days_difference = floor($difference / 86400);
        $weeks_difference = floor($days_difference / 7); // Complete weeks
        $first_day = date("w", $datefrom);
        $days_remainder = floor($days_difference % 7);
        $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
        if ($odd_days > 7) { // Sunday
            $days_remainder--;
        }
        if ($odd_days > 6) { // Saturday
            $days_remainder--;
        }
        $datediff = ($weeks_difference * 5) + $days_remainder;
        break;

    case "ww": // Number of full weeks

        $datediff = floor($difference / 604800);
        break;

    case "h": // Number of full hours

        $datediff = floor($difference / 3600);
        break;

    case "n": // Number of full minutes

        $datediff = floor($difference / 60);
        break;

    default: // Number of full seconds (default)

        $datediff = $difference;
        break;
    }    

    return $datediff;

} 

echo datediff('yyyy','2009-01-16','2011-03-16');

回答by opps

the $start_date and $end_date' value is the number of days, not seconds. so you should not divide the $diff with 365.25*60*60*24.

$start_date 和 $end_date' 值是天数,而不是秒数。所以你不应该用 365.25*60*60*24 来划分 $diff。

function daysDifference($endDate, $beginDate)
{

   $date_parts1 = explode("-", $beginDate);
   $date_parts2 = explode("-", $endDate);
   $start_date = gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date = gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   $diff = abs($end_date - $start_date);
   $years = floor($diff / 365.25);
   return $years;
}

echo daysDifference('2011-03-12','2008-03-09');

回答by Pascal MARTIN

If you want the number of years between two dates, why not just use the following :

如果您想要两个日期之间的年数,为什么不使用以下内容:

function yearsDifference($endDate, $beginDate)
{
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   return $date_parts2[0] - $date_parts1[0];
}

echo yearsDifference('2011-03-12','2008-03-09');

Which, in this case, will get you :

在这种情况下,您将获得:

3