【问题标题】:PHP DateTime Difference (Hours, Days, Weeks, Months)PHP DateTime 差异(小时、天、周、月)
【发布时间】:2017-08-14 19:55:51
【问题描述】:

我正在创建 PHP 函数,它将以以下格式返回两个日期之间的差异:2 个月、3 周、6 天、3 小时。我曾尝试使用 PHP DateTime 类,但它只返回月、日和小时,我找不到计算周数的方法。

这是我的功能:

public function DateTimeDifference($FromDate, $ToDate) {
  $FromDate = new DateTime($FromDate);
  $ToDate   = new DateTime($ToDate);
  $Interval = $FromDate->diff($ToDate);

  $Difference["Hours"] = $Interval->h;
  $Difference["Days"] = $Interval->d;
  $Difference["Months"] = $Interval->m;

  return $Difference;
}

现在,我需要返回数据中也包含 $Difference["Weeks"]。

编辑:我知道我可以将天数除以 7 并得到数周,但这并不正确。例如:2 个月、14 天、3 小时 - 当我将 14 天 除以 7 时,我会得到:2 个月、2 周、14 天, 3 小时,现在不是同一时期。

【问题讨论】:

  • 有趣...一周有多少天?
  • 有趣...如何从天除以 7 求模?
  • 你甚至不需要使用模数,这是简单的数学运算。
  • @Andreas,我听说有些国家的计算机科学与数学分开教授。没关系)但是 (days-weeks*7) === (days - floor(days/7)*7) === days%7, 这是模数。

标签: php datetime date-difference


【解决方案1】:
public function DateTimeDifference($FromDate, $ToDate) {
  $FromDate = new DateTime($FromDate);
  $ToDate   = new DateTime($ToDate);
  $Interval = $FromDate->diff($ToDate);

  $Difference["Hours"] = $Interval->h;
  $Difference["Weeks"] = floor($Interval->d/7);
  $Difference["Days"] = $Interval->d % 7;
  $Difference["Months"] = $Interval->m;

  return $Difference;
}

【讨论】:

【解决方案2】:
// this will only work from previous dates 
// difference between utc date time and custom date time

function FromUtcToCustomDateTimeDifference($ToDate)
{
    
    // Takes Two date time
    $UTC_DATE = new DateTime('now', new DateTimeZone('UTC'));
    $UTC_DATETIME = $UTC_DATE->format('Y-m-d H:i:s');
    
    // add your own date time 1
    $datetime1 = date_create(UTC_DATETIME);
    $datetime2 = date_create($ToDate);
   
    $Interval = date_diff($datetime1, $datetime2);

    // Count Number Of Days Difference
    
    $Day = $Interval->format('%a');
        
    if($Day > 1)
        {

            $Month = $Interval->format('%m');
            $Year = $Interval->format('%y');
            $Week = (int)($Interval->format('%a')/7);
            
            if($Year<1)
            {

                        if($Day <= 7)
                        {
                            return $Day > 1 ?  $Day.= " days ago" : $Day .= " day ago";

                        }

                        else if($Month<1)
                        {
                            
                        return $Week > 1 ?  $Week.= " weeks ago" : $Week .= " week ago";
                        }
                        
                        
                        return $Month > 1 ? $Month.= " months ago" : $Month .= " month ago";
            }
            else
            {
                return $Year > 1 ? $Year.= " years ago" : $Year .= " year ago";
                
            }

        
        }
        else
        {

            return "today";

        }

}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2011-09-30
  • 2021-07-18
  • 2018-07-30
  • 1970-01-01
  • 1970-01-01
  • 2019-08-11
  • 2020-02-11
  • 1970-01-01
  • 2016-08-30
相关资源
最近更新 更多