时间戳到:天 - 小时 - 分钟

时间:2012-10-16 15:27:27

标签: php

有人可以告诉我如何在PHP中将特定时间戳转换为天 - 小时 - 分钟?

例如我想从这样的随机时间戳得到这样的东西:( 20天13小时35分钟):534222234

谢谢。

5 个答案:

答案 0 :(得分:9)

function time2string($time) {
    $d = floor($time/86400);
    $_d = ($d < 10 ? '0' : '').$d;

    $h = floor(($time-$d*86400)/3600);
    $_h = ($h < 10 ? '0' : '').$h;

    $m = floor(($time-($d*86400+$h*3600))/60);
    $_m = ($m < 10 ? '0' : '').$m;

    $s = $time-($d*86400+$h*3600+$m*60);
    $_s = ($s < 10 ? '0' : '').$s;

    $time_str = $_d.':'.$_h.':'.$_m.':'.$_s;

    return $time_str;
}

使用示例:

echo time2string(86500); // 01:00:01:40  dd:hh:mm:ss

答案 1 :(得分:1)

以下是我正在做的最小秒数的示例

function convertToMinSec($time) {
    $min = intval(($time / 60) % 60);
    $minSec = $min.'m ';

    $sec = intval($time % 60);
    $sec = str_pad($sec, 2, "0", STR_PAD_LEFT);
    $minSec .= $sec.'s';

    return $minSec;
}

这是做几分钟它得到的时间(以秒为单位)除以60并得到60的mod。这将得到分钟数。

然后我基本上做同样的事情来获得秒数(mod是余数)然后如果它在10秒以内我填充左边

答案 2 :(得分:1)

您可以执行以下操作

$now = date_create("now");
$other_date = date_create("2012/05/10");
$interval = date_diff($now, $other_date);
$interval->format("d h i");

答案 3 :(得分:0)

您可以使用日期函数转换时间戳。

http://de2.php.net/manual/en/function.date.php

答案 4 :(得分:0)

我的代码支持数年和数周

function array2readable($array, $separator, $last_separator) {                              
    $result = preg_replace(strrev("/$separator/"),strrev($last_separator),strrev(implode($separator, $array)), 1);
    return strrev($result);
}

function unixTime2text($time) {
    $value['y'] = floor($time/31536000);
    $value['w'] = floor(($time-($value['y']*31536000))/604800);
    $value['d'] = floor(($time-($value['y']*31536000+$value['w']*604800))/86400);
    $value['h'] = floor(($time-($value['y']*31536000+$value['w']*604800+$value['d']*86400))/3600);
    $value['m'] = floor(($time-($value['y']*31536000+$value['w']*604800+$value['d']*86400+$value['h']*3600))/60);
    $value['s'] = $time-($value['y']*31536000+$value['w']*604800+$value['d']*86400+$value['h']*3600+$value['m']*60);

    $unit['y'] = 'year' . ($value['y'] > 1 ? 's' : '');
    $unit['w'] = 'week' . ($value['w'] > 1 ? 's' : '');
    $unit['d'] = 'day' . ($value['d'] > 1 ? 's' : '');
    $unit['h'] = 'hour' . ($value['h'] > 1 ? 's' : '');
    $unit['m'] = 'minute' . ($value['m'] > 1 ? 's' : '');
    $unit['s'] = 'second' . ($value['s'] > 1 ? 's' : '');

    foreach($value as $key => $val) {
        if (!empty($val)) {
            $not_null_values[] .= $val . ' ' . $unit[$key];
        }
    }

    return array2readable($not_null_values, ', ', ' and ');
}

示例1

echo unixTime2text(1776900); # 20 days 13 hours 35 minutes

示例2

echo unixTime2text(534222234); # 16 years, 49 weeks, 3 hours, 3 minutes and 54 seconds