protected function DateObject::forceValid in Date 7.2
Same name and namespace in other branches
- 7.3 date_api/date_api.module \DateObject::forceValid()
- 7 date_api/date_api.module \DateObject::forceValid()
Converts a date part into something that will produce a valid date.
Parameters
string $part: The date part.
int $value: The date value for this part.
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'.
int $month: (optional) Used when the date part is less than 'month' to specify the date. Defaults to NULL.
int $year: (optional) Used when the date part is less than 'year' to specify the date. Defaults to NULL.
Return value
int A valid date value.
2 calls to DateObject::forceValid()
- DateObject::arrayErrors in date_api/
date_api.module - Finds possible errors in an array of date part values.
- DateObject::setFuzzyDate in date_api/
date_api.module - Forces an incomplete date to be valid.
File
- date_api/
date_api.module, line 928 - This module will make the date API available to other modules.
Class
- DateObject
- Extend PHP DateTime class.
Code
protected function forceValid($part, $value, $default = 'first', $month = NULL, $year = NULL) {
$now = date_now();
switch ($part) {
case 'year':
$fallback = $now
->format('Y');
return !is_int($value) || empty($value) || $value < variable_get('date_min_year', 1) || $value > variable_get('date_max_year', 4000) ? $fallback : $value;
case 'month':
$fallback = $default == 'first' ? 1 : $now
->format('n');
return !is_int($value) || empty($value) || $value <= 0 || $value > 12 ? $fallback : $value;
case 'day':
$fallback = $default == 'first' ? 1 : $now
->format('j');
$max_day = isset($year) && isset($month) ? date_days_in_month($year, $month) : 31;
return !is_int($value) || empty($value) || $value <= 0 || $value > $max_day ? $fallback : $value;
case 'hour':
$fallback = $default == 'first' ? 0 : $now
->format('G');
return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value;
case 'minute':
$fallback = $default == 'first' ? 0 : $now
->format('i');
return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
case 'second':
$fallback = $default == 'first' ? 0 : $now
->format('s');
return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
}
}