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条)

报告相同问题?

悬赏问题

  • ¥66 比特币地址如何生成taproot地址
  • ¥20 数学建模数学建模需要
  • ¥15 关于#lua#的问题,请各位专家解答!
  • ¥15 什么设备可以研究OFDM的60GHz毫米波信道模型
  • ¥15 不知道是该怎么引用多个函数片段
  • ¥30 关于用python写支付宝扫码付异步通知收不到的问题
  • ¥50 vue组件中无法正确接收并处理axios请求
  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决