0

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

有任何想法吗?

4

3 回答 3

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); ?>

资源

于 2013-09-19T09:50:40.827 回答
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');

这将显示现在和日期之间的差异,以天为单位:

+0 天

于 2013-09-19T09:58:50.640 回答
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

链接到函数。

于 2013-09-19T10:17:28.687 回答