简体   繁体   English

使用闰年在PHP中的两个日期之间的差异

[英]Difference between two dates in php using Leap Year

Hey I am trying to get total months from two dates in php. 嘿,我试图从两个日期在PHP中获得总数。 I have searched about leap year calculations between two dates everywhere on internet but did not find the answer. 我在互联网上搜索了两个日期之间的闰年计算,但没有找到答案。

If my input is "2019-01-01" to "2019-03-31" then the result i expected is 3 months but result i get is 2 month. 如果我的输入是“2019-01-01”到“2019-03-31”那么我预期的结果是3个月,但我得到的结果是2个月。

Following is my code . 以下是我的代码。

 $date1 = strtotime("2019-01-01");  
 $date2 = strtotime("2019-02-28");  

 $diff = abs($date2 - $date1);  
 $years = floor($diff / (365*60*60*24));  
 $months = floor(($diff - $years * 365*60*60*24) 
                           / (30*60*60*24));  

 printf("%d months",$months);  

where am i going wrong 我哪里错了

Your math calculates the number of months between the two dates, exclusive of the ending month. 您的数学计算两个日期之间的月数,不包括结束月份。

Add 1 to the total, like so. 总计加1,就像这样。

 $months = (floor(($diff - $years * 365*60*60*24) 
                           / (30*60*60*24))) +1;

You can try the following: 您可以尝试以下方法:

$date1 = "2019-01-01";  
$date2 = "2019-02-28";  

$timestamp1 = strtotime($date1);
$timestamp2 = strtotime($date2);

$year1 = date('Y', $timestamp1);
$year2 = date('Y', $timestamp2);

$month1 = date('m', $timestamp1);
$month2 = date('m', $timestamp2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1); 

printf("%d months",$diff); 

Try it here: http://sandbox.onlinephpfunctions.com/code/496a08612489977e9e23e8ef3ea07ba991bc5e23 在这里试试: http//sandbox.onlinephpfunctions.com/code/496a08612489977e9e23e8ef3ea07ba991bc5e23

You have to add extra month in your $month variable 您必须在$ month变量中添加额外的月份

 $date1 = strtotime("2019-01-01");  
 $date2 = strtotime("2019-03-31");  

 $diff = abs($date2 - $date1);  
 $years = floor($diff / (365*60*60*24));  
 $months = floor(($diff - $years * 365*60*60*24) 
                           / (30*60*60*24)) +1 ;  

 printf("%d months",$months); //Output : give add one extra

OR 要么

$date1 = date_create('2019-01-01');
$date2 date_create('2019-03-31');
$interval= date_diff($date1, $date1);
echo $interval->format('%m months');

I Understand Your Problem.. Actually the result you are getting is correct,because day ends at night 12pm and you are checking at day time.So until day ends you cant get complete month.If You add one day and check you will get correctly 3 Months 我明白你的问题..实际上你得到的结果是正确的,因为一天晚上12点结束,你在白天检查。直到一天结束,你不能完成一个月。如果你加一天,检查你会得到正确的3个月

Below I Made Some Changes to Your Code.. 下面我对你的代码做了一些修改..

$new_date = date('Y-m-d', strtotime('2019-02-28' . ' +1 day'));
$date1 = strtotime("2019-01-01");  
$date2 = strtotime($new_date);  

$diff = abs($date2 - $date1);  
$years = floor($diff / (365*60*60*24));  
$months = floor(($diff - $years * 365*60*60*24) 
                       / (30*60*60*24));  

printf("%d months",$months);  

You can Also Get it by Mysql querying to server 你也可以通过Mysql查询到服务器来获取它

SELECT TIMESTAMPDIFF(MONTH, '2019-01-01', (SELECT DATE_ADD('2019-02-28', INTERVAL 1 DAY))) as month

You can Try it.Hope it Helps. 你可以尝试一下。希望它有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM