You are here

function date_limit_value in Date 6

Same name and namespace in other branches
  1. 5.2 date_api.module \date_limit_value()
  2. 6.2 date_api.module \date_limit_value()

Recalculate a date so it only includes elements from a granularity array. Helps prevent errors when unwanted values round up and ensures that unwanted date part values don't get stored in the database.

Example: date_limit_value('2007-05-15 04:45:59', array('year', 'month', 'day')) returns '2007-05-15 00:00:00'

Parameters

$date: a date value

$granularity: an array of allowed date parts, like ('year', 'month', 'day', 'hour', 'minute', 'second');

$type: the type of date value provided, DATE_DATETIME, DATE_ISO, DATE_UNIX, or DATE_ARRAY

Return value

the date with the unwanted parts reset to zeros (or ones if zeros are invalid for that date type).

2 calls to date_limit_value()
date_copy_import_ical_form_submit in date_copy/date_copy.module
_date_field_update in date/date_elements.inc
Private implementation of hook_field update and insert operations.

File

./date_api.module, line 1152
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_limit_value($date, $granularity, $type = DATE_DATETIME) {
  if (!date_is_valid($date, $type) || !($nongranularity = date_nongranularity($granularity))) {
    return $date;
  }
  else {
    $date = date_convert($date, $type, DATE_ARRAY);
    foreach ($nongranularity as $level) {
      switch ($level) {
        case 'second':
          $date['second'] = 0;
          break;
        case 'minute':
          $date['minute'] = 0;
          break;
        case 'hour':
          $date['hour'] = 0;
          break;
        case 'month':
          $date['month'] = $type != DATE_ISO ? 1 : 0;
          break;
        case 'day':
          $date['day'] = $type != DATE_ISO ? 1 : 0;
          break;
      }
    }
    return date_convert($date, DATE_ARRAY, $type);
  }
}