Home  >  Article  >  Backend Development  >  How to determine the number of months between two dates in PHP_PHP Tutorial

How to determine the number of months between two dates in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:49:541243browse

How to determine the number of months between two dates in php

This article describes the example of how to determine the number of months between two dates in php. Share it with everyone for your reference. The specific implementation method is as follows:

 ?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

/**

* @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]);

}

1

2 3

45 6 7 8 9 10
11
12
13 14 15 16 17
/** * @author injection(injection.mail@gmail.com) * @var date1date1 * @var date2 date 2 * @var tags Separator tag between year, month and day, default is '-' * @return the number of months difference * @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]); }
I hope this article will be helpful to everyone’s PHP programming design. http://www.bkjia.com/PHPjc/1018931.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1018931.htmlTechArticleHow to determine the number of months between two dates using php. This article explains how php determines the difference between two dates. How many months are the difference? Share it with everyone for your reference. Specific implementation...
Statement:
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