Home 类库下载 PHP类库 PHP calculates the difference between two dates in years, months and days

PHP calculates the difference between two dates in years, months and days

Oct 14, 2016 am 11:04 AM

In PHP programs, we often encounter processing time problems, such as determining how long a user has been online, how many days they have been logged in, the time difference between two posts or log records between different operations, etc. In the article, a simple example is given of how to calculate the year, month, and day difference between two dates in 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(&#39;-&#39;, $date1); 
    list($y2, $m2, $d2) = explode(&#39;-&#39;, $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(&#39;j&#39;, mktime(0, 0, 0, $m2, 0, $y2)); 
    } 
    $m < 0 && $y -= 1; 
    return array($y, $m, $d); 
} 
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1672
14
PHP Tutorial
1277
29
C# Tutorial
1257
24