简体   繁体   English

mysql datetime与strtotime()不兼容吗?

[英]Is mysql datetime not compatible with strtotime()?

I'm passing this function a mysql datetime and it is returning nothing. 我正在将此函数传递给mysql datetime,它什么也不返回。 Is mysql datetime not compatible with strtotime()? mysql datetime与strtotime()不兼容吗? I assume that the foreach loop is ending and the return variable is not being called, but why? 我假设foreach循环结束并且未调用return变量,但是为什么呢?

function timeAgo($time) {
    $time = time() - strtotime($time); // to get the time since that moment

    $tokens = array (
        1 => 'second',
        60 => 'minute',
        3600 => 'hour',
        86400 => 'day',
        604800 => 'week',
        2592000 => 'month',
        31536000 => 'year'
    );

    foreach ($tokens as $unit => $text) {
        if ($time < $unit) continue;
        $numberOfUnits = floor($time / $unit);
        return $numberOfUnits.' '.$text.(($numberOfUnits>1)?'s':'').' ago';
    }
}

Update This is the code that gets it from the database, there are no quotes in the datetime string. 更新这是从数据库获取代码的代码,datetime字符串中没有引号。

while ($row = mysqli_fetch_assoc($result)) { 
   $commentTime = $row["time"]; 
   $commentComment = $row["comment"]; 
   $commentCommenter = $row["commenter"]; 
} 

And this is the code to echo it: echo '<h3 class="party-time">'.timeAgo($commentTime).'</h3>'; 这是回显它的代码: echo '<h3 class="party-time">'.timeAgo($commentTime).'</h3>';

The problem was a timezone issue. 问题是时区问题。 I was manually setting the date in the SQL server I was using which set the datetime to my local time, but the server's timezone was -3 hours, causing issues. 我在正在使用的SQL Server中手动设置日期,该日期将datetime设置为本地时间,但是服务器的时区为-3小时,导致出现问题。

We cannot tell what $time holds exactly, but when you feed an ISO-8601 date to strtotime , it works fine: 我们无法确切地知道$time含义,但是当您将一个ISO-8601日期输入到 strtotime ,它可以正常工作:

php > echo strtotime("2013-08-01 12:00:00");
1375351200

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM