You are here

public function DateObject::setFuzzyDate in Date 7.2

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

Forces an incomplete date to be valid.

E.g., add a valid year, month, and day if only the time has been defined.

Parameters

array|string $date: An array of date parts or a datetime string with values to be massaged into a valid date object.

string $format: (optional) The format of the date. Defaults to NULL.

string $default: (optional) If the fallback should use the first value of the date part, or the current value of the date part. Defaults to 'first'.

File

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

Class

DateObject
Extend PHP DateTime class.

Code

public function setFuzzyDate($date, $format = NULL, $default = 'first') {
  $timezone = $this
    ->getTimeZone() ? $this
    ->getTimeZone()
    ->getName() : NULL;
  $comp = new DateObject($date, $timezone, $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']);
}