【问题标题】:How to exact difference between 2 dates in php如何在php中精确区分2个日期
【发布时间】:2013-12-24 15:07:47
【问题描述】:

我想在 php 中找到 2 个日期之间的差异。但我没有得到确切的差异。 请帮我。 我必须得到的输出是“2 年 0 个月 0 天”。 但我得到的是“1 年 12 个月零 4 天”。 我哪里错了?

$createddate="2015-12-24";

//find difference between the dates present-createddate of user
     $now = time(); // present time 
     $your_date = strtotime($createddate);

   $difference = abs($now - $your_date);
   echo $difference;

     // Years, months and days version
$years = floor($difference / (365*60*60*24));
//echo $years;
$months = floor(($difference - $years * 365*60*60*24) / (30*60*60*24));
//echo $months;
$days = floor(($difference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24));
//echo $days;
$membersince .= $years.' years '.$months.' months and '.$days.' days';

【问题讨论】:

标签: php date strtotime date-math


【解决方案1】:
<?php
$datetime1 = new DateTime();
$datetime2 = new DateTime('2015-12-24');
$interval  = $datetime1->diff($datetime2);
$elapsed   = $interval->format('%y years, %m months, %d days');
echo $elapsed;

See it in action

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多