My code for recalling my table displays date and UTC time in 24 hour format. This is cool, however is there a way to get it to say "seconds ago", "minutes ago", "hours ago", "days ago". Rather than the exact date and time? Thanks in advance! UPDATED CODE: Still doesn't work, now fields don't populate either :(
<!DOCTYPE html>
<html>
<head>
<link href='style.css?lol=<?php echo time(); ?>' rel='stylesheet'
type='text/css'>
</head>
<body>
<script type='text/javascript'>
setInterval(function() {
var date = new Date();
var fy = date.getUTCFullYear();
var tm = date.getUTCMonth() + 1;
var td = date.getUTCDate();
var h = date.getUTCHours();
var m = date.getUTCMinutes();
var s = date.getSeconds();
tm = checkTime(tm);
td = checkTime(td);
m = checkTime(m);
s = checkTime(s);
$('#time_id').html(fy + "-"+ tm + "-" + td + " " + h + ":" + m + ":" + s );
}, 500);
function checkTime(i) {
if (i < 10) {i = "0" + i}; // add zero in front of numbers < 10
return i;
}
</script>
<?php
$curenttime = $table['posted_time'];
$time_ago = strtotime($curenttime);
function timeAgo($time_ago) {
$cur_time = time();
$time_elapsed = $cur_time - $time_ago;
$seconds = $time_elapsed ;
$minutes = round($time_elapsed / 60 );
$hours = round($time_elapsed / 3600);
$days = round($time_elapsed / 86400 );
$weeks = round($time_elapsed / 604800);
$months = round($time_elapsed / 2600640 );
$years = round($time_elapsed / 31207680 );
if ($seconds <= 60) {
echo "just now";
} else if ($minutes <= 60) {
if ($minutes == 1) {
echo "1 minute ago";
} else {
echo "$minutes"." minutes ago";
}
} else if ($hours <= 24) {
if ($hours == 1) {
echo "1 hour ago";
} else {
echo "$hours"." hours ago";
}
} else if ($days <= 7) {
if ($days == 1) {
echo "yesterday";
} else {
echo "$days"." days ago";
}
} else if ($weeks <= 4.3) {
if ($weeks == 1) {
echo "1 week ago";
} else {
echo "$weeks"." weeks ago";
}
} else if ($months <= 12) {
if ($months == 1) {
echo "1 month ago";
} else {
echo "$months"." months ago";
}
} else {
if ($years == 1) {
echo "1 year ago";
} else {
echo "$years"." years ago";
}
}
}
include_once("dbConnect.php");
if (isset($_GET["frompost"])) {
echo "<h3>Thank you for your scrim post! It's been tweeted by <a
href='http://twitter.com/scrimfinder'>ScrimFinder</a>.";
echo "<br>";
} else {
}
echo '<table cellspacing="0" cellpadding="10" width="1"><tr class="top">
<th>System</th>
<th>Region</th>
<th>Game</th>
<th>Match Type</th>
<th>Time Posted (UTC)</th>
<th>Individual or Team?</th>
<th>Gamertag</th>
<th>Twitter</th>
</tr>';
$sql = "SELECT * FROM scrims ORDER BY id DESC LIMIT 15;";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows($result) > 0):
while ($row = mysqli_fetch_array($result) ) {
echo '
<tr><td><center>'.$row["system"].'</center></td><td class="grey">
<center>'.$row["region"].'</center></td> <td>
<center>'.$row["game"].'</center></td> <td class="grey">
<center>'.$row["matchtype"].'</center></td> <td>
<center>'.$row["timeAgo($time_ago)"].'</center></td> <td class="grey">
<center>'.$row["Ind_Team"].'</center></td> <td class="grey">
<center>'.$row["gamertag"].'</center></td> <td><center><a
href="http://www.twitter.com/'.$row['twitter'].'" target="_blank"><input
type="submit" value="Message '.$row['twitter'].'" class="button" style="white-space:normal;"></a>
</center></td> </tr> '; }
else:
echo "<tr><td colspan = '100'>NO RECORDS FOUND</td></tr>";
endif;
echo '</table>';
?>