【问题标题】:how may days are between two dates in specific year在特定年份的两个日期之间有多少天
【发布时间】:2018-02-06 11:45:42
【问题描述】:

我正在解决以下任务>

我有两个日期 - $start 和 $end,目标年份为 $year。

日期是 php DateTime 对象,年份是字符串。

add:dates 来自这种格式的 MySql 字段 2017-02-01 15:00:00 ... add2:如果结束日期为空,我使用今天的日期...

我需要计算出这两个日期之间特定年份的天数。

我还需要将它整整一整天,即使一天中的一分钟应该算作一整天......

我可以通过以下许多 ifs 来解决它。

我在示例中使用的值的预期结果是 2016 是 0 天 2017 年是 31 天 2018年是32天 2019 是 0 天

但是有什么优雅的 php 函数可以帮助我解决这个问题吗? 我所做的似乎是错误的方式并给出了不好的结果 - 似乎只计算一整天......

请在此处查看我的代码>

<?php
$diff = True;
$start = DateTime::createFromFormat('Y-m-d H:i:s','2017-12-01 23:05:00');
$end = DateTime::createFromFormat('Y-m-d H:i:s','2017-12-03 00:05:00');
$year = '2017';

// start date
if ($start->format('Y')<$year)
{
    $newstart = new DateTime('first day of January '. $year);
}

if ($start->format('Y')==$year)
{
    $newstart = $start;
}

if ($start->format('Y')>$year)
{
    $result = 0;
    $diff = False;
}

// end date
if ($end->format('Y')>$year)
{
    $newend = new DateTime('last day of December '. $year);
}

if ($end->format('Y')==$year)
{
    $newend = $end;
}

if ($end->format('Y')<$year)
{
    $result = 0;
    $diff = False;
}               

// count if diff is applicable
if ($diff)
{
    $result  = $newend->diff($newstart)->format("%a");
}

echo $result;
?>

【问题讨论】:

  • 由于四舍五入,不完全是重复的,但这可以让你开始stackoverflow.com/questions/15688775/…
  • “但是有什么优雅的 php 函数可以帮助我解决这个问题吗?” -- 阅读DateTime::diff()。它返回一个包含天数的DateInterval 对象(在$days 中),通过检查$h$i$s 的值,您可以判断是否必须将其递增以舍入结果。您还可以使用min()max() 将时间间隔裁剪为所需年份。
  • 非常感谢您的回答。如您所见,我已经使用了DateTime:diff(); 我将输入四舍五入,所以它只是日期,而忽略了时间。我想知道的是它总是少一天返回 - 比如 2017-12-01 到 2017-12-05 是 5 天,但它总是返回 4。也许我误解了“差异”这个词,但我需要这些天之间的总天数包括那些日子。解决方案似乎是在计算结束时添加+1?
  • 你提到了min()max(),似乎这可以避免我愚蠢的ifs,但我不知道如何裁剪这个间隔,请给我一个简短的例子吗?

标签: php datetime


【解决方案1】:

但是有什么优雅的 php 函数可以帮助我解决这个问题吗?

了解DateTime::diff()。它返回一个包含天数的DateInterval 对象(在$days 中),通过检查$h$i$s 的值,您可以判断是否必须将其递增以舍入结果。您还可以使用min()max() 将时间间隔裁剪为所需的年份。

function getDays(DateTimeInterface $start, DateTimeInterface $end, $year)
{
    // Extend the start date and end date to include the entire day
    $s = clone $start;            // Don't modify $start and $end, use duplicates
    $s->setTime(0, 0, 0);
    $e = clone $end;
    $e->setTime(0, 0, 0)->add(new DateInterval('P1D'));  // start of the next day

    // Crop the input interval to the desired year
    $s = min($s, new DateTime("$year-01-01 00:00:00"));
    $year ++;
    $e = max(new DateTime("$year-01-01 00:00:00"), $end);     // start of the next year

    if ($e <= $s) {
        // The input interval does not span across the desired year
        return 0;
    }

    // Compute the difference and return the number of days
    $diff = $e->diff($s);

    return $diff->days;
}

【讨论】:

    【解决方案2】:
        $d1 = strtotime('2017-05-15');
        $d2 = strtotime('2017-05-31');
        $div = 24 * 3600;
        echo abs(($d2 - $d1) / $div); // 16 days
    

    只需确保并且只有日期部分,您不必处理四舍五入。

    【讨论】:

    • 您好,谢谢您的建议。它没有反映的是需要指定年份......所以对于不同年份的日期它不起作用,最好说它会返回指定年份之间的总天数而不是天数。
    • 你试过代码了吗?它肯定会适用于不同的年份。我将 $d1 更改为 '2016-05-15' 并返回 381 天 - 即 365 + 16。不过,您可能对 1970 年之前的日期有一些问题。至于“指定年份的天数” - 哪一年?这是另一个计算。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多