开发者

php relative time problems

I have this function to work out the relative time that has elapsed since a certain date,

function nicetime($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开发者_StackOverflow中文版         = 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}";
}

I am passing the function a date as "2011-01-16 12:30", however I am getting bad date returned which means that $unix_date is empty however if I die in the function I get the $date that is passed to the function however it gets return with a ( before it, below is how I am calling the method.

echo nicetime(date('Y:m:d G:i', $a['created_at']))


echo nicetime("2011-01-16 12:30");

works fine for me. I would suggest you ensure

 $a['created_at']

is giving you what you thing it is. Maybe try

$dtDate = $a['created_at'];
$strDate = date('Y:m:d G:i', $dtDate );
echo $strDate;
echo nicetime($strDate);

Make sure $strDate is what you think it is.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜