Changeset 25114 for trunk


Ignore:
Timestamp:
Oct 24, 2013, 11:07:17 AM (10 years ago)
Author:
mistic100
Message:

bug:2947 fix compatibility with PHP 5.2 and 5.3

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions.inc.php

    r25113 r25114  
    459459
    460460/**
     461 * Computes the difference between two dates
     462 * returns a DateInterval object or a stdClass with the same attributes
     463 * http://stephenharris.info/date-intervals-in-php-5-2
     464 *
     465 * @param DateTime $date1
     466 * @param DateTime $date2
     467 * @return DateInterval
     468 */
     469function dateDiff($date1, $date2)
     470{
     471  if (version_compare(PHP_VERSION, '5.3.0') >= 0)
     472  {
     473    return $date1->diff($date2);
     474  }
     475 
     476  $diff = new stdClass();
     477 
     478  //Make sure $date1 is ealier
     479  $diff->invert = $date2 < $date1;
     480  if ($diff->invert)
     481  {
     482    list($date1, $date2) = array($date2, $date1);
     483  }
     484 
     485  //Calculate R values
     486  $R = ($date1 <= $date2 ? '+' : '-');
     487  $r = ($date1 <= $date2 ? '' : '-');
     488
     489  //Calculate total days
     490  $diff->days = round(abs($date1->format('U') - $date2->format('U'))/86400);
     491
     492  //A leap year work around - consistent with DateInterval
     493  $leap_year = $date1->format('m-d') == '02-29';
     494  if ($leap_year)
     495  {
     496    $date1->modify('-1 day');
     497  }
     498
     499  //Years, months, days, hours
     500  $periods = array('years'=>-1, 'months'=>-1, 'days'=>-1, 'hours'=>-1);
     501
     502  foreach ($periods as $period => &$i)
     503  {
     504    if ($period == 'days' && $leap_year)
     505    {
     506      $date1->modify('+1 day');
     507    }
     508
     509    while ($date1 <= $date2 )
     510    {
     511      $date1->modify('+1 '.$period);
     512      $i++;
     513    }
     514
     515    //Reset date and record increments
     516    $date1->modify('-1 '.$period);
     517  }
     518 
     519  list($diff->y, $diff->m, $diff->d, $diff->h) = array_values($periods);
     520
     521  //Minutes, seconds
     522  $diff->s = round(abs($date1->format('U') - $date2->format('U')));
     523  $diff->i = floor($diff->s/60);
     524  $diff->s = $diff->s - $diff->i*60;
     525 
     526  return $diff;
     527}
     528
     529/**
    461530 * converts a string into a DateTime object
    462531 * @param: mixed, datetime string or timestamp int
     
    466535function str2DateTime($original, $format=null)
    467536{
    468   if (!empty($format))// from known date format
     537  if ( !empty($format) && version_compare(PHP_VERSION, '5.3.0') >= 0 )// from known date format
    469538  {
    470539    return DateTime::createFromFormat('!'.$format, $original); // ! char to reset fields to UNIX epoch
     
    562631 
    563632  $now = new DateTime();
    564   $diff = $now->diff($date);
    565  
     633  $diff = dateDiff($now, $date);
     634 
     635  $chunks = array(
     636    'year' => $diff->y,
     637    'month' => $diff->m,
     638    'week' => 0,
     639    'day' => $diff->d,
     640    'hour' => $diff->h,
     641    'minute' => $diff->i,
     642    'second' => $diff->s,
     643  );
     644 
     645  // DateInterval does not contain the number of weeks
    566646  if ($with_week)
    567647  {
    568     // DateInterval does not compute the number of weeks
    569     $diff->w = (int)floor($diff->d/7);
    570     $diff->d = $diff->d - $diff->w*7;
    571   }
    572   else
    573   {
    574     $diff->w = 0;
    575   }
    576  
    577   $chunks = array(
    578     'year' => 'y',
    579     'month' => 'm',
    580     'week' => 'w',
    581     'day' => 'd',
    582     'hour' => 'h',
    583     'minute' => 'i',
    584     'second' => 's',
    585   );
     648    $chunks['week'] = (int)floor($chunks['day']/7);
     649    $chunks['day'] = $chunks['day'] - $chunks['week']*7;
     650  }
    586651 
    587652  $j = array_search($stop, array_keys($chunks));
    588653 
    589654  $print = ''; $i=0;
    590   foreach ($chunks as $name => $var)
    591   {
    592     if ($diff->{$var} != 0)
    593     {
    594       $print.= ' '.l10n_dec('%d '.$name, '%d '.$name.'s', $diff->{$var});
     655  foreach ($chunks as $name => $value)
     656  {
     657    if ($value != 0)
     658    {
     659      $print.= ' '.l10n_dec('%d '.$name, '%d '.$name.'s', $value);
    595660    }
    596661    if (!empty($print) && $i >= $j)
Note: See TracChangeset for help on using the changeset viewer.