分享PHP計算兩個日期相差天數(shù)的代碼
更新時間:2015年12月23日 14:09:42 投稿:lijiao
這篇文章主要為大家分享了PHP計算兩個日期差的代碼,實例分析了php操作日期的技巧,需要的朋友可以參考下
本文實例講述了php計算兩個日期相差天數(shù)的方法。分享給大家供大家參考。具體實現(xiàn)方法如下:
<?php $date1 = date( 'Y-m-d' ); $date2 = "2015-12-04"; $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 days\n", $years, $months, $days); -------------------------------------------------------- OR $date1 = new DateTime("2007-03-24"); $date2 = new DateTime("2009-06-26"); $interval = $date1->diff($date2); echo "difference " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days "; // shows the total amount of days (not divided into years, months and days like above) echo "difference " . $interval->days . " days "; -------------------------------------------------------- OR /** * Calculate differences between two dates with precise semantics. Based on PHPs DateTime::diff() * implementation by Derick Rethans. Ported to PHP by Emil H, 2011-05-02. No rights reserved. */ function _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; } /** * Accepts two unix timestamps. */ 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 = "2014-12-04 19:37:22"; echo '<pre>'; print_r( _date_diff( strtotime($date), time() ) ); echo '</pre>'; ?>
希望本文所述對大家學(xué)習(xí)php程序設(shè)計有所幫助。
您可能感興趣的文章:
- PHP實現(xiàn)針對日期,月數(shù),天數(shù),周數(shù),小時,分,秒等的加減運算示例【基于strtotime】
- php獲取給定日期相差天數(shù)的方法分析
- php根據(jù)年月獲取當(dāng)月天數(shù)及日期數(shù)組的方法
- PHP計算日期相差天數(shù)實例分析
- PHP使用strtotime計算兩個給定日期之間天數(shù)的方法
- php計算兩個日期相差天數(shù)的方法
- 計算一段日期內(nèi)的周末天數(shù)的php代碼(星期六,星期日總和)
- php 計算兩個時間相差的天數(shù)、小時數(shù)、分鐘數(shù)、秒數(shù)詳解及實例代碼
- php實現(xiàn)按天數(shù)、星期、月份查詢的搜索框
- PHP獲取某個月最大天數(shù)(最后一天)的方法
- php算開始時間到過期時間的相隔的天數(shù)
- PHP編程計算日期間隔天數(shù)的方法
相關(guān)文章
PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題示例
這篇文章主要介紹了PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題,結(jié)合具體實例形式分析了php使用單鏈表解決約瑟夫環(huán)問題的算法原理與相關(guān)操作技巧,需要的朋友可以參考下2017-09-09php在頁面中調(diào)用fckeditor編輯器的方法
fckeditor編輯器的代碼可以寫成一個函數(shù)或者類別,直接調(diào)用,不用每次都要寫那么多代碼!2011-06-06php中curl和file_get_content的區(qū)別
抓取遠(yuǎn)程內(nèi)容,之前一直都在用file_get_content函數(shù),其實早就知道有curl這么一個好東西的存在,但是看了一眼后感覺使用頗有些復(fù)雜,沒有file_get_content那么簡單,再就是需求也不大,所以沒有學(xué)習(xí)使用curl2014-05-05