【问题标题】:Undefined date_diff()未定义的 date_diff()
【发布时间】:2010-08-13 09:41:33
【问题描述】:

我正在尝试使用date_diff()

$datetime1 = date_create('19.03.2010');
$datetime2 = date_create('22.04.2010');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('%R%d days');

它对我不起作用,给出一个错误:

Call to undefined function  date_diff()

我怎样才能让它工作?

使用 PHP 5.2。

谢谢。

【问题讨论】:

    标签: php datediff


    【解决方案1】:

    函数 date_diff 需要 PHP 5.3 或更高版本。

    更新

    PHP 5.2 的示例(取自 date_diff 用户 cmets)。

    <?php 
    function date_diff($date1, $date2) { 
        $current = $date1; 
        $datetime2 = date_create($date2); 
        $count = 0; 
        while(date_create($current) < $datetime2){ 
            $current = gmdate("Y-m-d", strtotime("+1 day", strtotime($current))); 
            $count++; 
        } 
        return $count; 
    } 
    
    echo (date_diff('2010-3-9', '2011-4-10')." days <br \>"); 
    ?>
    

    【讨论】:

    • 有什么方法可以在 PHP 5.2 上使用它?
    • 这个方案效率很低
    • 您问如何让 date_diff 工作,我给出了明确的答案,您不能,并提供了一个简单的解决方案,该解决方案直接来自 date_diff 的用户 cmets,我只是复制粘贴了它。有很多方法可以解决这个问题。减去两个 strtotime 值并将其除以正确的段将是另一种方法。如果您想要一种“简单”的方式,请升级 PHP。
    【解决方案2】:

    这是一个不使用 Date 对象的版本,但这些在 5.2 中无论如何都没有用。

    function date_diff($d1, $d2){
        $d1 = (is_string($d1) ? strtotime($d1) : $d1);
        $d2 = (is_string($d2) ? strtotime($d2) : $d2);  
        $diff_secs = abs($d1 - $d2);
        return floor($diff_secs / (3600 * 24));
    }
    

    【讨论】:

    • 我不建议使用这种方法,因为它不考虑可能的夏令时更改: ini_set('date.timezone', 'America /洛杉矶'); echo _date_diff('2011-03-13', '2011-03-14'); >> 0
    【解决方案3】:

    首先将两个日期转换为 mm/dd/yyyy 格式,然后执行以下操作:

     $DateDiff = floor( strtotime($datetime2 ) - strtotime( $datetime1 ) ) / 86400 ;
    
    //this will yield the resultant difference in days
    

    【讨论】:

    • 很棒的解决方案 sqlchild
    • @JonMatthews : 非常感谢先生,我的荣幸
    【解决方案4】:
    function date_diff($date1, $date2) { 
    $count = 0; 
    //Ex 2012-10-01 and 2012-10-20
    if(strtotime($date1) < strtotime($date2))
    {                       
      $current = $date1;                
      while(strtotime($current) < strtotime($date2)){ 
          $current = date("Y-m-d",strtotime("+1 day", strtotime($current))); 
          $count++; 
      } 
    }                 
    //Ex 2012-10-20 and 2012-10-01
    else if(strtotime($date2) < strtotime($date1))
    {           
      $current = $date2;                
      while(strtotime($current) < strtotime($date1)){ 
          $current = date("Y-m-d",strtotime("+1 day", strtotime($current))); 
          $count++;  
      }
      $current = $current * -1;
    }
    return $count; } 
    

    【讨论】:

    • 请解释你的答案。
    【解决方案5】:

    将您的 DateTime 转换为 Unix 日期类型,并从另一个中减去: format->("U") 是转换日期时间的地方。

    $datetime1 = date_create('19.03.2010');
    $datetime2 = date_create('22.04.2010');
    $intervalInDays = ($datetime2->format("U") - $datetime1->format("U"))/(3600 * 24);
    

    不确定这是否是 Y2K38 安全的,但它是最简单的 date_diff 解决方法之一。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多