Php 异常的原因,而不是处理它们,但我已经更新,以包括关于排除异常处理的说明。从OOP显示的代码中,$epoch是一个有效值数组。如果这些值是timestampints,是的,那是行不通的,我个人发现DateTime构造远不如其他DateTime方法灵活。谢谢!
Php 异常的原因,而不是处理它们,但我已经更新,以包括关于排除异常处理的说明。从OOP显示的代码中,$epoch是一个有效值数组。如果这些值是timestampints,是的,那是行不通的,我个人发现DateTime构造远不如其他DateTime方法灵活。谢谢!,php,arrays,foreach,Php,Arrays,Foreach,异常的原因,而不是处理它们,但我已经更新,以包括关于排除异常处理的说明。从OOP显示的代码中,$epoch是一个有效值数组。如果这些值是timestampints,是的,那是行不通的,我个人发现DateTime构造远不如其他DateTime方法灵活。谢谢!只是想让你知道,作者一直是。现在已删除的答案解决了问题,这导致OP现在遇到的时间戳数组出现问题。@fyrye再次感谢。我看到了你关于数组迭代的帖子,这是一个很好的解释,我不想对你的答案进行讨论,但我想对问题上显示的具体错误(仅)进行分类和澄清。
异常的原因,而不是处理它们,但我已经更新,以包括关于排除异常处理的说明。从OOP显示的代码中,
$epoch
是一个有效值数组。如果这些值是timestampint
s,是的,那是行不通的,我个人发现DateTime
构造远不如其他DateTime方法灵活。谢谢!只是想让你知道,作者一直是。现在已删除的答案解决了问题,这导致OP现在遇到的时间戳数组出现问题。@fyrye再次感谢。我看到了你关于数组迭代的帖子,这是一个很好的解释,我不想对你的答案进行讨论,但我想对问题上显示的具体错误(仅)进行分类和澄清。很多关于SO的帖子都是OP问题的多米诺骨牌效应…@fyrye我已经对我的答案进行了大量修改。我还删除了关于时区的部分。干杯
// getting controller ap info //
$name=$status=$uptime=$last_seen=[];
foreach ($ligowave->result as $index => $obj) {
$name[] = $obj->name;
$status[] = $obj->status;
$uptime[] = $obj->uptime;
$last_seen[] = $obj->last_seen;
}
// time settings //
$epoch = $uptime;
$uptimetime = (new DateTime("@$epoch"))->format(' H:i:s');
$epoch = $last_seen;
$lastseendate = (new DateTime("@$epoch"))->SetTimeZone(new DateTimeZone('Europe/Amsterdam'))->format(' d-m-Y H:i:s');
if ($status == "up") {
echo $name;
echo " is up, ";
echo "uptime is:" . $uptimetime;
} else {
echo $name;
echo " is down, ";
echo "device is last seen:" . $lastseendate;
}
return array($name, $status, $epoch, $uptimetime, $lastseendate);
}
//...
// getting controller ap info //
$values = [];
foreach ($ligowave->result as $index => $obj) {
//convert the unix timestamps to DateTime objects
$uptime = (new DateTime('@' . $obj->uptime));
$last_seen = (new DateTime('@' . $obj->last_seen))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
//store the return values into an array
$values[] = $value = [
'name' => $obj->name,
'status' => $obj->status,
'uptimetime' => $uptime->format('H:i:s'),
'lastseendate' => $last_seen->format('d-m-Y H:i:s')
];
//output each of the statuses
printf('%s is %s, ', $obj->name, $obj->status);
if ('up' === $obj->status) {
echo 'uptime is: ' . $value['uptimetime'];
} else {
echo 'device is last seen: ' . $value['lastseendate'];
}
}
return $values;
}
foo is up, uptime is: 10:20:54
bar is down, device is last seen: 01-06-2019 08:22:30
array (
0 =>
array (
'name' => 'foo',
'status' => 'up',
'uptimetime' => '10:20:54',
'lastseendate' => '05-06-2019 11:16:21',
),
1 =>
array (
'name' => 'bar',
'status' => 'down',
'uptimetime' => '10:20:54',
'lastseendate' => '01-06-2019 08:22:30',
),
)
//...
$uptime = (new DateTime('@' . $obj->uptime))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$last_seen = (new DateTime('@' . $obj->last_seen))->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$values[] = $value = [
'name' => $obj->name,
'status' => $obj->status,
'lastseendate' => $last_seen->format('d-m-Y H:i:s'),
'uptimetime' => $uptime->diff($last_seen)->format('%a.%H:%I:%S'), //DD.HH:MM:SS
];
//...
foo is up, uptime is: 12.22:48:26
$epoch = $uptime;
try{
/***
* Uptime appears to be a numeric array of time string values
* Therefore go for the first one.
* You may want to wrap this code in a loop to catch each one.
***/
$uptimeTime = new DateTime("@".(string)$epoch[0]);
}
catch (Exception $ex){
/***
* If you wish to ignore these errors simply leave this block empty
***/
error_log("There was a problem on line ".__LINE__."! ".print_r($ex));
}
/***
* By default UTC timestamps are not zoned.
***/
// $uptimeTime->setTimeZone(new DateTimeZone('Europe/Amsterdam'));
$uptimeTimeOutput = $uptimeTime->format('H:i:s');
/***
* $uptimeTimeOutput is the correctly formatted date from $epoch[0]
***/
print $uptimeTimeOutput;