0

I have an SQL query and PHP code that shows a post feed which includes the time (TIMESTAMP'd from the database) and the PHP code converts that time to duration of how long ago the post was posted.

But it's echoing "45 Years Ago" (I am aware that Unix Time began 45 years ago - 1970)

Example - Time right now is August 28th, 2015 - 9:24pm (EST) and I see that post at 9:59pm (EST), it should say "35 minutes ago"

Here's the code

<?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':'');

    }

}
?>
John Conde
  • 217,595
  • 99
  • 455
  • 496
PHP Web Dev 101
  • 535
  • 2
  • 9
  • 21

1 Answers1

4

PHP variables are not interpolated in single quotes. So:

$time = strtotime('$post_time');

means '$post_time' is a literal string. Just remove the quotes and, assuming $post_time contains a valid date format, this will work.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John Conde
  • 217,595
  • 99
  • 455
  • 496