在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 關注
- 977 瀏覽
添加回答
舉報
0/150
提交
取消