doutangqi4444 2019-08-14 06:57
浏览 95
已采纳

计算PHP中两个日期之间的差异?

I want calculate the difference between two dates in Javascript. Result should be X years, Y months, Z days

I run below code in PHP but result is different. Which is problem in here?

PHP CODE 1


// Declare and define two dates 
$date1 = strtotime("2010-04-01");  
$date2 = strtotime("2019-06-30");  

// Formulate the Difference between two dates 
$diff = abs($date2 - $date1);  


// To get the year divide the resultant date into 
// total seconds in a year (365*60*60*24) 
$years = floor($diff / (365*60*60*24));  


// To get the month, subtract it with years and 
// divide the resultant date into 
// total seconds in a month (30*60*60*24) 
$months = floor(($diff - $years * 365*60*60*24) 
                               / (30*60*60*24));  


// To get the day, subtract it with years and  
// months and divide the resultant date into 
// total seconds in a days (60*60*24) 
$days = floor(($diff - $years * 365*60*60*24 -  
             $months*30*60*60*24)/ (60*60*24)); 


// To get the hour, subtract it with years,  
// months & seconds and divide the resultant 
// date into total seconds in a hours (60*60) 
$hours = floor(($diff - $years * 365*60*60*24  
       - $months*30*60*60*24 - $days*60*60*24) 
                                   / (60*60));  


// To get the minutes, subtract it with years, 
// months, seconds and hours and divide the  
// resultant date into total seconds i.e. 60 
$minutes = floor(($diff - $years * 365*60*60*24  
         - $months*30*60*60*24 - $days*60*60*24  
                          - $hours*60*60)/ 60);  


// To get the minutes, subtract it with years, 
// months, seconds, hours and minutes  
$seconds = floor(($diff - $years * 365*60*60*24  
         - $months*30*60*60*24 - $days*60*60*24 
                - $hours*60*60 - $minutes*60));  

// Print the result 
printf("%d years, %d months, %d days, %d hours, "
     . "%d minutes, %d seconds", $years, $months, 
             $days, $hours, $minutes, $seconds);  
?>

Result is 9 years, 3 months, 2 days

HOWEVER. if I run below code, Result is 9 years, 2 months, 29days

PHP CODE 2

//差を求める日時の変数を作成
$dateTime1 = '2010-04-01';
$dateTime2 = '2019-06-30';

//DateTimeクラスで2つの日時のオブジェクトを作成
$objDatetime1 = new DateTime($dateTime1);
$objDatetime2 = new DateTime($dateTime2);

//diff()メソッドで2つの日時のオブジェクトから
//ふたつの日付の差をあらわす DateInterval オブジェクトを生成する
$objInterval = $objDatetime1->diff($objDatetime2);

//$objInterval(DateInterval オブジェクト)をformat()メソッドで日時を出力
//%Rは + や - の符号を出力するオプションです
echo $objInterval->format('%R%Y').'year<br/>'; //年
echo $objInterval->format('%R%M').'month<br/>'; //月
echo $objInterval->format('%R%D').'day<br/>'; //日
?>

9 years, 3 months, 2 days and 9 years, 2 months, 29days

what is correct?

  • 写回答

1条回答 默认 最新

  • drozwmi5440 2019-08-14 13:12
    关注

    A date difference by itself can only be precise to the Day. If you go up to the months and years, then the results will be inevitably false without a date reference.

    You can say

    9 years, 2 months, 29days till X date

    and the reference would be today.

    If you take the same period(Year, month, day), and use the date 2019-03-01 as reference, you would have the wrong result.

    Here is an example:

    $objDatetimeSource1 = new DateTime('2010-04-01');
    $objDatetimeSource2 = new DateTime('2019-06-30');
    $interval =  $objDatetimeSource1->diff($objDatetimeSource2);
    
    $objDatetime1 = new DateTime('2020-02-28');
    $objDatetime2 = new DateTime('2020-03-01');
    

    $objDatetime1->add($interval); will give 2029-05-27 00:00:00 and

    $objDatetime1->add($interval); will give 2029-05-30 00:00:00 with 3 days difference between them, when, from the dates 2020-02-28 and 2020-03-01, you can clearly see that there is only 2 days difference.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)