例如,我想在几秒钟之内将大量数字转换为结果;
但是该值以"SparkUI-54-acceptor-1@407beebf-Spark@361a6919{HTTP/1.1,[http/1.1]}{0.0.0.0:4042}" #54 daemon prio=3 os_prio=0 tid=0x00007effdd622000 nid=0x2f8c9 runnable [0x00007efef6ce0000]
java.lang.Thread.State: RUNNABLE
at sun.nio.ch.ServerSocketChannelImpl.accept0(Native Method)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:422)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:250)
- locked <0x00000006c0405bd8> (a java.lang.Object)
at org.spark_project.jetty.server.ServerConnector.accept(ServerConnector.java:371)
at org.spark_project.jetty.server.AbstractConnector$Acceptor.run(AbstractConnector.java:601)
at org.spark_project.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
at org.spark_project.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
at java.lang.Thread.run(Thread.java:748)
"SparkUI-53-acceptor-0@38dd8072-Spark@361a6919{HTTP/1.1,[http/1.1]}{0.0.0.0:4042}" #53 daemon prio=3 os_prio=0 tid=0x00007effdd620800 nid=0x2f8c8 waiting for monitor entry [0x00007efef6de1000]
java.lang.Thread.State: BLOCKED (on object monitor)
at sun.nio.ch.ServerSocketChannelImpl.accept(ServerSocketChannelImpl.java:233)
- waiting to lock <0x00000006c0405bd8> (a java.lang.Object)
at org.spark_project.jetty.server.ServerConnector.accept(ServerConnector.java:371)
at org.spark_project.jetty.server.AbstractConnector$Acceptor.run(AbstractConnector.java:601)
at org.spark_project.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:671)
at org.spark_project.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:589)
at java.lang.Thread.run(Thread.java:748)
秒存储。
这是我当前的代码,但未返回任何内容。如您所见,我正在尝试使其尽可能像人一样。
1728000
代码不够精确,例如60秒将返回function convert_seconds($seconds)
{
if ($seconds <= 60){
return "nill";
} else if ($seconds >= 60 || $seconds <= 3600){ # hr
$time = ($seconds) / 3600;
$time_val = "Hour";
} else if ($seconds >= 3600 || $seconds <= 86400){ # day
$time = 0;
$time = ($seconds) / 86400;
$time_val = "Day";
} else if ($seconds >= 86400 || $seconds <= 604800){ # week
$time = 0;
$time = ($seconds) / 604800;
$time_val = "Week";
}
if ($time_val == "Hour" || $time <= 120){
$time_val == "Hours";
} else if ($time_val == "Day" || $time <= 7200){
$time_val == "Days";
} else if ($time_val == "Week" || $time <= 172800){
$time_val == "Weeks";
}
return $time.' '.$time_val;
}
echo convert_seconds(1728000);
?如果有人知道更好的方式,我很想听听。
答案 0 :(得分:1)
您需要使用return语句。您还应该使用单个“ =”来分配变量。
function convert_seconds($seconds)
{
if ($seconds <= 60){
return "nill";
} else if ($seconds >= 60 || $seconds <= 3600){ # hr
$time = ($seconds) / 3600;
$time_val = "Hour";
} else if ($seconds >= 3600 || $seconds <= 86400){ # day
$time = 0;
$time = ($seconds) / 86400;
$time_val = "Day";
} else if ($seconds >= 86400 || $seconds <= 604800){ # week
$time = 0;
$time = ($seconds) / 604800;
$time_val = "Week";
}
if ($time_val == "Hour" || $time <= 120){
$time_val = "Hours";
} else if ($time_val == "Day" || $time <= 7200){
$time_val = "Days";
} else if ($time_val == "Week" || $time <= 172800){
$time_val = "Weeks";
}
return $time.' '.$time_val;
}
答案 1 :(得分:1)
您的代码有一些问题。首先,您需要在条件中使用&&
,而不是||
,否则您的第二个条件始终为true。另外,您的条件是$time
始终为0或1,因为您要除以该条件可能的最大值。尝试以下方法:
function convert_seconds($seconds) {
if ($seconds < 60){
return "nill\n";
}
else if ($seconds >= 60 && $seconds < 3600){ # hr
$time = floor($seconds / 60);
$time_val = "Minute";
}
else if ($seconds >= 3600 && $seconds < 86400){ # day
$time = floor($seconds / 3600);
$time_val = "Hour";
}
else if ($seconds >= 86400 && $seconds < 604800){ # week
$time = floor($seconds / 86400);
$time_val = "Day";
}
else if ($seconds >= 604800) {
$time = floor($seconds / 604800);
$time_val = "Week";
}
if ($time > 1) $time_val .= 's';
return "$time $time_val\n";
}
echo convert_seconds(40);
echo convert_seconds(530);
echo convert_seconds(35930);
echo convert_seconds(240000);
echo convert_seconds(2345775);
输出:
nill
8 Minutes
9 Hours
2 Days
3 Weeks
我假设您要输出整数,如果要十进制(例如8.25 Hours
),则可以将floor
更改为适当的round
,例如$time = round($seconds / 60, 2);