我正在使用一个函数(在网上找到它)来计算到现在为止的时间。我传递了两个参数:发布日期和当前日期。它将返回年、月、日、小时、分钟或秒。它使用 PHP 5.3 的 date diff 函数,这在 5.2 版中不会:(
function pluralize( $zaehler, $inhalt ) {
return trim($zaehler . ( ( $zaehler == 1 ) ? ( " $inhalt" ) : ( " ${inhalt}s" ) )." ago");}function ago($datetime, $datetime_post){
$interval = date_create($datetime_post)->diff( date_create($datetime) );
if ( $interval->y >= 1 ) return pluralize( $interval->y, 'year' );
if ( $interval->m >= 1 ) return pluralize( $interval->m, 'month' );
if ( $interval->d >= 1 ) return pluralize( $interval->d, 'day' );
if ( $interval->h >= 1 ) return pluralize( $interval->h, 'hour' );
if ( $interval->i >= 1 ) return pluralize( $interval->i, 'minute' );
if ( $interval->s >= 1 ) return pluralize( $interval->s, 'second' );}
例子:
$post_date_time = "01/01/2012 11:30:22";
$current_date_time = "02/02/2012 07:35:41";
echo ago($current_date_time, $post_date_time);
将输出:
1 month
现在我需要一个等效的函数“ago”来做同样的事情,具体取决于 $interval 对象。
太感谢了
附加信息:所提供的解决方案都没有真正做到我想要的。我必须改进我的解释,对不起。最后,我只需要 $interval 对象就可以了:
object(DateInterval)#3 (8) { ["y"]=> int(0) ["m"]=> int(1) ["d"]=> int(0) ["h"]=> int(20) ["i"]=> int(5) ["s"]=> int(19) ["invert"]=> int(0) ["days"]=> int(6015) }
没必要改变这么多东西。