【问题标题】:PHP how many hours are in certain interval [duplicate]PHP在某个时间间隔内有多少小时[重复]
【发布时间】:2017-07-18 19:44:41
【问题描述】:

我有以下格式的日期时间:
开始时间:2017-07-16 20:00
结束:2017-07-16 23:30

开始:2017-07-18 21:30
结束:2017-07-19 00:30

我需要从这些间隔中得知一个月内在 18:00-22:00 和 22:00-06:00 之间总共花费了多少小时(以 0.5 为增量)。

提前感谢您的任何提示。

我目前的代码是这样的,但我不确定它是否涵盖了所有时间范围的可能性:

<?php
  date_default_timezone_set("UTC");

  $start = array(
    "2017-07-16 21:30:00",
    "2017-07-16 18:00:00"
  );
  $end = array(
    "2017-07-17 00:30:00",
    "2017-07-16 18:30:00"
  );

  $amount_low = 0;
  $amount_high = 0;

  for($i = 0; $i < sizeof($start); $i++) {
    $start_time = date("H:i:s", strtotime($start[$i]));
    $end_time   = date("H:i:s", strtotime($end[$i]));

    $start_date = date("Ymd", strtotime($start[$i]));
    $end_date   = date("Ymd", strtotime($end[$i]));

    // getting chunk before 22:00 if
    if(
        (strtotime($start[$i]) >= strtotime($start_date . " 18:00") && strtotime($start[$i]) < strtotime($start_date . " 22:00"))
        &&
        $start_date < $end_date
      ) {

      $interval_low = strtotime($start_date . " 22:00") - strtotime($start[$i]);
      $amount_low += ceil($interval_low / 1800) / 2;

    }

    //amount_high
    if(strtotime($start[$i]) > strtotime($start_date . " 22:00") && strtotime($start[$i]) < strtotime($start_date . " 06:00")) {

      $interval_high = strtotime($end[$i]) - strtotime($start[$i]); //needs further things
      $amount_high += ceil($interval_high / 1800) / 2;

    } elseif (strtotime($start[$i]) < strtotime($start_date . " 22:00") && strtotime($end[$i]) > strtotime($start_date . " 22:00")) {

      $interval_high = strtotime($end[$i]) - strtotime($start_date . " 22:00");
      $amount_high += ceil($interval_high / 1800) / 2;
    } else {

      $interval_low = strtotime($end[$i]) - strtotime($start[$i]);
      $amount_low += ceil($interval_low / 1800) / 2;

    }

  }
  echo $amount_low;
  echo "\n$amount_high";
?>

【问题讨论】:

  • 你能提供一些代码和预期的输出吗?第一个例子的答案是 2 小时和 1.5 小时吗?
  • @Andreas 是的,对于第一个示例,它确实应该给出上述金额。我尝试实现的代码与下面提到的两个日期之间的时间差相同。
  • 我们要求提供代码的原因是因为我们想知道您尝试过什么/尝试过什么。并且不必从头开始。并使用与您相同的变量,这样您就不必将它们更改为您的变量。
  • 代码被添加到问题中,我相信这不是重复的。
  • 太晚了。以此为你的教训。写下你的问题,这样就不会有任何后续问题或任何不清楚的地方。我的建议。删除这个问题并写一个新问题。记住这一课。

标签: php datetime dateinterval


【解决方案1】:

这对你有用吗?

$start = strtotime('2017-07-16 20:00');
$end = strtotime('2017-07-16 23:30');

$interval = $end - $start;

$hours = floor($interval / 1800)/2;

echo($hours);

这将显示两个日期之间的总小时数(以 0.5 为增量)(向下舍入,因此如果是 55 分钟,则为 0.5 小时;相反,将 'floor' 替换为 'ceil')。

【讨论】:

  • 这就是我现在得到的。这应该针对上述间隔进一步修改,并检查一个部分和另一个部分的小时数。
  • @ediets - 这是添加现有代码对您很重要的一个很好的理由。我们真的不想花时间编写与您已经拥有的代码相同的示例。
  • @MagnusEriksson - 这是我试图做的,但我不确定它是否会涵盖所有可能的时间组合:code
  • @ediets - 将所有相关代码添加到您的问题中,而不是链接到外部网站。
  • @MagnusEriksson 完成,请参阅原始问题。
【解决方案2】:

我相信您的解决方案的一部分是从Aillynhere 找到的

您可以将它们转换为时间戳并从那里开始:

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

除以 3600 因为一小时有 3600 秒并使用 round() 避免有很多小数位。

$hourdiff = round((strtotime($time1) - strtotime($time2))/3600, 1);

这将为您提供两个时间之间的四舍五入小时差。你可以做类似的事情,只需将它应用于每个月的每个间隔并调整你的数学。真的没有一个 PHP 函数可以调用来解决这个问题。

【讨论】:

  • 请不要仅发布链接答案。如果链接更改/删除,则答案对未来的访问者将毫无用处。在您的答案中包含实际实现并将链接发布为额外参考。但是,如果链接是指向解决问题的 SO 上的另一个帖子,则应将问题标记为重复。
  • 在我看来是重复的。
  • 不确定@Dont OP 总共要求一个月,如果理解正确,分为两个单独的“类别”。
  • 感谢@MagnusEriksson
  • 但是就像我说的,如果另一个帖子回答了这个问题,那么这个问题应该被标记为那个帖子的副本。不要重复现有的答案。如果您需要更改或继续构建该答案是可以的,但如果它是直接副本则不行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-08-13
  • 2016-04-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多