2017-07-18 114 views
-1

我有以下格式日期时间:
开始:2017年7月16日20:00
结束:2017年7月16日23:30PHP多少小时都在一定的时间间隔

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

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

在此先感谢您的任何提示。

当前的代码我是这样的,但我不知道这是否是涵盖了所有的时间内可能:

<?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

你能提供一些代码和预期的输出吗?是第一个例子2小时和1.5小时的答案? – Andreas

+0

@Andreas是的,第一个例子确实应该提供上述金额。我试图实现的代码与下面两个日期之间的时间差异相同。 – ediets

+0

我们要求代码的原因是因为我们想知道您尝试了什么。而不必从头开始。并使用相同的变量,以便您不必将它们更改为变量。 – Andreas

回答

0

我相信您的解决方案的一部分被发现hereAillyn

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

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

由于在一小时内有3600秒并使用一轮() 以避免有很多小数位,所以除以3600。

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

这会给你两个时间之间的圆角小时的时差。你可以做类似的事情,只要在每个月的每个时间间隔应用它并调整你的数学。实际上你可以调用一个PHP函数来解决这个问题。

+1

请不要张贴链接唯一的答案。如果链接更改/获取被删除,答案将是未来的访客无用的。在实际答案中加入实际执行情况,并将链接作为额外参考。但是,如果链接到SO上的另一个帖子来解决问题,则应将问题标记为重复。 –

+0

看起来像重复给我。 –

+0

不确定@Dont OP是否需要一个月的总计,并在两个单独的“类别”中正确理解。 – Andreas

0

这会为你工作吗?

$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小时;与“小区”替换“地板”对面)。

+0

这就是我现在得到的。这应该进一步修改为上述间隔,并检查一个部分和另一个部分有多少个小时。 – ediets

+1

@ediets - 这是为什么添加现有代码对您很重要的一个很好的理由。我们真的不想花时间写出与您已有的代码相同的示例。 –

+0

@MagnusEriksson - 这正是我想要做的,但我不确定它是否会涵盖所有可能的时间组合:[code](https://pastebin.com/SgcY62gm) – ediets