You are here

protected static function Datelist::incrementRound in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/Datetime/Element/Datelist.php \Drupal\Core\Datetime\Element\Datelist::incrementRound()

Rounds minutes and seconds to nearest requested value.

Parameters

$date:

$increment:

1 call to Datelist::incrementRound()
Datelist::valueCallback in core/lib/Drupal/Core/Datetime/Element/Datelist.php
Validates the date type to adjust 12 hour time and prevent invalid dates. If the date is valid, the date is set in the form.

File

core/lib/Drupal/Core/Datetime/Element/Datelist.php, line 354

Class

Datelist
Provides a datelist element.

Namespace

Drupal\Core\Datetime\Element

Code

protected static function incrementRound(&$date, $increment) {

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