我正在使用一个函数(在网上找到它)来计算到目前为止已经过去的时间。我传递了两个参数:发布日期和当前日期。它将返回年,月,日,小时,分钟或秒。它使用PHP 5.3的日期差异功能,在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
现在我需要一个等效的“之前”功能,它会做同样的功能,具体取决于$ 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) }
不需要改变这么多东西。
答案 0 :(得分:1)
我只需要(不幸的是)WordPress插件。这个我用了2次这个功能。我发布了这个答案here:
在我的课堂上调用 - &gt; diff()(我的课程扩展 DateTime ,所以 $ this < / strong>是参考 DateTime )
function diff ($secondDate){
$firstDateTimeStamp = $this->format("U");
$secondDateTimeStamp = $secondDate->format("U");
$rv = ($secondDateTimeStamp - $firstDateTimeStamp);
$di = new DateInterval($rv);
return $di;
}
然后我重新创建了一个假的DateInterval类(因为DateInterval仅在PHP&gt; = 5.3中有效),如下所示:
Class DateInterval {
/* Properties */
public $y = 0;
public $m = 0;
public $d = 0;
public $h = 0;
public $i = 0;
public $s = 0;
/* Methods */
public function __construct ( $time_to_convert /** in seconds */) {
$FULL_YEAR = 60*60*24*365.25;
$FULL_MONTH = 60*60*24*(365.25/12);
$FULL_DAY = 60*60*24;
$FULL_HOUR = 60*60;
$FULL_MINUTE = 60;
$FULL_SECOND = 1;
// $time_to_convert = 176559;
$seconds = 0;
$minutes = 0;
$hours = 0;
$days = 0;
$months = 0;
$years = 0;
while($time_to_convert >= $FULL_YEAR) {
$years ++;
$time_to_convert = $time_to_convert - $FULL_YEAR;
}
while($time_to_convert >= $FULL_MONTH) {
$months ++;
$time_to_convert = $time_to_convert - $FULL_MONTH;
}
while($time_to_convert >= $FULL_DAY) {
$days ++;
$time_to_convert = $time_to_convert - $FULL_DAY;
}
while($time_to_convert >= $FULL_HOUR) {
$hours++;
$time_to_convert = $time_to_convert - $FULL_HOUR;
}
while($time_to_convert >= $FULL_MINUTE) {
$minutes++;
$time_to_convert = $time_to_convert - $FULL_MINUTE;
}
$seconds = $time_to_convert; // remaining seconds
$this->y = $years;
$this->m = $months;
$this->d = $days;
$this->h = $hours;
$this->i = $minutes;
$this->s = $seconds;
}
}
希望能帮助某人。
答案 1 :(得分:0)
尝试即时使用。
function dateDiff($dformat, $endDate, $beginDate)
{
$date_parts1=explode($dformat, $beginDate);
$date_parts2=explode($dformat, $endDate);
$start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
$end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
return $end_date - $start_date;
}
$ dformat是您在日期中使用的分隔符。
答案 2 :(得分:0)
有一种简单的方法。你可以在那里改变一些东西,以满足你的需求。
<?php //preparing values
date_default_timezone_set('Europe/Berlin');
$startDate = '2011-01-21 09:00:00';
$endDate = date('Y-m-d H:i:s');
// time span seconds
$sec = explode(':', (gmdate('Y:m:d:H:i:s', strtotime($endDate) - strtotime($startDate))));
// getting all the data into array
$data = array();
list($data['years'], $data['months'], $data['days'], $data['hours'], $data['minutes'], $data['seconds']) = $sec;
$data['years'] -= 1970;
var_dump($data);
?>