Logo Search packages:      
Sourcecode: davical version File versions  Download package

iCalDate::DateDifference ( from )

Produce an iCalendar format DURATION for the difference between this an another iCalDate

Parameters:
date$fromThe start of the period
Returns:
string The date difference, as an iCalendar duration format

One way to simplify is to always go for positive differences

Also somewhat simpler if we can use seconds

From an intense reading of RFC2445 it appears that durations which are not expressible in Weeks/Days/Hours/Minutes/Seconds are invalid. ==> This code is not needed then :-) $yy = $from->_yy - $this->_yy; $mo = $from->_mo - $this->_mo; $dd = $from->_dd - $this->_dd; $hh = $from->_hh - $this->_hh; $mi = $from->_mi - $this->_mi; $ss = $from->_ss - $this->_ss;

if ( $ss < 0 ) { $mi -= 1; $ss += 60; } if ( $mi < 0 ) { $hh -= 1; $mi += 60; } if ( $hh < 0 ) { $dd -= 1; $hh += 24; } if ( $dd < 0 ) { $mo -= 1; $dd += $this->DaysInMonth(); } // Which will use $this->_(mo|yy) - seemingly sensible if ( $mo < 0 ) { $yy -= 1; $mo += 12; }

$result = ""; if ( $yy > 0) { $result .= $yy."Y"; } if ( $mo > 0) { $result .= $mo."M"; } if ( $dd > 0) { $result .= $dd."D"; } $result .= "T"; if ( $hh > 0) { $result .= $hh."H"; } if ( $mi > 0) { $result .= $mi."M"; } if ( $ss > 0) { $result .= $ss."S"; } return $result;

Definition at line 321 of file RRule.php.

References iCalDate().

                                   {
    if ( !is_object($from) ) {
      $from = new iCalDate($from);
    }
    if ( $from->_epoch < $this->_epoch ) {
      /** One way to simplify is to always go for positive differences */
      return( "-". $from->DateDifference( &$self ) );
    }
//    if ( $from->_yy == $this->_yy && $from->_mo == $this->_mo ) {
      /** Also somewhat simpler if we can use seconds */
      $diff = $from->_epoch - $this->_epoch;
      $result = "";
      if ( $diff >= 86400) {
        $result = intval($diff / 86400);
        $diff = $diff % 86400;
        if ( $diff == 0 && (($result % 7) == 0) ) {
          // Duration is an integer number of weeks.
          $result .= intval($result / 7) . "W";
          return $result;
        }
        $result .= "D";
      }
      $result = "P".$result."T";
      if ( $diff >= 3600) {
        $result .= intval($diff / 3600) . "H";
        $diff = $diff % 3600;
      }
      if ( $diff >= 60) {
        $result .= intval($diff / 60) . "M";
        $diff = $diff % 60;
      }
      if ( $diff > 0) {
        $result .= intval($diff) . "S";
      }
      return $result;
//    }

/**
* From an intense reading of RFC2445 it appears that durations which are not expressible
* in Weeks/Days/Hours/Minutes/Seconds are invalid.
*  ==> This code is not needed then :-)
    $yy = $from->_yy - $this->_yy;
    $mo = $from->_mo - $this->_mo;
    $dd = $from->_dd - $this->_dd;
    $hh = $from->_hh - $this->_hh;
    $mi = $from->_mi - $this->_mi;
    $ss = $from->_ss - $this->_ss;

    if ( $ss < 0 ) {  $mi -= 1;   $ss += 60;  }
    if ( $mi < 0 ) {  $hh -= 1;   $mi += 60;  }
    if ( $hh < 0 ) {  $dd -= 1;   $hh += 24;  }
    if ( $dd < 0 ) {  $mo -= 1;   $dd += $this->DaysInMonth();  } // Which will use $this->_(mo|yy) - seemingly sensible
    if ( $mo < 0 ) {  $yy -= 1;   $mo += 12;  }

    $result = "";
    if ( $yy > 0) {    $result .= $yy."Y";   }
    if ( $mo > 0) {    $result .= $mo."M";   }
    if ( $dd > 0) {    $result .= $dd."D";   }
    $result .= "T";
    if ( $hh > 0) {    $result .= $hh."H";   }
    if ( $mi > 0) {    $result .= $mi."M";   }
    if ( $ss > 0) {    $result .= $ss."S";   }
    return $result;
*/
  }

Here is the call graph for this function:


Generated by  Doxygen 1.6.0   Back to index