php計算兩個日期時間差(返回年、月、日)_php執行個體

來源:互聯網
上載者:User
在PHP程式中,很多時候都會遇到處理時間的問題,比如:判斷使用者線上了多長時間,共登入了多少天,兩個文章發布的時間差或者是不同操作之間的日誌記錄等等。在文章中,簡單地舉例介紹了PHP中如何計算兩個日期相差 年、月、日。

<?php  /**  +----------------------------------------------------------  * 功能:計算兩個日期相差 年 月 日  +----------------------------------------------------------  * @param date   $date1 起始日期  * @param date   $date2 到期日日期  +----------------------------------------------------------  * @return array        +----------------------------------------------------------  */function DiffDate($date1, $date2) {   if (strtotime($date1) > strtotime($date2)) {     $ymd = $date2;     $date2 = $date1;     $date1 = $ymd;   }   list($y1, $m1, $d1) = explode('-', $date1);   list($y2, $m2, $d2) = explode('-', $date2);   $y = $m = $d = $_m = 0;   $math = ($y2 - $y1) * 12 + $m2 - $m1;   $y = round($math / 12);   $m = intval($math % 12);   $d = (mktime(0, 0, 0, $m2, $d2, $y2) - mktime(0, 0, 0, $m2, $d1, $y2)) / 86400;   if ($d < 0) {     $m -= 1;     $d += date('j', mktime(0, 0, 0, $m2, 0, $y2));   }   $m < 0 && $y -= 1;   return array($y, $m, $d); } ?>
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.