我试图转换格式的时间戳,2009-09-12 20:57:19
并将其转换为类似于3 minutes ago
PHP的东西.
我找到了一个有用的脚本来做到这一点,但我认为它正在寻找一种不同的格式作为时间变量.我想要修改以使用此格式的脚本是:
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?)格式?
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';
}
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'; } } }
$time_elapsed = timeAgo($time_ago); //The argument $time_ago is in timestamp (Y-m-d H:i:s)format.
//Function definition
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 "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";
}
}
}
这实际上是我发现的更好的解决方案.使用jQuery然而它完美地工作.它也会自动刷新,类似于SO和Facebook的方式,因此您无需刷新页面即可查看更新.
此插件将读取标签中的datetime
attr 并为您填写.
e.g. "4 minutes ago" or "about 1 day ago
http://timeago.yarp.com/
我不知道为什么没有人提到碳.
https://github.com/briannesbitt/Carbon
这实际上是php dateTime(这里已经使用过)的扩展,它有:diffForHumans方法.所以你需要做的就是:
$dt = Carbon::parse('2012-9-5 23:26:11.123789'); echo $dt->diffForHumans();
更多例子:http://carbon.nesbot.com/docs/#api-humandiff
这个解决方案的优点:
它适用于未来的日期,并将返回2个月等内容.
你可以使用本地化来获得其他语言和复数工作正常
如果您将开始使用Carbon,那么使用日期的其他事情将变得非常简单.
function humanTiming ($time) { $time = time() - $time; // to get the time since that moment $time = ($time<1)? 1 : $time; $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':''); } } echo humanTiming( strtotime($mytimestring) );
我发现结果如下丑:
1年,2个月,0天,0小时,53分钟和1秒
因此,我实现了一个尊重复数的函数,删除空值,并可选择缩短输出:
function since($timestamp, $level=6) { global $lang; $date = new DateTime(); $date->setTimestamp($timestamp); $date = $date->diff(new DateTime()); // build array $since = array_combine(array('year', 'month', 'day', 'hour', 'minute', 'second'), explode(',', $date->format('%y,%m,%d,%h,%i,%s'))); // remove empty date values $since = array_filter($since); // output only the first x date values $since = array_slice($since, 0, $level); // build string $last_key = key(array_slice($since, -1, 1, true)); $string = ''; foreach ($since as $key => $val) { // separator if ($string) { $string .= $key != $last_key ? ', ' : ' ' . $lang['and'] . ' '; } // set plural $key .= $val > 1 ? 's' : ''; // add date value $string .= $val . ' ' . $lang[ $key ]; } return $string; }
看起来好多了:
1年,2个月,53分钟和1秒
可选择使用$level = 2
以缩短它如下:
1年零2个月
$lang
如果您只需要英文版本,请删除该部分或编辑此翻译以满足您的需求:
$lang = array( 'second' => 'Sekunde', 'seconds' => 'Sekunden', 'minute' => 'Minute', 'minutes' => 'Minuten', 'hour' => 'Stunde', 'hours' => 'Stunden', 'day' => 'Tag', 'days' => 'Tage', 'month' => 'Monat', 'months' => 'Monate', 'year' => 'Jahr', 'years' => 'Jahre', 'and' => 'und', );
我稍微修改了原始函数(在我看来更有用,或逻辑上).
// display "X time" ago, $rcs is precision depth function time_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 = count($lngh) - 1; ($v >= 0) && (($no = $dif / $lngh[$v]) <= 1); $v--); if ($v < 0) $v = 0; $_tm = $cur_tm - ($dif % $lngh[$v]); $no = ($rcs ? floor($no) : round($no)); // if last denomination, round if ($no != 1) $pds[$v] .= 's'; $x = $no . ' ' . $pds[$v]; if (($rcs > 0) && ($v >= 1)) $x .= ' ' . $this->time_ago($_tm, $rcs - 1); return $x; }
我做了这个并且它工作得很好,它既适用于unix时间戳一样,1470919932
也适用于格式化时间16-08-11 14:53:30
function timeAgo($time_ago) { $time_ago = strtotime($time_ago) ? strtotime($time_ago) : $time_ago; $time = time() - $time_ago; switch($time): // seconds case $time <= 60; return 'lessthan a minute ago'; // minutes case $time >= 60 && $time < 3600; return (round($time/60) == 1) ? 'a minute' : round($time/60).' minutes ago'; // hours case $time >= 3600 && $time < 86400; return (round($time/3600) == 1) ? 'a hour ago' : round($time/3600).' hours ago'; // days case $time >= 86400 && $time < 604800; return (round($time/86400) == 1) ? 'a day ago' : round($time/86400).' days ago'; // weeks case $time >= 604800 && $time < 2600640; return (round($time/604800) == 1) ? 'a week ago' : round($time/604800).' weeks ago'; // months case $time >= 2600640 && $time < 31207680; return (round($time/2600640) == 1) ? 'a month ago' : round($time/2600640).' months ago'; // years case $time >= 31207680; return (round($time/31207680) == 1) ? 'a year ago' : round($time/31207680).' years ago' ; endswitch; } ?>