将此日期转换为时间

时间:2013-09-19 09:48:47

标签: php date datetime time

寻找一个可以转换此日期的轻量级函数,因为它显示为“ Thu Sep 19 08:43:29 +0000 2013

有什么想法吗?

3 个答案:

答案 0 :(得分:7)

以前的功能

function time_ago($date) {
    if (empty($date)) {
        return "No date provided";
    }
    $periods = array("second", "minute", "hour", "day", "week", "month", "year", "decade");
    $lengths = array("60", "60", "24", "7", "4.35", "12", "10");
    $now = time();
    $unix_date = strtotime($date);
// check validity of date
    if (empty($unix_date)) {
        return "Bad date";
    }
// is it future date or past date
    if ($now > $unix_date) {
        $difference = $now - $unix_date;
        $tense = "ago";
    } else {
        $difference = $unix_date - $now;
        $tense = "from now";
    }
    for ($j = 0; $difference >= $lengths[$j] && $j < count($lengths) - 1; $j++) {
        $difference /= $lengths[$j];
    }
    $difference = round($difference);
    if ($difference != 1) {
        $periods[$j].= "s";
    }
    return "$difference $periods[$j] {$tense}";
}

要使用此功能,只需致电:

<?php echo time_ago($mydate); ?>

source

答案 1 :(得分:1)

您可以使用方便的DateTime类:

$oDate = new DateTime('Thu Sep 19 08:43:29 +0000 2013');
$oNow = new DateTime();
$oInterval = $oDate->diff($oNow);
echo $oInterval->format('%R%a days');

这将显示now和date之间的差异,以天为单位:

  

+0天

答案 2 :(得分:0)

使用示例:

echo time_elapsed_string('Thu Sep 19 08:43:29 +0000 2013');
echo time_elapsed_string('Thu Sep 19 08:43:29 +0000 2013', true);

输出:

1 hour ago
1 hour, 35 minutes, 7 seconds ago

<强> Link to the function.