我正试图在评论框中放置时间。
尝试使用以下代码,但它给了我45年的时间。
这是我的代码
$data = $row['created_on'];
$time = strtotime('$data');
<li>
<span class="author">
<?php echo $row['name']; ?>
</span>
<span class="time">
<?php echo ''.humanTiming($time).' ago'; ?>
</span><br/>
<div class="c_content">
<?php echo $row['comment']; ?>
</div>
<div class="clear"></div>
</li>
<?php } ?>
</ul>
<?php
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':'');
}
}
答案 0 :(得分:0)
function timeAgo($time_ago)
{
$time_ago = strtotime($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 );
// Seconds
if($seconds <= 60){
return "now";
}
//Minutes
else if($minutes <=60){
if($minutes==1){
return "1mn";
}
else{
return "$minutes"."mn";
}
}
//Hours
else if($hours <=24){
if($hours==1){
return "1h";
}else{
return "$hours"."h";
}
}
//Days
else if($days <= 7){
if($days==1){
return "1j";
}else{
return "$days"."j";
}
}
//Weeks
else if($weeks <= 4.3){
if($weeks==1){
return "1sm";
}else{
return "$weeks"."sm";
}
}
//Months
else if($months <=12){
if($months==1){
return "1m";
}else{
return "$months"."m";
}
}
//Years
else{
if($years==1){
return "1a";
}else{
return "$years"."a";
}
}
}
答案 1 :(得分:0)
这是一种让人类计时的聪明方法......
注意:此解决方案并不理解任何时区声明,您需要将其添加到DateTime declarations。
$data="2015-03-13 01:28:05"; // declare your input
$then=new DateTime($data); // feed input to DateTime
$now=new DateTime(); // get DateTime for Now
$diff=(array)$then->diff($now); // calculate the difference & cast as array
$labels=array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$readable=""; // declare as empty string
// filter the $diff array to only include the desired elements and loop
foreach(array_intersect_key($diff,$labels) as $k=>$v){
if($v>0){ // only add non-zero values to $readable
$readable.=($readable!=""?", ":"")."$v {$labels[$k]}".($v>1?"s":"");
// use comma-space as glue | show value | show unit | pluralize when necessary
}
}
echo "$readable ago";
// e.g. 2 years, 9 days, 23 hours, 1 minute, 7 seconds ago
在这项工作Demo中了解自己。
如果您只是在寻找一个功能:
function humanTiming($time){
$then=new DateTime($time);
$now=new DateTime();
$diff=(array)$then->diff($now);
$labels=array("y"=>"year","m"=>"month","d"=>"day","h"=>"hour","i"=>"minute","s"=>"second");
$readable="";
foreach(array_intersect_key($diff,$labels) as $k=>$v){
if($v>0){
$readable.=($readable!=""?", ":"")."$v {$labels[$k]}".($v>1?"s":"");
}
}
return "$readable ago";
}
...
<span class="time">
<?php echo humanTiming($row['created_on']); ?>
</span><br/>
...