You are here

public function DateObject::setFuzzyDate in Date 7

Same name and namespace in other branches
  1. 7.3 date_api/date_api.module \DateObject::setFuzzyDate()
  2. 7.2 date_api/date_api.module \DateObject::setFuzzyDate()

Force an incomplete date to be valid, for instance to add a valid year, month, and day if only the time has been defined.

Parameters

$date: An array of date parts or a datetime string with values to be forced into date.

$format: The format of the date.

$default : 'current' - default to current day values. 'first' - default to the first possible valid value.

File

date_api/date_api.module, line 540
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.

Class

DateObject
Extend PHP DateTime class with granularity handling, merge functionality and slightly more flexible initialization parameters.

Code

public function setFuzzyDate($date, $format = NULL, $default = 'first') {
  $comp = new DateObject($date, $this
    ->getTimeZone()
    ->getName(), $format);
  $arr = $comp
    ->toArray(TRUE);
  foreach ($arr as $key => $value) {

    // Set to intval here and then test that it is still an integer.
    // Needed because sometimes valid integers come through as strings.
    $arr[$key] = $this
      ->forceValid($key, intval($value), $default, $arr['month'], $arr['year']);
  }
  $this
    ->setDate($arr['year'], $arr['month'], $arr['day']);
  $this
    ->setTime($arr['hour'], $arr['minute'], $arr['second']);
}