-
$date1 = date( 'Y-m-d' );
- $date2 = "2015-12-04";
- $diff = abs ($diff = abs ($diff = abs ($diff =diff 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 年, %d 個月, %d 天n", $年, $月, $天);
- - -- ------------------------------------------------ -- --- 或
- $date1 = new DateTime("2007-03-24");
- $date2 = new DateTime("2009-06-26");
- $interval = $date1 -> ;diff($date2);
- 回顯「差異」。 $interval->y 。 「 年, 」 。 $interval->m." 月,".$interval->d." 天";
- //顯示總天數(不是像上面那樣分為年月日)
- echo "difference" . $間隔->天。 「日子」;
- ------------------------------------------- --------- ------------- 或
-
-
- /**
- * 以精確的語意計算兩個日期之間的差異。基於 PHP DateTime::diff()
- * 由 Derick Rethans 實作。由 Emil H 移植到 PHP,2011-05-02。不保留任何權利。
- *
- * 請參閱此處查看原始程式碼:
- * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/tm2unixtime。 c?revision=302890&view=markup
- * http://svn.php.net/viewvc/php/php-src/trunk/ext/date/lib/interval.c?revision=298973&view=markup
- */
- function _date_range_limit($start, $ end, $adj, $a, $ b, $result)
- {
- if ($result[$a] $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_array_month = 310, 315 , 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" ] $month--;
- if ($month $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"] $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 ;
- }
- }
- }
- 回傳$result;
- }
- 函數_date_normalize($result;
- }
- 函數_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;}/** * 接受兩個 unix 時間戳記。 */function _date_diff($one, $two){ $in =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"];
- $結果["m"] = $b["m"] - $a["m"];
- $結果["d"] = $b["d"] - $a[" d" ];
- $結果["h"] = $b["h"] - $a["h"];
- $結果["i"] = $b["i"] - $a[" i"];
- $結果["s"] = $b["s"] - $a["s"];
- $結果["反轉"] = $反轉 ? 1 : 0;
- $result["days"] = intval(abs(($one - $two)/86400));
- if ($invert) {
- _date_normalize(&$a, & $結果);
- } else {
- _date_normalize(&$b, &$result);
- }
- 回傳$結果;
- }
- $date = "2014-12- 04 19:37:22";
- echo '
';
- print_r( _date_diff( strtotime($date), time() ) );
- echo '';
- ?>
複製程式碼
|