2

PHP中计算指定格式的两个日期之间的天数差异的最佳(日期格式独立方式)是什么。

我尝试了以下功能:

function get_date_offset($start_date, $end_date)
{
  $start_time = strtotime($start_date);
  $end_time = strtotime($end_date);
  return round(($end_time-$start_time)/(3600*24));
}

它在 linux 机器上工作正常,但是在 windows 下运行时 strtotime 返回 ''。

编辑

输入日期为mm/dd/yyyy格式,但我想让它接受 $format 作为参数。

我只需要几天的差异。

4

11 回答 11

6

如果您不想要或无法使用 Zend Framework 或 Pear 包,请尝试此功能,我希望这会有所帮助:

function dateDifference($date1, $date2)
{
    $d1 = (is_string($date1) ? strtotime($date1) : $date1);
    $d2 = (is_string($date2) ? strtotime($date2) : $date2);

    $diff_secs = abs($d1 - $d2);
    $base_year = min(date("Y", $d1), date("Y", $d2));

    $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);

    return array
    (
        "years"         => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
        "months_total"  => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
        "months"        => date("n", $diff) - 1,
        "days_total"    => floor($diff_secs / (3600 * 24)),
        "days"          => date("j", $diff) - 1,
        "hours_total"   => floor($diff_secs / 3600),
        "hours"         => date("G", $diff),
        "minutes_total" => floor($diff_secs / 60),
        "minutes"       => (int) date("i", $diff),
        "seconds_total" => $diff_secs,
        "seconds"       => (int) date("s", $diff)
    );
}
于 2008-12-23T14:56:41.960 回答
4

PEAR Date 类提供了各种功能来查找日期之间的差异以及大约 1000 种其他事物。它的文档在这里...

于 2008-12-23T11:54:28.910 回答
3

PHP 的问题在于它没有明确的 DateTime 类型。您可以使用 Unix 时间戳或内置的 DateTime 类,但它们的功能非常有限。我希望应该有一些 3rd 方类对日期时间计算有更广泛的支持,但我没有寻找它。

使用 Unix 时间戳进行日期(而不是时间)计算也很棘手。您必须放弃时间部分,但由于夏令时 (DST),简单地重置为 00:00 并不安全。DST 的效果是每年有两天不完全是 24 小时。因此,在添加/减去日期时,您最终可能会得到一个不能与 3600*24 均分的值。

我建议寻找一些对所有这些东西有适当支持的第 3 方类。日期/时间计算的丑陋令人敬畏。:P

于 2008-12-23T11:53:21.673 回答
2

Zend 框架有Zend_Date类来处理“日期数学”。它通过使用 BCMath 扩展来解决系统特定的时间戳限制,或者如果不可用,则通过系统的最大浮点值限制时间戳。

// example printing difference in days
require('Zend/Date.php');

$date1 = new Zend_Date();
$date1->set(2, Zend_Date::MONTH);
$date1->set(27, Zend_Date::DAY);
$date1->set(2008, Zend_Date::YEAR);

$date2 = new Zend_Date();
$date2->set(3, Zend_Date::MONTH);
$date2->set(3, Zend_Date::DAY);
$date2->set(2008, Zend_Date::YEAR);

echo ($date2->getTimestamp() - $date1->getTimestamp()) / (3600*24);
于 2008-12-23T13:40:10.833 回答
1

我不确定什么是最好的,因为 PHP 中没有内置函数可以做到这一点,但有些人使用了gregoriantojd(),例如在这个论坛帖子中。

于 2008-12-23T11:40:29.780 回答
1

gregoriantojd() 给出的结果与使用 strtotime() 的结果相同,请参阅此博客文章了解如何操作:

http://www.phpro.org/examples/Calculate-Age-With-PHP.html

于 2009-05-12T23:51:39.693 回答
1

以下对我有用。相信我在某个地方的 php.net 文档中找到了它。

*编辑 - 糟糕,没有看到 csl 的帖子。这是他链接中的确切功能,一定是我找到它的地方。;)

//Find the difference between two dates
function dateDiff($startDate, $endDate)
{
    // Parse dates for conversion
    $startArry = date_parse($startDate);
    $endArry = date_parse($endDate);

    // Convert dates to Julian Days
    $start_date = gregoriantojd($startArry["month"], $startArry["day"], $startArry["year"]);
    $end_date = gregoriantojd($endArry["month"], $endArry["day"], $endArry["year"]);

    // Return difference
    return round(($end_date - $start_date), 0);
}
于 2009-05-13T00:04:37.083 回答
1

为了显示事件的持续时间,我试图计算两个日期的差异。如果事件的持续时间为周五 17:00 到周日 15:00,则针对该问题给出的大多数函数都会失败。我的目标是找出日期之间的差异,例如:

date('Ymd',$end)-date('Tmd',$begining);

但这很可能会失败,因为一年没有 99 个月,一个月没有 99 天。我可以将日期字符串转换为 UNIX 时间戳并除以 60*60*12,但有些日子的小时数或多或少,有时甚至是闰秒。因此,我使用 getdate() 创建了自己的函数,该函数返回有关时间戳的信息数组。

/*
 * datediff($first,$last)
 * $first - unix timestamp or string aksepted by strtotime()
 * $last - unix timestamp or string aksepted by strtotime()
 * 
 * return - the difference in days betveen the two dates
 */
function datediff($first,$last){
 $first = is_numeric($first) ? $first : strtotime($first);
 $last  = is_numeric($last ) ? $last  : strtotime($last );
 if ($last<$first){
  // Can't calculate negative difference in dates
  return -1;
 }
 $first = getdate($first);
 $last =  getdate($last );
 // find the difference in days since the start of the year
 $datediff = $last['yday'] - $first['yday'];
 // if the years do not match add the appropriate number of days.
 $yearCountedFrom = $first['year'];
 while($last['year'] != $yearCountedFrom ){
  // Take leap years into account
  if( $yearCountedFrom % 4 == 0 && $yearCountedFrom != 1900 && $yearCountedFrom != 2100 ){
   //leap year
   $datediff += 366;
  }else{
   $datediff += 365;
  }
  $yearCountedFrom++;
 }
 return $datediff;
}

关于 GregorianToJD() 函数,它可能会起作用,但我有点不安,因为我不明白它是如何工作的。

于 2009-09-05T06:55:13.093 回答
0

伙计们,我们不要过度设计。

$d1 = strtotime($first_date);
$d2 = strtotime($second_date);
$delta = $d2 - $d1;
$num_days = ($delta / 86400);
于 2008-12-23T18:57:47.543 回答
0

使用 PHP 计算两个日期(和时间)之间的差异。以下页面提供了一系列不同的方法(总共 7 种),用于使用 Php 执行日期/时间计算,以确定两个日期之间的时间(小时、分钟)、天、月或年的差异。

请参阅PHP 日期时间 - 计算 2 个日期之间差异的 7 种方法

于 2010-01-06T06:38:21.887 回答
0

PHP 5.2 引入了DateTimeDateInterval类,这使得这很容易:

function get_date_offset($start_date, $end_date)
{
    $start_time = new DateTime($start_date);
    $end_time   = new DateTime($end_date);
    $interval   = $end_time->diff($start_time);
    return $interval->format('%a days');
}
于 2013-01-30T02:12:17.660 回答