douqihua6212 2016-02-12 20:56
浏览 25
已采纳

TimeAgo函数返回负秒

I have a function that returns how long ago the time was posted, but there seem be to a slight problem with it. The first time, $timeAgo1 works just fine, but the second time, $timeAgo2 seems to return negative seconds. How is that happening?

<?php

//*****************************************************START OF FUNCTION
function timeAgo($time_ago) {

    $time_ago = strtotime($time_ago);
    $cur_time = time();
    $time_elapsed = $cur_time - $time_ago;
    $seconds = $time_elapsed ;
    $minutes = round($time_elapsed / 60 );
    $hours = round($time_elapsed / 3600);
    $days = round($time_elapsed / 86400 );
    $weeks = round($time_elapsed / 604800);
    $months = round($time_elapsed / 2600640 );
    $years = round($time_elapsed / 31207680 );

    echo $seconds."<br>";
    if ($seconds <= 60) {
        return "just now";
    } //end of if ($seconds <= 60)

    else if ($minutes <=60) {
        if ($minutes == 1) {
            return "one minute ago";
        } //end of else if ($minutes <=60)
        else {
            return "$minutes minutes ago";
        } //end of else not ($minutes == 1)
    } //end of else if ($minutes <= 60)


    else if ($hours <= 24) {
        if ($hours == 1) {
            return "an hour ago";
        } //end of if ($hours == 1)
        else {
            return "$hours hours ago";
        } //end of else not ($hours == 1)
    } //end of else if ($hours <= 24)


    else if ($days <= 7) {
        if ($days == 1) {
            return "yesterday";
        } //end of else if ($days <= 7)
        else {
            return "$days days ago";
        } //end of else not ($days == 1)
    } //end of else if ($days <= 7)

    else if ($weeks <= 4.3) {
        if ($weeks == 1) {
            return "a week ago";
        } //end of if ($weeks == 1)
        else {
            return "$weeks weeks ago";
        } //end of else not ($weeks == 1)
    } //end of else if ($weeks <= 4.3)


    else if ($months <= 12) {
        if ($months == 1) {
            return "a month ago";
        } //end of if ($months == 1)
        else {
            return "$months months ago";
        } //end of else not ($months == 1)
    } //end of else if ($months <= 12)

    else {
        if ($years == 1) {
            return "one year ago";
        } //end of if ($years == 1)
        else {
            return "$years years ago";
        } //end of else not ($years == 1)
    } //end of last else

} //end of function timeAgo($time_ago)
//*****************************************************END OF FUNCTION

$date1 = "2016-02-10";
$time1 = "22:41:58";
$date2 = "2016-02-12";
$time2 = "15:25:57";

$timeAgo1 = timeAgo($date1.$time1);
$timeAgo2 = timeAgo($date2.$time2);

echo $timeAgo1."<br>".$timeAgo2;

?>
  • 写回答

2条回答 默认 最新

  • dongzhuo0895 2016-02-12 21:09
    关注

    Set the timezone in the beginning of the script to the same date as original dates you are checking against, example:

    date_default_timezone_set('America/Los_Angeles');

    http://php.net/manual/en/function.date-default-timezone-set.php

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

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向