dsiuy42084 2012-02-23 13:31
浏览 126
已采纳

在PHP中将秒转换为小时/分钟/秒[重复]

Possible Duplicate:
Converting Seconds to HH:MM:SS

I have an integer as a number of seconds. I want to convert that integer into into hours/minutes/seconds like this:

1:45:32

If the number of seconds equates to less than one hour then it should return the string:

45:32

If the number of minutes is less than 10 it should return the string formatted like this:

3:25

And finally if the number of seconds equate to less than 1 minute, it should return the following:

0:04

What is the best way to do this in PHP?

  • 写回答

5条回答 默认 最新

  • duanna1407 2012-02-23 13:40
    关注

    The simpelst approch would be

    if($seconds < 3600){
     $format = 'i:s';
    }else{
     $format = 'G:i:s';
    }
    
    echo date($format, $seconds);
    

    EDIT: this wouldnt fix your minutes < 10 problem. you could handle the minutes by itself. like

    $time = ($seconds >= 3600) ? date('G', $seconds).':' : '';
    $time .= intval(date('i',$seconds)).':'.date('s', $seconds);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类