You are here

function date_is_all_day in Date 7.2

Same name and namespace in other branches
  1. 8 date_api/date_api.module \date_is_all_day()
  2. 6.2 date_api.module \date_is_all_day()
  3. 7.3 date_api/date_api.module \date_is_all_day()
  4. 7 date_api/date_api.module \date_is_all_day()

Determine if a start/end date combination qualify as 'All day'.

Parameters

string $string1: A string date in datetime format for the 'start' date.

string $string2: A string date in datetime format for the 'end' date.

string $granularity: (optional) The granularity of the date. Allowed values are:

  • 'second' (default)
  • 'minute'
  • 'hour'

int $increment: (optional) The increment of the date. Only allows positive integers. Defaults to 1.

Return value

bool TRUE if the date is all day, FALSE otherwise.

4 calls to date_is_all_day()
DateApiUnitTestCase::testDateIsAllDay in date_api/tests/DateApiUnitTestCase.test
Tests for date_is_all_day().
date_all_day_date_combo_process_alter in date_all_day/date_all_day.module
Implements hook_date_combo_process_alter().
date_all_day_field in date_all_day/date_all_day.module
Determine if a Start/End date combination qualify as 'All day'.
date_field_all_day in ./date.module
Duplicate of what is now date_all_day_field() in the Date All Day module.

File

date_api/date_api.module, line 2922
This module will make the date API available to other modules.

Code

function date_is_all_day($string1, $string2, $granularity = 'second', $increment = 1) {

  // Both date strings must be present.
  if (empty($string1) || empty($string2)) {
    return FALSE;
  }
  elseif (!in_array($granularity, array(
    'hour',
    'minute',
    'second',
  ))) {
    return FALSE;
  }
  elseif (!is_int($increment) || $increment === 0) {
    return FALSE;
  }

  // Verify the first date argument is a valid date string.
  preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string1, $matches);
  $count = count($matches);
  $date1 = $count > 1 ? $matches[1] : '';
  $time1 = $count > 2 ? $matches[2] : '';
  $hour1 = $count > 3 ? intval($matches[3]) : 0;
  $min1 = $count > 4 ? intval($matches[4]) : 0;
  $sec1 = $count > 5 ? intval($matches[5]) : 0;
  if (empty($date1) || empty($time1)) {
    return FALSE;
  }

  // Verify the second date argument is a valid date string.
  preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string2, $matches);
  $count = count($matches);
  $date2 = $count > 1 ? $matches[1] : '';
  $time2 = $count > 2 ? $matches[2] : '';
  $hour2 = $count > 3 ? intval($matches[3]) : 0;
  $min2 = $count > 4 ? intval($matches[4]) : 0;
  $sec2 = $count > 5 ? intval($matches[5]) : 0;
  if (empty($date2) || empty($time2)) {
    return FALSE;
  }
  $tmp = date_seconds('s', TRUE, $increment);
  $max_seconds = intval(array_pop($tmp));
  $tmp = date_minutes('i', TRUE, $increment);
  $max_minutes = intval(array_pop($tmp));

  // See if minutes and seconds are the maximum allowed for an increment, or the
  // maximum possible (59), or 0.
  switch ($granularity) {
    case 'second':
      $min_match = $hour1 == 0 && $min1 == 0 && $sec1 == 0;
      $max_match = $hour2 == 23 && in_array($min2, array(
        $max_minutes,
        59,
      )) && in_array($sec2, array(
        $max_seconds,
        59,
      )) || $date1 != $date2 && $hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0 && $sec1 == 0 && $sec2 == 0;
      break;
    case 'minute':
      $min_match = $hour1 == 0 && $min1 == 0;
      $max_match = $hour2 == 23 && in_array($min2, array(
        $max_minutes,
        59,
      )) || $date1 != $date2 && $hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0;
      break;
    case 'hour':
      $min_match = $hour1 == 0;
      $max_match = $hour2 == 23 || $date1 != $date2 && $hour1 == 0 && $hour2 == 0;
      break;
    default:
      $min_match = TRUE;
      $max_match = FALSE;
  }
  if ($min_match && $max_match) {
    return TRUE;
  }
  return FALSE;
}