What can use for DateTime::diff() for PHP 5.2?

2019-01-05 03:22发布

Is there any function equivalent to DateTime::diff() in PHP 5.2?

My local server is PHP 5.3 and using DateTime::diff(). then I found that my live site uses PHP 5.2 and gives an error.

Fatal error: Call to undefined method DateTime::diff() in /var/www/some/other/dir/web/daikon/modules/projects/views/admin/log/admin_log_list.php on line 40

The PHP code:

 foreach ($logs as $key => $list){
 ...
 // show date in European way dd-mm-yyyy not in MySQL way yyyy-mm-dd
    $newdate =new DateTime($list['date']) ;
    echo "<td class=\"left\" width=\"8%\">".$newdate->format('d-m-Y')."</td>\n";
    $starttime = new DateTime($list['start_time']);
    echo "<td width=\"7%\">".date_format($starttime, 'H:i')."</td>\n";
    $finishtime = new DateTime($list['finish_time']);
    echo "<td width=\"8%\">".date_format($finishtime, 'H:i')."</td>\n";
    $timediff = 0;
    $interval = $starttime->diff($finishtime);
    $hours   = $interval->format('%h');
    $minutes = $interval->format('%i');
    $timediff = $hours * 60 + $minutes;

标签: php php-5.2
10条回答
等我变得足够好
2楼-- · 2019-01-05 03:56

Spudley was close, but you need to use gmdate not date.

So this works for 24 hours or less (if it's a positive value at least):

$difference = $date2->format('U') - $date1->format('U');
$time_diff = gmdate('H:i:s',$difference);
查看更多
Juvenile、少年°
3楼-- · 2019-01-05 03:57

Yes, it's annoying that feature didn't make it into PHP5.2.

I'll assume you can't upgrade to 5.3? You should look into it; there's very little reason not to upgrade; but I'll assume you can't for whatever the reason.

First tip: If you only need a diff of less than 24hours, you can simply subtract the two time stamps, and do $time_diff = date('H:i:s',$subtracted_value);

If you're doing more than 24 hour diffs, but you're okay with just returning the number of days along with the time difference, you can expand on the above technique by doing a modulus calculation on the subtrated value, against the number of seconds in a day (ie 24*60*60, which is 86400)

$subtracted_value = $date1 - $date2;
$days_diff = $subtracted_value % 86400;
$time_diff = date('H:i:s',$subtracted_value);

If you need weeks, you can of course do $days_diff % 7.

Unfortunately, the manual technique breaks down after weeks, because months and years are variable in length (technically days are too, given daylight saving, but you can probably ignore that, especially as you're only ever going to be one hour out, at the most), but hopefully that's good enough to get you started.

查看更多
萌系小妹纸
4楼-- · 2019-01-05 04:04

Spudley's answer doesn't work for me--subtracting any DateTime from another gives 0 on my system.

I was able to get it to work by using DateTime::format with the 'U' specifier (seconds since Unix epoch):

$start = new DateTime('2010-10-12');
$end = new DateTime();
$days = round(($end->format('U') - $start->format('U')) / (60*60*24));

This works on both my dev system (5.3.4) and my deployment system (5.2.11).

查看更多
Animai°情兽
5楼-- · 2019-01-05 04:05

PHP has methods for working with Unix timestamps.

As has been noted by others, by working with seconds since the Unix date, it is easy to calculate times.

PHP's strtotime() converts a date to a timestamp:

$diff = round((strtotime($list['start']) - strtotime($list['finish'])) / 86400);

If you wish to calculate till the current time, time() provides the timestamp of "now":

$diff = round((time() - strtotime($list['date'])) / 86400);

86400 is the number of seconds in a day.
If you wish to convert to years use 31557000, which is almost exactly 365.24219 * 86400.

An added advantage here is that strtotime can take the input date in almost any human readable format, so it is very easy to work with within the code.

查看更多
你好瞎i
6楼-- · 2019-01-05 04:05

I was trying to improve Christopher Pickslay's answer.

I made this function that returns and object with most of the properties from the original DateInterval object.

There is no "days" property, because it seems to be some bug in my test server (it always return 6015) .

Also, I am assuming every month has 30 days, which is definitely not precise, but may help.

function dateTimeDiff($date1, $date2) {

    $alt_diff = new stdClass();
    $alt_diff->y =  floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24*365));
    $alt_diff->m =  floor((floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365))/30);
    $alt_diff->d =  floor(floor(abs($date1->format('U') - $date2->format('U')) / (60*60*24)) - ($alt_diff->y * 365) - ($alt_diff->m * 30));
    $alt_diff->h =  floor( floor(abs($date1->format('U') - $date2->format('U')) / (60*60)) - ($alt_diff->y * 365*24) - ($alt_diff->m * 30 * 24 )  - ($alt_diff->d * 24) );
    $alt_diff->i = floor( floor(abs($date1->format('U') - $date2->format('U')) / (60)) - ($alt_diff->y * 365*24*60) - ($alt_diff->m * 30 * 24 *60)  - ($alt_diff->d * 24 * 60) -  ($alt_diff->h * 60) );
    $alt_diff->s =  floor( floor(abs($date1->format('U') - $date2->format('U'))) - ($alt_diff->y * 365*24*60*60) - ($alt_diff->m * 30 * 24 *60*60)  - ($alt_diff->d * 24 * 60*60) -  ($alt_diff->h * 60*60) -  ($alt_diff->i * 60) );
    $alt_diff->invert =  (($date1->format('U') - $date2->format('U')) > 0)? 0 : 1 ;

    return $alt_diff;
}    
查看更多
forever°为你锁心
7楼-- · 2019-01-05 04:12

I use this, seems to work alright - obviously you can add a second parameter to make it more flexible:

function GetDateDiffFromNow($originalDate) 
{
    $unixOriginalDate = strtotime($originalDate);
    $unixNowDate = strtotime('now');
    $difference = $unixNowDate - $unixOriginalDate ;
    $days = (int)($difference / 86400);
    $hours = (int)($difference / 3600);
    $minutes = (int)($difference / 60);
    $seconds = $difference;

    // now do what you want with this now and return ...
}
查看更多
登录 后发表回答