public function DatexObject::difference in Datex 7
Same name and namespace in other branches
- 7.2 datex_api/datex_api.class.inc \DatexObject::difference()
Returns amount of time difference to another date object.
Throws
Exception if given measure is week, it will throw an exception, it is not implemented yet.
File
- datex_api/
datex_api_classes.inc, line 887 - API and helper functions used by other datex modules.
Class
- DatexObject
- This class is Jalali equivilant of php DateTime. It also has some functionallity from object defiend in Drupal's date module DateObject.
Code
public function difference(DatexObject $date2_in, $measure = 'seconds', $absolute = TRUE) {
// Create cloned objects or original dates will be impacted by the
// date_modify() operations done in this code.
$date1 = $this
->getDateobjClone();
$date2 = $date2_in
->getDateobjClone();
$diff = date_format($date2, 'U') - date_format($date1, 'U');
if ($diff == 0) {
return 0;
}
elseif ($diff < 0 && $absolute) {
// Make sure $date1 is the smaller date.
$temp = $date2;
$date2 = $date1;
$date1 = $temp;
$diff = date_format($date2, 'U') - date_format($date1, 'U');
}
$year_diff = intval(date_format($date2, 'Y') - date_format($date1, 'Y'));
switch ($measure) {
case 'seconds':
return $diff;
case 'minutes':
return $diff / 60;
case 'hours':
return $diff / 3600;
case 'years':
return $year_diff;
case 'months':
$format = 'n';
$item1 = $this
->format_php($format, $date1);
$item2 = $this
->format_php($format, $date2);
if ($year_diff == 0) {
return intval($item2 - $item1);
}
else {
$item_diff = 12 - $item1;
$item_diff += intval(($year_diff - 1) * 12);
return $item_diff + $item2;
}
break;
case 'days':
$format = 'z';
$item1 = $this
->format_php($format, $date1);
$item2 = $this
->format_php($format, $date2);
if ($year_diff == 0) {
return intval($item2 - $item1);
}
else {
$item_diff = date_days_in_year($date1) - $item1;
for ($i = 1; $i < $year_diff; $i++) {
date_modify($date1, '+1 year');
$item_diff += date_days_in_year($date1);
}
return $item_diff + $item2;
}
break;
case 'weeks':
default:
break;
}
return NULL;
}