首页 > 解决方案 > 从日期时间减少浮点数组

问题描述

基本上,在减少数组时,它会错误地将小时部分中的数字相加。

我有以下功能

function reduceHours($carry, $item)
{
    if (! array_key_exists($item["recruiter"], $carry))
        $carry[$item["recruiter"]] = 0;
    $carry[$item["recruiter"]] += $item["hours"];
    return $carry;
}

这是它正在减少的原始数组

    SimpleXMLElement Object
(
    [orderId] => 3988445
    [status] => filled
    [shiftStartTime] => 2019-01-11T07:00:00
    [shiftEndTime] => 2019-01-11T15:30:00
    [tempId] => 57921
    [firstName] => SimpleXMLElement Object
        (
        )

    [lastName] => SimpleXMLElement Object
        (
        )

    [clientId] => 1543
    [clientName] => SimpleXMLElement Object
        (
        )

    [regionName] => SimpleXMLElement Object
        (
        )

    [orderSpecialty] => School
    [orderCertification] => SLP
    [floor] => SimpleXMLElement Object
        (
        )

    [shiftNumber] => 1
    [note] => SimpleXMLElement Object
        (
        )

    [payrollNumber] => SimpleXMLElement Object
        (
        )

    [lessLunchMin] => SimpleXMLElement Object
        (
        )

    [dateTimeCreated] => 2018-08-06T17:03:34
    [takenBy] => 480
    [bookedByUserId] => 480
    [orderTypeId] => 1
    [orderType] => SimpleXMLElement Object
        (
        )

    [city] => Media
    [state] => xx
    [zipCode] => xxxxx
    [orderSourceID] => SimpleXMLElement Object
        (
        )

    [orderSourceName] => SimpleXMLElement Object
        (
        )

    [lt_orderID] => 0
    [dateTimeModified] => 2018-08-06T17:03:34
    [subjectID] => SimpleXMLElement Object
        (
        )

    [subject] => SimpleXMLElement Object
        (
        )
    [Recruiter] => Name
    [Hours] => 8
)

简化为看起来像这样的数组

(
    [Max ] => 23.8
    [Allison ] => 5.3
    [Travis ] => 74.4
    [Alyssa ] => 12
    [total] => 6703.83
)

它有效,但我得到了错误的总数。原始变量是这样的日期时间间隔

DateInterval Object
(
    [y] => 0
    [m] => 0
    [d] => 0
    [h] => 7
    [i] => 30
    [s] => 0
    [f] => 0
    [weekday] => 0
    [weekday_behavior] => 0
    [first_last_day_of] => 0
    [invert] => 0
    [days] => 0
    [special_type] => 0
    [special_amount] => 0
    [have_weekday_relative] => 0
    [have_special_relative] => 0
)

这是基于时间的,所以它以 60 为底。从第一个数组中可以看出,我得到了 4 和 8,有时还有 2 个以上的小数位。

如果您需要发布更多代码或结果,请告诉我。

标签: php

解决方案


所以你的问题是使用dateinterval对象来获取时差。但是他们你只是把小时和分钟加起来,但这是“基数 60”(1 小时 60 分钟),所以你不能把它们加起来+

示例:1 小时 15 分钟 ( 1.15) 加上 1 小时 45 分钟 ( 1.45) 从2.6数字上讲是 3 小时(根据您的评论)。

为了解决这个问题,您需要转换为数字。您可以使用以下功能:

function convertToDecimal($hours) {
    $parts = explode('.', (string)$hours);
    $min = (count($parts) == 2) ? $parts[1] / 60 : 0;
    return $parts[0] + $min;
}

当调用convertToDecimal(1.15)将返回1.25。将其添加到您的 reduce 函数中:

carry[$item["recruiter"]] += convertToDecimal($item["hours"]);

提示 - 正确提出问题并非易事 - 既然您有了解决方案,请尝试编辑您的问题,以便它包含所有(且)理解您的问题并重现问题所需的数据。

更好的解决方案包括更改提取时间差的方式。考虑以下方法:

$shiftStartTime = "2019-01-11T07:00:00";
$shiftEndTime = "2019-01-11T15:15:00";

$startLong = (new DateTime($shiftStartTime))->getTimestamp();
$endLong = (new DateTime($shiftEndTime))->getTimestamp();;

echo ($endLong - $startLong) / (60 * 60);

这将8.25在您计算秒数时为您提供,而不是使用 DateInterval 设置小时数。

希望有帮助!


推荐阅读