栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何使用PHP计算两个日期之间的差异?

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何使用PHP计算两个日期之间的差异?

将此用于旧代码(PHP <5.3)。有关最新解决方案,请参见下面的jurka答案

您可以使用strtotime()将两个日期转换为Unix时间,然后计算它们之间的秒数。由此很容易计算出不同的时间段。

$date1 = "2007-03-24";$date2 = "2009-06-26";$diff = abs(strtotime($date2) - strtotime($date1));$years = floor($diff / (365*60*60*24));$months = floor(($diff - $years * 365*60*60*24) / (30*60*60*24));$days = floor(($diff - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));printf("%d years, %d months, %d daysn", $years, $months, $days);

编辑: 显然,执行此操作的首选方法如下面的jurka所述。通常,仅当您没有PHP 5.3或更高版本时才建议使用我的代码。

评论中的几个人指出,上面的代码只是一个近似值。我仍然认为,对于大多数目的来说,这是很好的,因为使用范围更多的是为了提供经过或保留了多少时间的感觉,而不是提供精度-
如果要这样做,只需输出日期即可。

尽管如此,我还是决定解决这些投诉。如果您确实需要一个确切的范围,但又无法访问PHP 5.3,请使用下面的代码(它也应在PHP
4中工作)。这是PHP内部用于计算范围的代码的直接端口,但不考虑夏令时。这意味着它最多可以关闭一个小时,但是除了它应该是正确的之外。

<?phpfunction _date_range_limit($start, $end, $adj, $a, $b, $result){    if ($result[$a] < $start) {        $result[$b] -= intval(($start - $result[$a] - 1) / $adj) + 1;        $result[$a] += $adj * intval(($start - $result[$a] - 1) / $adj + 1);    }    if ($result[$a] >= $end) {        $result[$b] += intval($result[$a] / $adj);        $result[$a] -= $adj * intval($result[$a] / $adj);    }    return $result;}function _date_range_limit_days($base, $result){    $days_in_month_leap = array(31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);    $days_in_month = array(31, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);    _date_range_limit(1, 13, 12, "m", "y", &$base);    $year = $base["y"];    $month = $base["m"];    if (!$result["invert"]) {        while ($result["d"] < 0) { $month--; if ($month < 1) {     $month += 12;     $year--; } $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0); $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month]; $result["d"] += $days; $result["m"]--;        }    } else {        while ($result["d"] < 0) { $leapyear = $year % 400 == 0 || ($year % 100 != 0 && $year % 4 == 0); $days = $leapyear ? $days_in_month_leap[$month] : $days_in_month[$month]; $result["d"] += $days; $result["m"]--; $month++; if ($month > 12) {     $month -= 12;     $year++; }        }    }    return $result;}function _date_normalize($base, $result){    $result = _date_range_limit(0, 60, 60, "s", "i", $result);    $result = _date_range_limit(0, 60, 60, "i", "h", $result);    $result = _date_range_limit(0, 24, 24, "h", "d", $result);    $result = _date_range_limit(0, 12, 12, "m", "y", $result);    $result = _date_range_limit_days(&$base, &$result);    $result = _date_range_limit(0, 12, 12, "m", "y", $result);    return $result;}function _date_diff($one, $two){    $invert = false;    if ($one > $two) {        list($one, $two) = array($two, $one);        $invert = true;    }    $key = array("y", "m", "d", "h", "i", "s");    $a = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $one))));    $b = array_combine($key, array_map("intval", explode(" ", date("Y m d H i s", $two))));    $result = array();    $result["y"] = $b["y"] - $a["y"];    $result["m"] = $b["m"] - $a["m"];    $result["d"] = $b["d"] - $a["d"];    $result["h"] = $b["h"] - $a["h"];    $result["i"] = $b["i"] - $a["i"];    $result["s"] = $b["s"] - $a["s"];    $result["invert"] = $invert ? 1 : 0;    $result["days"] = intval(abs(($one - $two)/86400));    if ($invert) {        _date_normalize(&$a, &$result);    } else {        _date_normalize(&$b, &$result);    }    return $result;}$date = "1986-11-10 19:37:22";print_r(_date_diff(strtotime($date), time()));print_r(_date_diff(time(), strtotime($date)));


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/421601.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号