gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 10:45:33 24 4
gpt4 key购买 nike

我有一个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com