Home > Backend Development > PHP Problem > How to calculate the difference in months between two times in php

How to calculate the difference in months between two times in php

藏色散人
Release: 2023-03-04 06:06:02
Original
4718 people have browsed it

php method to calculate the difference in months: first set two time points; then pass "abs($date1[0] - $date2[0]) * 12 abs($date1[1] - $ date2[1]);" method can calculate the difference between two times.

How to calculate the difference in months between two times in php

Recommendation: "PHP Video Tutorial"

php determines how many months the difference is between two dates

The specific implementation method is as follows:

/**
 * @author injection(injection.mail@gmail.com)
* @var date1日期1
* @var date2 日期2
* @var tags 年月日之间的分隔符标记,默认为'-' 
* @return 相差的月份数量
* @example:
$date1 = "2003-08-11";
$date2 = "2008-11-06";
$monthNum = getMonthNum( $date1 , $date2 );
echo $monthNum;
*/
function getMonthNum( $date1, $date2, $tags='-' ){
 $date1 = explode($tags,$date1);
 $date2 = explode($tags,$date2);
 return abs($date1[0] - $date2[0]) * 12 + abs($date1[1] - $date2[1]);
}
Copy after login

The above is the detailed content of How to calculate the difference in months between two times in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template