dreinuqm992401 2010-01-03 15:38
浏览 29
已采纳

php时差脚本问题

here is my php code.....

<?php
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  = strtotime($date);

       // check validity of date
    if(empty($unix_date)) {    
        return "Incorrect Date";
    }

    // is it future date or past date
    if($now > $unix_date) {    
        $difference     = $now - $unix_date;
        $tense         = "ago";

    } else {
        $difference     = $unix_date - $now;
        $tense         = "ago";
    }

    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}";
}
?>

but it is not showing the time correctly....and if any current time is given to it is showing 25-35 seconds ago and so on....but i want to make a time difference funtion like digg.com in a format like 2 Mints AND 3 Sec ago... how can i do that

  • 写回答

2条回答 默认 最新

  • duanbi7204 2010-01-03 15:48
    关注

    Try it to do with the other way - at the beginning multiply all $lengths values (i.e. $multiplied).
    1. Divide $difference with that value.
    2. If >= 1 then you have a solution.
    Else divide $multiplied with the array_pop($lengths) value. Go to 2.
    3. You have the solution.

    I think it would be it.

    BTW - $tense is always "ago".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效