如何根据需要将秒数转换为具有更大单位的纯英文字符串?
How to convert number of seconds to plain English string with larger units as needed?
我的时间戳转字符串函数
// 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 hours 15 minutes
期望的输出
- 1秒
- 1分钟和1秒
- 1 小时 1 分钟和1 秒
- 1天1小时1分钟和1秒
我已经为自己创建了一个函数来执行此操作一次,因此我可以在更多应用程序中重复使用它:
function smart_implode($values, $join, $joinLast) {
$lastValue = end($values); // take last value
unset($arr[count($values)-1]); // remove from array
$values = implode($join, $values); // implode all remaining values
return $values.$joinLast.$lastValue; // and add the last value
}
echo smartImplode($timeValueArray, ", ", " and ");
这有额外的好处,如果您不想显示 0 值(例如 0 分钟 ),您就不必使用硬编码解决方案。只是不要在 smart_implode()
中输入它
5 hours, 0 mins and 7 seconds -> 5 hours and 7 seconds
我完全改变了第一次 post 的方式。我想摆脱所有的数学处理,所以我决定使用 DateTime 对象......毕竟 diff()
非常适合这项任务。
代码:(Demo)
function time2string($time) {
if($time==0){return '0 seconds';}
$t1=new DateTime();
$t2=new DateTime("+$time seconds");
$diff=$t1->diff($t2);
$units=['days'=>'day','h'=>'hour','i'=>'minute','s'=>'second']; // nominate units
foreach($units as $k=>$v){
if($diff->$k!=0){$result[]=$diff->$k.' '.$v.($diff->$k>1?'s':'');} // store non-zero strings
}
if(sizeof($result)==1){
return $result[0]; // return single element
}else{
$last=array_splice($result,-1); // remove last element from $a and store separately
return implode(', ',$result)." and {$last[0]}"; // return with correct delimiters
}
}
echo time2string(122510); // output: 1 day, 10 hours, 1 minute and 50 seconds
echo time2string(0); // output: 0 seconds
echo time2string(1); // output: 1 second
echo time2string(9199800); // output: 106 days, 11 hours and 30 minutes
echo time2string(69206400); // output: 801 days
echo time2string(3599); // output: 59 minutes and 59 seconds
我的时间戳转字符串函数
// 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 hours 15 minutes
期望的输出
- 1秒
- 1分钟和1秒
- 1 小时 1 分钟和1 秒
- 1天1小时1分钟和1秒
我已经为自己创建了一个函数来执行此操作一次,因此我可以在更多应用程序中重复使用它:
function smart_implode($values, $join, $joinLast) {
$lastValue = end($values); // take last value
unset($arr[count($values)-1]); // remove from array
$values = implode($join, $values); // implode all remaining values
return $values.$joinLast.$lastValue; // and add the last value
}
echo smartImplode($timeValueArray, ", ", " and ");
这有额外的好处,如果您不想显示 0 值(例如 0 分钟 ),您就不必使用硬编码解决方案。只是不要在 smart_implode()
中输入它5 hours, 0 mins and 7 seconds -> 5 hours and 7 seconds
我完全改变了第一次 post 的方式。我想摆脱所有的数学处理,所以我决定使用 DateTime 对象......毕竟 diff()
非常适合这项任务。
代码:(Demo)
function time2string($time) {
if($time==0){return '0 seconds';}
$t1=new DateTime();
$t2=new DateTime("+$time seconds");
$diff=$t1->diff($t2);
$units=['days'=>'day','h'=>'hour','i'=>'minute','s'=>'second']; // nominate units
foreach($units as $k=>$v){
if($diff->$k!=0){$result[]=$diff->$k.' '.$v.($diff->$k>1?'s':'');} // store non-zero strings
}
if(sizeof($result)==1){
return $result[0]; // return single element
}else{
$last=array_splice($result,-1); // remove last element from $a and store separately
return implode(', ',$result)." and {$last[0]}"; // return with correct delimiters
}
}
echo time2string(122510); // output: 1 day, 10 hours, 1 minute and 50 seconds
echo time2string(0); // output: 0 seconds
echo time2string(1); // output: 1 second
echo time2string(9199800); // output: 106 days, 11 hours and 30 minutes
echo time2string(69206400); // output: 801 days
echo time2string(3599); // output: 59 minutes and 59 seconds