【发布时间】:2017-12-06 07:31:22
【问题描述】:
我的时间戳到字符串函数
// Timestamp To String
function time2string($time) {
// DAYS
$d = floor($time/86400);
if ($d > 0) { $_d = $d.($d > 1 ? ' days' : ' day'); }
else { $_d = ""; }
// HOURS
$h = floor(($time-$d*86400)/3600);
if ($h > 0) { $_h = $h.($h > 1 ? ' hours' : ' hour'); }
else { $_h = ""; }
// MINUTES
$m = floor(($time-($d*86400+$h*3600))/60);
if ($m > 0) { $_m = $m.($m > 1 ? ' minutes' : ' minute'); }
else { $_m = ""; }
// SECONDS
$s = $time-($d*86400+$h*3600+$m*60);
if ($s >0) { $_s = $s.($s > 1 ? ' seconds' : ' second'); }
else { $s = ""; }
$time_str = $_d.' '.$_h.' '.$_m.' '.$_s;
return $time_str;
}
用法
time2string(22500)
6 小时 15 分钟
期望的输出
- 1 秒
- 1 分钟和 1 秒
- 1 小时 1 分钟 和 1 秒
- 1 天 1 小时 1 分钟 和 1 秒
【问题讨论】:
-
@MagnusEriksson 我确实问过如何将 and 功能与逗号一起添加到我的脚本中,所以不,不是直接问我是否已经这样做了 最好的方式
-
@MagnusEriksson 我不确定如果没有很多 的
IF语句使这个相当小的函数变成一个很大的函数,我将如何实现这一点。你有什么建议可以让自己走下去吗?谢谢 -
这段代码似乎对我不起作用。 eval.in/826236.
-
您正在设置
$d,但正在使用$_d($m和$h也是如此)。这只是抛出“未定义的变量”。
标签: php string time units-of-measurement elapsedtime