Home > Backend Development > PHP Tutorial > PHP calculates the time difference between two dates (returns year, month, day)_PHP tutorial

PHP calculates the time difference between two dates (returns year, month, day)_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-13 10:24:39
Original
820 people have browsed it

In PHP programs, you 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. wait. In the article, a simple example is given of how to calculate the year, month, and day difference between two dates in PHP.

<&#63;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); 
} 
&#63;>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/825387.htmlTechArticleIn PHP programs, we often encounter processing time problems, such as: determining how long the user has been online , how many days have you been logged in in total, the time difference between the two posts or different operations...
Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template