我有一个SQL查询和PHP代码,显示包含时间(来自数据库的TIMESTAMP' d)的帖子提要,PHP代码将该时间转换为帖子发布的持续时间。
但它已经回响了45年前#34; (我知道Unix时间是在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':'');
}
}
?>
答案 0 :(得分:4)
PHP变量不以单引号进行插值。所以:
$time = strtotime('$post_time');
表示'$post_time'
是一个文字字符串。只需删除引号,并假设$post_time
包含有效的日期格式,这将有效。