gpt4 book ai didi

php - 如果日期已经存在或有重复,则求和时间

转载 作者:搜寻专家 更新时间:2023-10-31 21:36:05 26 4
gpt4 key购买 nike

我有这个数组

$date = array{"2013-09-17 00:21:00",
"2013-09-23 00:12:00",
"2013-09-23 00:41:00",
"2013-09-20 00:13:00",
"2013-09-19 00:34:00",
"2013-09-17 00:38:00"}

我正在尝试对数组中具有相同日期的时间求和。

这是我的预期输出:

 $date = array{"2013-09-17 00:59:00",
"2013-09-23 00:53:00",
"2013-09-20 00:13:00",
"2013-09-19 00:34:00"}

这就是我到目前为止所尝试的

foreach($date as $key => $value)
{
$lf_date[] = date("Y-m-d",strtotime($value));
$lf_time[] = date("H:i:s",strtotime($value));

if(isset($lf_date[$key]))
{
$output[] += $lf_time[$key];
}
else
{
$output[] = $lf_time[$key];
}

}

这给了我一个 0 输出 T_T.. 我已经尝试在谷歌上搜索并且它说我必须使用 issetarray_key_exists 但我无法得到它上类。 :(。感谢任何可以帮助我的人。

最佳答案

使用:

<?php
$date = array("2013-09-17 00:21:00",
"2013-09-23 00:12:00",
"2013-09-23 00:41:00",
"2013-09-20 00:13:00",
"2013-09-19 00:34:00",
"2013-09-17 00:38:00");

$array = array();
foreach($date as $key => $value)
{
$lf_date = date("Y-m-d",strtotime($value));
$lf_time = date("H:i:s",strtotime($value));

$midnight = strtotime("0:00");

if(!isset($array[$lf_date]))
$array[$lf_date] = 0;//check is array index exists

$array[$lf_date] += strtotime($lf_time) - $midnight;
}

foreach($array as $key => $value)
{
$midnight = strtotime("0:00");
$array[$key] = $key." ".date("G:i:s", $midnight + $value);
}

$result = array_values($array);

print_r($result);

?>

输出:

Array
(
[0] => 2013-09-17 0:59:00
[1] => 2013-09-23 0:53:00
[2] => 2013-09-20 0:13:00
[3] => 2013-09-19 0:34:00
)

关于php - 如果日期已经存在或有重复,则求和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19221432/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com