-1

我已经搜索过,我有太多的试验/错误,但没有运气。所以这里是:

我想找出自插入的 Mysql 条目NOW()的日期和格式为 . 的当前日期以来已经过去了多少秒。这date("Y.m.d H:i:s")是我的 php 函数(取自用户发布的@stackoverflow)。

function multipost(){
global $dbh;
$ip= $_SERVER['REMOTE_ADDR']; 

$sql = "select * from  table where ip='$ipa' order by date_time desc limit 1";

foreach ($dbh->query($sql) as $row) {
    $timeFirst=$row['date_time'];
    $timeSecond=date("Y.m.d H:i:s");
    $differenceInSeconds = $timeSecond-$timeFirst;
    echo $differenceInSeconds;
}

我也尝试过strtotime($row['date_time'];),但没有运气。有什么帮助吗?提前致谢。

4

3 回答 3

1
function secDifference($date,$time){
    $dateVals = explode("-",$date);
    $timeUnix = 0;
    if($time == 0){
        $timeUnix = date("U",mktime(0,0,0,$dateVals[1],$dateVals[2],$dateVals[0]));
    }
    else{
        $tempVals = explode(" ",$dateVals[2]);
        $dateVals[2] = $tempVals[0];
        $timeVals = explode(":",$tempVals[1]);
        $timeUnix = date("U",mktime($timeVals[0],$timeVals[1],$timeVals[2],$dateVals[1],$dateVals[2],$dateVals[0]));
    }
    return (date("U")-$timeUnix);
}

复制此功能。要获得 DATE 和 NOW 的差异:

secDifference("12/03/1993");

要获得日期时间和现在的差异:

secDifference("12/03/1993 14:54:32",1);

希望能帮助到你。

于 2012-11-02T14:58:13.807 回答
1

您可以让 mysql 为您返回值:

select *, TIMESTAMPDIFF(SECOND, NOW(), date_time) AS time_diff from  table where ip='$ipa' order by date_time desc limit 1
于 2012-11-02T15:05:18.267 回答
0

如果您只想知道以秒为单位的时间差异,您可以使用 MySQL TIMESTAMPDIFF()。您的查询字符串如下所示:

$sql = "SELECT *, TIMESTAMPDIFF(SECOND, date_time, NOW()) AS time_diff FROM table WHERE ip='$ipa' ORDER BY date_time DESC LIMIT 1";

请注意,返回的TIMESTAMPDIFF存储为“time_diff”。您的结果将添加“time_diff”作为列。

如需更多文档,请参阅此页面

于 2012-11-02T15:02:12.717 回答