php 5.3日期差异等效于php<;=5.2关于自身功能


php 5.3 date diff equivalent for PHP <= 5.2 on own function

我正在使用一个函数(在网上找到)来计算到现在为止所经过的时间。我传递两个参数:发布日期和当前日期。它将返回年、月、天、小时、分钟或秒。它使用了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

现在,我需要一个等价的函数"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) }

没有必要改变这么多事情。

我只是需要一个WordPress插件(不幸的是)。这个函数我用了两次。我在这里发布了这个答案:

  1. 在我的类中调用->diff()(我的类扩展了DateTime,所以$this是引用DateTime

    function diff ($secondDate){
        $firstDateTimeStamp = $this->format("U");
        $secondDateTimeStamp = $secondDate->format("U");
        $rv = ($secondDateTimeStamp - $firstDateTimeStamp);
        $di = new DateInterval($rv);
        return $di;
    }
    

  2. 然后我重新创建了一个假的DateInterval类(因为DateInterval只在PHP>=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;
        }
    }
    

希望这能帮助到别人。

试试我用的东西。

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是您在日期中使用的分隔符。

有一个简单的方法。你可以在那里改变一些东西,这样它就符合你的需求。

<?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);
?>