我的调用表的代码以24小时格式显示日期和UTC时间。这很酷,但是有没有办法让它说出'#34;秒前","分钟前","小时前","天前& #34 ;.而不是确切的日期和时间?提前致谢!更新后的代码:仍然无法正常工作,现在这些领域还没有填充:(
<!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>';
?>
&#13;
答案 0 :(得分:1)
您可以使用momentJS并获取问题中提到的值。
moment("20111031", "YYYYMMDD").fromNow(); // 6 years ago
moment("20120620", "YYYYMMDD").fromNow(); // 6 years ago
moment().startOf('day').fromNow(); // an hour ago
moment().endOf('day').fromNow(); // in a day
moment().startOf('hour').fromNow(); // 10 Minutes ago
根据@MEE的评论和PHP的关注,我建议你参考下面的链接,
答案 1 :(得分:1)
重要,这是我用来获取时间的JS代码
<textarea id="time_id"></textarea>
<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脚本从DB
获取时间我不记得我从哪里得到这段代码,所以我不能赞美作者,但我确实在网上找到了。
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";
}
}
}
然后你会回复时间:
<?php
$curenttime = $table['date_time'];
$time_ago = strtotime($curenttime);
echo timeAgo($time_ago);
?>