在PHP中将时间戳转换为时间,例如1天前,2天前...我试图转换格式的时间戳,2009-09-12 20:57:19并将其转换为类似于3 minutes agoPHP的东西。我找到了一个有用的脚本来做到这一点,但我认为它正在寻找一种不同的格式作为时间变量。我想要修改以使用此格式的脚本是:function _ago($tm,$rcs = 0) {
$cur_tm = time();
$dif = $cur_tm-$tm;
$pds = array('second','minute','hour','day','week','month','year','decade');
$lngh = array(1,60,3600,86400,604800,2630880,31570560,315705600);
for($v = sizeof($lngh)-1; ($v >= 0)&&(($no = $dif/$lngh[$v])<=1); $v--); if($v < 0) $v = 0; $_tm = $cur_tm-($dif%$lngh[$v]);
$no = floor($no);
if($no <> 1)
$pds[$v] .='s';
$x = sprintf("%d %s ",$no,$pds[$v]);
if(($rcs == 1)&&($v >= 1)&&(($cur_tm-$_tm) > 0))
$x .= time_ago($_tm);
return $x;
}我认为在前几行中脚本试图做一些看起来像这样的事情(不同的日期格式数学):$dif = 1252809479 - 2009-09-12 20:57:19;我如何将我的时间戳转换为(unix?)格式?
4 回答
一只甜甜圈
TA贡献1836条经验 获得超5个赞
使用示例:
echo time_elapsed_string('2013-05-01 00:22:35');
echo time_elapsed_string('@1367367755'); # timestamp input
echo time_elapsed_string('2013-05-01 00:22:35', true);
输入可以是任何支持的日期和时间格式。
输出:
4 months ago 4 months, 2 weeks, 3 days, 1 hour, 49 minutes, 15 seconds ago
功能:
function time_elapsed_string($datetime, $full = false) { $now = new DateTime; $ago = new DateTime($datetime); $diff = $now->diff($ago); $diff->w = floor($diff->d / 7); $diff->d -= $diff->w * 7; $string = array( 'y' => 'year', 'm' => 'month', 'w' => 'week', 'd' => 'day', 'h' => 'hour', 'i' => 'minute', 's' => 'second', ); foreach ($string as $k => &$v) { if ($diff->$k) { $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : ''); } else { unset($string[$k]); } } if (!$full) $string = array_slice($string, 0, 1); return $string ? implode(', ', $string) . ' ago' : 'just now';}
HUX布斯
TA贡献1876条经验 获得超6个赞
function time_elapsed_string($ptime){ $etime = time() - $ptime; if ($etime < 1) { return '0 seconds'; } $a = array( 365 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second' ); $a_plural = array( 'year' => 'years', 'month' => 'months', 'day' => 'days', 'hour' => 'hours', 'minute' => 'minutes', 'second' => 'seconds' ); foreach ($a as $secs => $str) { $d = $etime / $secs; if ($d >= 1) { $r = round($d); return $r . ' ' . ($r > 1 ? $a_plural[$str] : $str) . ' ago'; } }}
Cats萌萌
TA贡献1805条经验 获得超9个赞
$time_elapsed = timeAgo($time_ago); //The argument $time_ago is in timestamp (Y-m-d H:i:s)format.//Function definitionfunction 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 "just now"; } //Minutes else if($minutes <=60){ if($minutes==1){ return "one minute ago"; } else{ return "$minutes minutes ago"; } } //Hours else if($hours <=24){ if($hours==1){ return "an hour ago"; }else{ return "$hours hrs ago"; } } //Days else if($days <= 7){ if($days==1){ return "yesterday"; }else{ return "$days days ago"; } } //Weeks else if($weeks <= 4.3){ if($weeks==1){ return "a week ago"; }else{ return "$weeks weeks ago"; } } //Months else if($months <=12){ if($months==1){ return "a month ago"; }else{ return "$months months ago"; } } //Years else{ if($years==1){ return "one year ago"; }else{ return "$years years ago"; } }}
- 4 回答
- 0 关注
- 941 浏览
添加回答
举报
0/150
提交
取消