php - 不正确的 PHP 时间 - 45 年前的 echo

标签 php mysql sql date mysqli

我有一个 SQL 查询和 PHP 代码,它显示了一个包含时间(从数据库中获取时间戳)的帖子提要,并且 PHP 代码将该时间转换为帖子发布前的持续时间。

但它呼应“45 年前”(我知道 Unix Time 开始于 45 年前 - 1970)

示例 - 现在时间是 2015 年 8 月 28 日 - 晚上 9:24(美国东部时间),我在晚上 9:59(美国东部时间)看到该帖子,它应该说“35 分钟前”

这是代码

<?php
$servername = "localhost";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("<center><img src='http://s1.postimg.org/gflrr630f/unnamed.png' width='200' height='200'><br><br><font face='helveticaneue-medium' size='5' color='white'>There seems to be an error establishing a connection to the CDN.<br><br>Please contact an administrator <b>immediately</b>!</font></center>");
} 

$sql = "SELECT * FROM posts WHERE bp='0' ORDER BY id DESC LIMIT 50";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
$post_time = $row["time"];
$time = strtotime('$post_time');
        echo "<div class='entire' style='margin: auto;width: 98%;background-color: white;padding-top: 7px;box-shadow: 0px 2px 5px 0px rgba(0,0,0,0.50);border-radius:5px;'> <span style='margin-left:10px;'><div style='margin-left:10px;'><img src='" . $row["pro_pic"]. "' class='circular' style='float:left;vertical-align:middle; width: 50px;height: 50px;border-radius: 150px;-webkit-border-radius: 150px;-moz-border-radius: 150px;background-color: #BDBDBD;'></div></span><span class='txt' style='float: left;margin: 5px 0 5px 10px;'><span class='user-info' style='line-height: 35px;font-family: helveticaneue-medium;font-size: 20px;color: #4671A5;'> " . $row["username"]. "</span><br><span class='user-time' style=' line-height: 10px;font-family: helveticaneue-light;font-size: 15px;color:#585858;'>".humanTiming($time)." ago   </span> </span></span></span> <br><img src='" . $row["content_url"]. "' width='100%' class='post_img'><br><font style='font-family: helveticaneue-light;font-size:  22px;color:#585858;float:left;padding-left:10px;'>" . $row["post_title"]. "</font><br><center><hr width='90%' style='border-width:1px;background-color:#D8D8D8;border-color:#D8D8D8;border-style:solid;'></center><span style='padding-left:15px;'><iframe src='http://bithumor.co/i/like?id=" . $row["id"]. "&user_id=". $_SESSION["id"] ."' width='15%' style='height: 85px;border-style:none;'></iframe><iframe src='http://bithumor.co/i/bitpicks/comment.php?id=" . $row["id"]. "' width='15%' style='height: 85px;border-style:none;'></iframe> <br><iframe src='http://bithumor.co/server/post_comments/index1.php?id=" . $row["id"]. "' width='100%' height='auto' style='height:85px;border-style:none;'></iframe> </div><br>";
    }
} else {
    echo "0 results";
}
$conn->close();
?>
<br>
<?php


function humanTiming ($time) 
{
date_default_timezone_set('America/New_York');

    $time = time() - $time; // to get the time since that moment

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

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

    }

}
?>

最佳答案

PHP 变量插入单引号中。所以:

$time = strtotime('$post_time');

表示 '$post_time' 是一个文字字符串。只需删除引号,假设 $post_time 包含有效的日期格式,这将起作用。

关于php - 不正确的 PHP 时间 - 45 年前的 echo ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32281825/

相关文章:

sql - 如何使用 sysdate 更新日期列,包括动态 sql 中的时间戳

php - 使用 Google Map API 和 PHP 进行反向地理编码以使用 Lat、Long 坐标获取最近的位置

Mysql IN(VARCHAR) 与 IN(INT)

php - 带有内部选择查询的 MySQL 搜索

sql - 如何自动将 1 年日期添加到 SQL Server 中的现有日期

MYSQL 根据所​​选数据库插入 ID?

php - 我如何通过我的应用程序检查谁在线?

php - 使用 PHP 复制文件夹

php - Laravel Input::file 返回空数组,而 $_FILES 有数据

php - 尖叫 : Error suppresion ignored for Parse error: syntax error PHP