/** * Compute difference between two days using a given measure. * * @param mixed $date1 * the starting date * @param mixed $date2 * the ending date * @param string $measure * 'years', 'months', 'weeks', 'days', 'hours', 'minutes', 'seconds' * @param string $type * the type of dates provided: * DATE_OBJECT, DATE_DATETIME, DATE_ISO, DATE_UNIX, DATE_ARRAY */ public function difference($date2_in, $measure = 'seconds') { // Create cloned objects or original dates will be impacted by // the date_modify() operations done in this code. $date1 = clone $this; $date2 = clone $date2_in; if (is_object($date1) && is_object($date2)) { $diff = date_format($date2, 'U') - date_format($date1, 'U'); if ($diff == 0) { return 0; } elseif ($diff < 0) { // 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) { // The easy cases first. case 'seconds': return $diff; case 'minutes': return $diff / 60; case 'hours': return $diff / 3600; case 'years': return $year_diff; case 'months': $format = 'n'; $item1 = date_format($date1, $format); $item2 = date_format($date2, $format); 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 = date_format($date1, $format); $item2 = date_format($date2, $format); 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': $week_diff = date_format($date2, 'W') - date_format($date1, 'W'); $year_diff = date_format($date2, 'o') - date_format($date1, 'o'); for ($i = 1; $i <= $year_diff; $i++) { date_modify($date1, '+1 year'); $week_diff += date_iso_weeks_in_year($date1); } return $week_diff; } } return NULL; }
/** * @todo. */ public function testDateAPI() { $calendar = system_calendar(); // Test the order of the weeks days for a calendar that starts on Monday and // one that starts on Sunday. variable_set('date_first_day', 1); $expected = array(0 => t('Mon'), 1 => t('Tue'), 2 => t('Wed'), 3 => t('Thu'), 4 => t('Fri'), 5 => t('Sat'), 6 => t('Sun')); $days = $calendar->week_days_ordered($calendar->week_days_abbr(1)); $this->assertEqual($expected, $days, 'Test that $calendar->week_days_ordered() array starts on Monday when the site first day is on Monday.'); variable_set('date_first_day', 0); $expected = array(0 => t('Sun'), 1 => t('Mon'), 2 => t('Tue'), 3 => t('Wed'), 4 => t('Thu'), 5 => t('Fri'), 6 => t('Sat')); $days = $calendar->week_days_ordered($calendar->week_days_abbr(1)); $this->assertEqual($expected, $days, 'Test that $calendar->week_days_ordered() array starts on Sunday when the site first day is on Sunday.'); // Test days in February for a leap year and a non-leap year. $expected = 28; $date = new DrupalDateTime(array('year' => 2005, 'month' => 2)); $value = $calendar->days_in_month($date); $this->assertEqual($expected, $value, "Test \$calendar->days_in_month(2, 2005): should be {$expected}, found {$value}."); $expected = 29; $date = new DrupalDateTime(array('year' => 2004, 'month' => 2)); $value = $calendar->days_in_month($date); $this->assertEqual($expected, $value, "Test \$calendar->days_in_month(2, 2004): should be {$expected}, found {$value}."); // Test days in year for a leap year and a non-leap year. $expected = 365; $date = new DrupalDateTime('2005-06-01 00:00:00'); $value = $calendar->days_in_year($date); $this->assertEqual($expected, $value, "Test \$calendar->days_in_year(2005-06-01): should be {$expected}, found {$value}."); $expected = 366; $date = new DrupalDateTime('2004-06-01 00:00:00'); $value = $calendar->days_in_year($date); $this->assertEqual($expected, $value, "Test \$calendar->days_in_year(2004-06-01): should be {$expected}, found {$value}."); // Test ISO weeks for a leap year and a non-leap year. $expected = 52; $value = date_iso_weeks_in_year('2008-06-01 00:00:00'); $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2008-06-01): should be {$expected}, found {$value}."); $expected = 53; $value = date_iso_weeks_in_year('2009-06-01 00:00:00'); $this->assertEqual($expected, $value, "Test date_iso_weeks_in_year(2009-06-01): should be {$expected}, found {$value}."); // Test day of week for March 1, the day after leap day. $expected = 6; $date = new DrupalDateTime('2008-03-01 00:00:00'); $value = $calendar->day_of_week($date); $this->assertEqual($expected, $value, "Test \$calendar->day_of_week(2008-03-01): should be {$expected}, found {$value}."); $expected = 0; $date = new DrupalDateTime('2009-03-01 00:00:00'); $value = $calendar->day_of_week($date); $this->assertEqual($expected, $value, "Test \$calendar->day_of_week(2009-03-01): should be {$expected}, found {$value}."); // Test day of week name for March 1, the day after leap day. $expected = 'Sat'; $date = new DrupalDateTime('2008-03-01 00:00:00'); $value = $calendar->day_of_week_name($date); $this->assertEqual($expected, $value, "Test \$calendar->day_of_week_name(2008-03-01): should be {$expected}, found {$value}."); $expected = 'Sun'; $date = new DrupalDateTime('2009-03-01 00:00:00'); $value = $calendar->day_of_week_name($date); $this->assertEqual($expected, $value, "Test \$calendar->day_of_week_name(2009-03-01): should be {$expected}, found {$value}."); // Test week range with calendar weeks. variable_set('date_first_day', 0); $expected = '2008-01-27 to 2008-02-03'; $result = date_calendar_week_range(5, 2008); $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test calendar date_calendar_week_range(5, 2008): should be {$expected}, found {$value}."); $expected = '2009-01-25 to 2009-02-01'; $result = date_calendar_week_range(5, 2009); $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test calendar date_calendar_week_range(5, 2009): should be {$expected}, found {$value}."); // And now with ISO weeks. variable_set('date_first_day', 1); $expected = '2008-01-28 to 2008-02-04'; $result = date_iso_week_range(5, 2008); $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test ISO date_iso_week_range(5, 2008): should be {$expected}, found {$value}."); $expected = '2009-01-26 to 2009-02-02'; $result = date_iso_week_range(5, 2009); $value = $result[0]->format(DATE_FORMAT_DATE) . ' to ' . $result[1]->format(DATE_FORMAT_DATE); $this->assertEqual($expected, $value, "Test ISO date_iso_week_range(5, 2009): should be {$expected}, found {$value}."); config('date_api.settings')->set('iso8601', FALSE)->save(); // Find calendar week for a date. variable_set('date_first_day', 0); $expected = '09'; $value = date_calendar_week('2008-03-01'); $this->assertEqual($expected, $value, "Test date_calendar_week(2008-03-01): should be {$expected}, found {$value}."); $expected = '10'; $value = date_calendar_week('2009-03-01'); $this->assertEqual($expected, $value, "Test date_calendar_week(2009-03-01): should be {$expected}, found {$value}."); // Test date ranges. $valid = array('-20:+20', '-1:+0', '-10:-5', '2000:2020', '-10:2010', '1980:-10', '1920:+20'); $invalid = array('abc', 'abc:+20', '1920:+20a', '+-20:+-30', '12:12', '0:+20', '-20:0'); foreach ($valid as $range) { $this->assertTrue(date_range_valid($range), "{$range} recognized as a valid date range."); } foreach ($invalid as $range) { $this->assertFalse(date_range_valid($range), "{$range} recognized as an invalid date range."); } }