function date_part_extract in Date 5.2
Same name and namespace in other branches
- 6.2 date_api.module \date_part_extract()
- 6 date_api.module \date_part_extract()
Extract integer value of any date part from any type of date.
Example: date_part_extract('2007-03-15 00:00', 'month', DATE_DATETIME) returns: 3
Parameters
mixed $date: the date value to analyze.
string $part: the part of the date to extract, 'year', 'month', 'day', 'hour', 'minute', 'second'
string $type: the type of date supplied, DATE_ISO, DATE_UNIX, DATE_DATETIME, or DATE_OBJECT;
Return value
integer the integer value of the requested date part.
3 calls to date_part_extract()
- DateAPI::testDateAPI in tests/
date_api.test - date_is_valid in ./
date_api.module - Functions to test the validity of a date in various formats. Has special case for ISO dates and arrays which can be missing month and day and still be valid.
- date_php4.inc in date_php4/
date_php4.inc
File
- ./
date_api.module, line 1160 - 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_part_extract($date, $part, $type = DATE_DATETIME, $tz = 'UTC') {
$formats = array(
'year' => 'Y',
'month' => 'n',
'day' => 'j',
'hour' => 'G',
'minute' => 'i',
'second' => 's',
);
$positions = array(
'year' => 0,
'month' => 5,
'day' => 8,
'hour' => 11,
'minute' => 14,
'second' => 17,
);
$ipositions = array(
'year' => 0,
'month' => 4,
'day' => 6,
'hour' => 9,
'minute' => 11,
'second' => 13,
);
switch ($type) {
case DATE_ARRAY:
return (int) $date[$part];
case DATE_DATETIME:
case DATE_ISO:
return (int) substr($date, $positions[$part], $part == 'year' ? 4 : 2);
case DATE_ICAL:
return (int) substr($date, $ipositions[$part], $part == 'year' ? 4 : 2);
case DATE_UNIX:
// Special case when creating dates with timestamps.
// The date_create() function will assume date is UTC value
// and will ignore our timezone.
$date = date_create("@{$date}", timezone_open('UTC'));
date_timezone_set($date, timezone_open($tz));
return date_format($date, $formats[$part]);
case DATE_OBJECT:
return date_format($date, $formats[$part]);
}
}