我正在尝试创建时间戳来检查帖子发布的“前”时间,例如Stack overflow,例如
刚刚
30秒前
1分钟前
1小时前
等。
1小时后,它会显示正常的时间戳,例如:date("y/m/d - h/i");
。
我的问题是如何为发布的帖子计算时间? (假设帖子是隐藏输入类型的一部分)
隐藏输入:
<input type="hidden" name="date" value="<?php echo date("l M Y - h-i") ?>"/>
编辑:
我的完整代码:
<?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>
';
}
}
?>
前哨:
它只前哨1档,但“前”时间有效..
答案 0 :(得分:1)
本网站上已经有一些很棒的功能可以提供帮助:
答案 1 :(得分:0)
使用此功能:
function humanTiming ($time) {
$time = time() - $time;
if ($time > 3600) {
return date("y/m/d h/i", $time);
}
else {
$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':'');
}
}
}
正如您从$tokens
数组中看到的那样,一小时为3600
,因此,如果time() - $time
超过该值,那么您只需要返回日期。