在php中使用sql数据库从当前时间获得时差

时间:2013-06-18 17:48:57

标签: php sql

我目前正在制作发帖评论系统。

当用户对帖子发表评论时,我想将日期和时间存储在数据库中。现有注释应显示自添加注释后经过多长时间的时间差。

即: 2分钟前,3天前等等 - 与Facebook上的情况类似。

SQL或PHP中是否有任何方法或属性来处理这个问题?

哪种SQL数据类型适合此目的?日期时间或时间戳?

2 个答案:

答案 0 :(得分:1)

MySQL中的时间戳通常用于跟踪记录的更改,并且每次更改记录时都会更新。如果要存储特定值,则应使用datetime字段。 如果您想要在使用UNIX时间戳或本机MySQL日期时间字段之间做出决定,请使用本机格式。您可以在MySQL中以这种方式进行计算(" SELECT DATE_ADD(my_datetime,INTERVAL 1 DAY)")并且可以很容易地将值的格式更改为UNIX时间戳(" SELECT UNIX_TIMESTAMP( my_datetime)")当你查询记录时,如果你想用PHP操作它。 一个重要的区别是DATETIME表示日期(在日历中找到)和时间(可以在挂钟上观察到),而TIMESTAMP表示明确定义的时间点。如果您的应用程序处理时区,这可能非常重要。多久以前' 2010-09-01 16:31:00'?这取决于你所处的时区。对我而言,就在几秒钟前,对你而言,这可能代表着未来的一段时间。如果我说' 1970-01-01 00:00:00 UTC'之后的1283351460秒,你就知道我到底在什么时间点。

差异使用:

$timeFirst  = strtotime($postDateTime);
$timeSecond = strtotime($commentDateTime);
$differenceInSeconds = $timeSecond - $timeFirst;

要显示时间,请使用:

function relativedate($secs) {
        $second = 1;
        $minute = 60;
        $hour = 60*60;
        $day = 60*60*24;
        $week = 60*60*24*7;
        $month = 60*60*24*7*30;
        $year = 60*60*24*7*30*365;

        if ($secs <= 0) { $output = "now";
        }elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
        }elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
        }elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
        }elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
        }elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
        }elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
        }elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
        }else{ $output = " more than a decade ago"; }

        if ($output <> "now"){
            $output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
        }
        return $output;
    }



echo relativedate(60); // 1 minute

它将完全像在脸上显示。你必须在几秒钟内将注释和帖子之间的差异传递给函数。

答案 1 :(得分:0)

我只想改善Jun的答案。 在我尝试在我的代码上实现这一点之后,Jun对他的代码进行了一些不正确的计算,在月份和年度计算中

这是我的代码

function relativedate($secs) {
    $second = 1;
    $minute = 60;
    $hour = 60*60;
    $day = 60*60*24;
    $week = 60*60*24*7;
    $month = 60*60*24*7*4;         // because 1 month is 4 week
    $year = 60*60*24*7*4*12;       // because 1 year is 12 month

    if ($secs <= 0) { $output = "now";
    }elseif ($secs > $second && $secs < $minute) { $output = round($secs/$second)." second";
    }elseif ($secs >= $minute && $secs < $hour) { $output = round($secs/$minute)." minute";
    }elseif ($secs >= $hour && $secs < $day) { $output = round($secs/$hour)." hour";
    }elseif ($secs >= $day && $secs < $week) { $output = round($secs/$day)." day";
    }elseif ($secs >= $week && $secs < $month) { $output = round($secs/$week)." week";
    }elseif ($secs >= $month && $secs < $year) { $output = round($secs/$month)." month";
    }elseif ($secs >= $year && $secs < $year*10) { $output = round($secs/$year)." year";
    }else{ $output = " more than a decade ago"; }

    if ($output <> "now"){
        $output = (substr($output,0,2)<>"1 ") ? $output."s" : $output;
    }
    return $output;
}

echo relativedate(60); // 1 minute