【问题标题】:How to convert number of seconds to plain English string with larger units as needed?如何根据需要将秒数转换为单位较大的纯英文字符串?
【发布时间】: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;
}

现场演示:https://eval.in/826278

用法

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


【解决方案1】:

从我的第一篇文章开始,我完全改变了方向。我想摆脱所有的数学处理,所以我决定使用 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

【讨论】:

  • 确实如此,非常完美。很抱歉延迟奖励,我正在玩它并陷入了一个无关紧要的小错误!
【解决方案2】:

我已经为自己创建了一个函数来执行此操作,因此我可以将它重用于更多应用程序:

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 

Quick example for your specific code.

【讨论】:

  • 我有点困惑,这是否与我所做的功能一起工作,否则我看不到您如何添加“小时”,“分钟”......另外,如果没有,它似乎在这里不起作用:eval.in/826326 -- 除非我用错了?
  • 是的,你使用它(简化)像这样:smartImplode(array('5 hours', '7mins', '8seconds'));您当前的代码将所有内容都设置在一个字符串中,这需要作为一个数组。
  • 你可以在你的函数中使用这个函数仍然返回一个字符串。
  • 我已经编辑了您的答案,使其更加深入。我还修复了 smart_implode 函数,因为它抛出了一个未定义的变量错误。
  • 我已经修复了您的错误,但坚持使用不太详细的答案。那些通常工作得更好。而且我还添加了一个快速示例的链接,不需要完全添加它
猜你喜欢
  • 2016-07-30
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多