未定义的 date_diff()

发布于 2024-09-14 04:44:48 字数 388 浏览 5 评论 0原文

我正在尝试使用 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。

谢谢。

I'm trying to use 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');

Its doesn't work for me, gives an error:

Call to undefined function  date_diff()

How can I get it work?

PHP 5.2 is used.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

我ぃ本無心為│何有愛 2024-09-21 04:44:48

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

更新

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

<?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 \>"); 
?>

The function date_diff requires a PHP version of 5.3 or greater.

UPDATE

An example for PHP 5.2 (taken from the date_diff user comments).

<?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 \>"); 
?>
动次打次papapa 2024-09-21 04:44:48

这是一个不使用 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));
}

Here is a version that doesn't use Date objects, but these are of no use anyways in 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));
}
(り薆情海 2024-09-21 04:44:48

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

 $DateDiff = floor( strtotime($datetime2 ) - strtotime( $datetime1 ) ) / 86400 ;

//this will yield the resultant difference in days

First convert both dates to mm/dd/yyyy format then do this :

 $DateDiff = floor( strtotime($datetime2 ) - strtotime( $datetime1 ) ) / 86400 ;

//this will yield the resultant difference in days
甜嗑 2024-09-21 04:44:48
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; } 
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; } 
忆梦 2024-09-21 04:44:48

将 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 解决方法之一。

Converting your DateTime to Unix date type, and subtracting one from another:
The format->("U") is where the DateTime is converted.

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

Not sure if this is Y2K38 safe but it's one of the simplest date_diff workarounds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文