欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

分享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è)計有所幫助。

相關(guān)文章

  • PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題示例

    PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題示例

    這篇文章主要介紹了PHP實現(xiàn)的基于單向鏈表解決約瑟夫環(huán)問題,結(jié)合具體實例形式分析了php使用單鏈表解決約瑟夫環(huán)問題的算法原理與相關(guān)操作技巧,需要的朋友可以參考下
    2017-09-09
  • FCKeditor添加自定義按鈕

    FCKeditor添加自定義按鈕

    我想在FCKeditor 中的工具欄上添加一個按鈕,在哪個配置文件中修改?答案很簡單。
    2008-03-03
  • PHP平滑關(guān)閉/重啟的實現(xiàn)方法

    PHP平滑關(guān)閉/重啟的實現(xiàn)方法

    "平滑重啟"和"快速重啟"是兩種不同的概念,下面這篇文章主要給大家介紹了關(guān)于PHP平滑關(guān)閉/重啟的實現(xiàn)方法,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • 淺談json_encode用法

    淺談json_encode用法

    這邊文章主要向我們簡單介紹了下json_encode的使用方法,非常簡單實用,這里推薦給大家。
    2015-03-03
  • PHP 危險函數(shù)解釋 分析

    PHP 危險函數(shù)解釋 分析

    在編譯 PHP 時,如無特殊需要,一定禁止編譯生成 CLI 命令行模式的 php 解析支持。
    2009-04-04
  • php在頁面中調(diào)用fckeditor編輯器的方法

    php在頁面中調(diào)用fckeditor編輯器的方法

    fckeditor編輯器的代碼可以寫成一個函數(shù)或者類別,直接調(diào)用,不用每次都要寫那么多代碼!
    2011-06-06
  • php中curl和file_get_content的區(qū)別

    php中curl和file_get_content的區(qū)別

    抓取遠(yuǎn)程內(nèi)容,之前一直都在用file_get_content函數(shù),其實早就知道有curl這么一個好東西的存在,但是看了一眼后感覺使用頗有些復(fù)雜,沒有file_get_content那么簡單,再就是需求也不大,所以沒有學(xué)習(xí)使用curl
    2014-05-05
  • Mac下php 5升級到php 7的步驟詳解

    Mac下php 5升級到php 7的步驟詳解

    這篇文章主要給大家介紹了在Mac下將php 5升級到php 7的步驟,文中將步驟介紹的非常詳細(xì),并分享了在升級過程中可能遇到的問題的解決方法,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-04-04
  • php實現(xiàn)編輯和保存文件的方法

    php實現(xiàn)編輯和保存文件的方法

    這篇文章主要介紹了php實現(xiàn)編輯和保存文件的方法,涉及php針對文件的讀取、編輯和保存操作的相關(guān)技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-07-07
  • php中的異常和錯誤淺析

    php中的異常和錯誤淺析

    PHP錯誤是屬于php程序自身的問題,一般是由非法的語法,環(huán)境問題導(dǎo)致的,使得編譯器無法通過檢查甚至無法運行的情況。PHP異常一般是業(yè)務(wù)邏輯上出現(xiàn)的不合預(yù)期、與正常流程不同的狀況,不是語法錯誤。本文介紹了php中異常和錯誤的相關(guān)資料,需要的朋友可以參考下。
    2017-05-05

最新評論