I'm trying create timestamp to check the "ago" time the post was posted like Stack overflow has, e.g.
Just Now
30 Seconds ago
1 Minute ago
1 Hour ago
etc..
after 1 hour ago it will show the normal timestamp such as: date("y/m/d - h/i");
.
My question is how can I do timestamp that counts time befor the post posted? (assuming the post is part of hidden input type)
Hidden Input:
<input type="hidden" name="date" value="<?php echo date("l M Y - h-i") ?>"/>
Edit:
My full code:
<?php
$listedTypes["doc"] = 1;
$listedTypes["xlsx"] = 2;
$listedTypes["txt"] = 4;
$listedTypes["pdf"] = 8;
$listedTypes["upload"] = 16;
$listedTypes["all"] = $listedTypes["pdf"] + $listedTypes["txt"] + $listedTypes["doc"] + $listedTypes["xlsx"] ;
if(!isset($_GET['doctype'])) $_GET['doctype'] = "all";
if(!isset($listedTypes[$_GET['doctype']])) $_GET['doctype'] = "all";
$requestedType = $listedTypes[$_GET['doctype']];
//Pages
$perpage = 10; // Avoid magic numbers
$files = glob('docs/*.xml');
$file_count = count($files);
$pages = ceil($file_count/$perpage);
$page = $_GET["page"];
$files = array_slice($files, ($page-1)*$perpage, $perpage);
if ((int) $page <= 0) { $page = 1; }
//Page - END
foreach ($files as $file){
$xml = new SimpleXMLElement($file, 0, true);
//Timestamp
$time = strtotime($xml->date);
function humanTiming ($time)
{
$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':'');
}
}
//Timestamp - END
if($xml->doctype == "Microsoft Office Word") $fileType = $listedTypes["doc"];
elseif($xml->doctype == "Microsoft Office Excel") $fileType = $listedTypes["xlsx"];
elseif($xml->doctype == "Text File") $fileType = $listedTypes["txt"];
elseif($xml->doctype == "Adobe PDF File") $fileType = $listedTypes["pdf"];
elseif($xml->doctype == "upload") $fileType = $listedTypes["upload"];
elseif($xml->doctype == "Adobe PDF File" || "Text File" || "Microsoft Office Word" || "Microsoft Office Excel") $fileType = $listedTypes["all"];
if($fileType & $requestedType){
echo'
<tr>
<td>' . $xml->doctype . '</td>
<td><a href="viewdoc.php?docname=' . basename($file, '.xml') . '&username='. $xml->startedby .'&myname='. $_SESSION['username'] .'">' . basename($file, '.xml') . '</a></td>
<td><a href="viewprofile.php?name='. $xml->startedby .'">'. $xml->startedby .'</a></td>
<td>'.humanTiming($time).' ago</td>
<td>* * * * *</td>
<td>'. filesize( $file ) .'kb</td>
</tr>
';
}
}
?>
The outpost:
It outpost only 1 file, but "ago" time works..