You are here

function date_increment_round in Date 8

Same name and namespace in other branches
  1. 5.2 date_api_elements.inc \date_increment_round()
  2. 6.2 date_api.module \date_increment_round()
  3. 6 date_api_elements.inc \date_increment_round()
  4. 7.3 date_api/date_api.module \date_increment_round()
  5. 7 date_api/date_api_elements.inc \date_increment_round()
  6. 7.2 date_api/date_api.module \date_increment_round()

Helper function to round minutes and seconds to requested value.

1 call to date_increment_round()
date_devel_generate in ./date.devel_generate.inc
Implements hook_devel_generate().

File

date_api/date_api.module, line 605
This module will make the date API available to other modules. Designed to provide a light but flexible assortment of functions and constants, with more functionality in additional files that are not loaded unless other modules specifically include them.

Code

function date_increment_round(&$date, $increment) {

  // Round minutes and seconds, if necessary.
  if ($date instanceof DrupalDateTime && $increment > 1) {
    $day = intval(date_format($date, 'j'));
    $hour = intval(date_format($date, 'H'));
    $second = intval(round(intval(date_format($date, 's')) / $increment) * $increment);
    $minute = intval(date_format($date, 'i'));
    if ($second == 60) {
      $minute += 1;
      $second = 0;
    }
    $minute = intval(round($minute / $increment) * $increment);
    if ($minute == 60) {
      $hour += 1;
      $minute = 0;
    }
    date_time_set($date, $hour, $minute, $second);
    if ($hour == 24) {
      $day += 1;
      $hour = 0;
      $year = date_format($date, 'Y');
      $month = date_format($date, 'n');
      date_date_set($date, $year, $month, $day);
    }
  }
  return $date;
}