Révision 599a39cd
Ajouté par Assos Assos il y a environ 3 ans
drupal7/sites/all/modules/date/date_api/date_api.module | ||
---|---|---|
3 | 3 |
/** |
4 | 4 |
* @file |
5 | 5 |
* This module will make the date API available to other modules. |
6 |
* Designed to provide a light but flexible assortment of functions |
|
7 |
* and constants, with more functionality in additional files that |
|
8 |
* are not loaded unless other modules specifically include them. |
|
6 |
* |
|
7 |
* Designed to provide a light but flexible assortment of functions and |
|
8 |
* constants, with more functionality in additional files that are not loaded |
|
9 |
* unless other modules specifically include them. |
|
9 | 10 |
*/ |
10 | 11 |
|
11 | 12 |
/** |
... | ... | |
14 | 15 |
* Includes standard date types, format strings, strict regex strings for ISO |
15 | 16 |
* and DATETIME formats (seconds are optional). |
16 | 17 |
* |
17 |
* The loose regex will find any variety of ISO date and time, with or |
|
18 |
* without time, with or without dashes and colons separating the elements,
|
|
19 |
* and with either a 'T' or a space separating date and time.
|
|
18 |
* The loose regex will find any variety of ISO date and time, with or without
|
|
19 |
* time, with or without dashes and colons separating the elements, and with
|
|
20 |
* either a 'T' or a space separating date and time. |
|
20 | 21 |
*/ |
21 | 22 |
define('DATE_ISO', 'date'); |
22 | 23 |
define('DATE_UNIX', 'datestamp'); |
... | ... | |
68 | 69 |
$output .= '<h2>More Information</h2><p>' . t('Complete documentation for the Date and Date API modules is available at <a href="@link">http://drupal.org/node/92460</a>.', array('@link' => 'http://drupal.org/node/262062')) . '</p>'; |
69 | 70 |
|
70 | 71 |
return $output; |
71 |
|
|
72 | 72 |
} |
73 | 73 |
} |
74 | 74 |
|
... | ... | |
108 | 108 |
} |
109 | 109 |
|
110 | 110 |
return array('errors', $error_messages, 'success' => $success_messages); |
111 |
|
|
112 | 111 |
} |
113 | 112 |
|
114 | 113 |
/** |
115 | 114 |
* Implements hook_menu(). |
116 |
* |
|
117 |
* Creates a 'Date API' section on the administration page for Date |
|
118 |
* modules to use for their configuration and settings. |
|
119 | 115 |
*/ |
120 | 116 |
function date_api_menu() { |
117 |
// Creates a 'Date API' section on the administration page for Date modules |
|
118 |
// to use for their configuration and settings. |
|
121 | 119 |
$items['admin/config/date'] = array( |
122 | 120 |
'title' => 'Date API', |
123 | 121 |
'description' => 'Settings for modules the use the Date API.', |
... | ... | |
132 | 130 |
} |
133 | 131 |
|
134 | 132 |
/** |
135 |
* Extend PHP DateTime class with granularity handling, merge functionality and |
|
136 |
* slightly more flexible initialization parameters. |
|
133 |
* Extend PHP DateTime class. |
|
134 |
* |
|
135 |
* Adds granularity handling, merge functionality and slightly more flexible |
|
136 |
* initialization parameters. |
|
137 | 137 |
* |
138 | 138 |
* This class is a Drupal independent extension of the >= PHP 5.2 DateTime |
139 | 139 |
* class. |
... | ... | |
150 | 150 |
'hour', |
151 | 151 |
'minute', |
152 | 152 |
'second', |
153 |
'timezone' |
|
153 |
'timezone',
|
|
154 | 154 |
); |
155 | 155 |
private $serializedTime; |
156 | 156 |
private $serializedTimezone; |
... | ... | |
221 | 221 |
// Special handling for Unix timestamps expressed in the local timezone. |
222 | 222 |
// Create a date object in UTC and convert it to the local timezone. Don't |
223 | 223 |
// try to turn things like '2010' with a format of 'Y' into a timestamp. |
224 |
if (is_numeric($time) && (empty($format) || $format == 'U')) {
|
|
224 |
if (!is_array($time) && ctype_digit((string)$time) && (empty($format) || $format == 'U')) {
|
|
225 | 225 |
// Assume timestamp. |
226 | 226 |
$time = "@" . $time; |
227 | 227 |
$date = new DateObject($time, 'UTC'); |
... | ... | |
294 | 294 |
if (preg_match('/[a-zA-Z]/', $tz->getName())) { |
295 | 295 |
$this->setTimezone($tz); |
296 | 296 |
} |
297 |
// We have no information about the timezone so must fallback to a default. |
|
297 |
// We have no information about the timezone so must fallback to a |
|
298 |
// default. |
|
298 | 299 |
else { |
299 | 300 |
$this->setTimezone(new DateTimeZone("UTC")); |
300 | 301 |
$this->errors['timezone'] = t('No valid timezone name was provided.'); |
... | ... | |
305 | 306 |
/** |
306 | 307 |
* Merges two date objects together using the current date values as defaults. |
307 | 308 |
* |
308 |
* @param object $other
|
|
309 |
* @param DateObject $other
|
|
309 | 310 |
* Another date object to merge with. |
310 | 311 |
* |
311 |
* @return object
|
|
312 |
* @return DateObject
|
|
312 | 313 |
* A merged date object. |
313 | 314 |
*/ |
314 |
public function merge(FeedsDateTime $other) {
|
|
315 |
public function merge(DateObject $other) {
|
|
315 | 316 |
$other_tz = $other->getTimezone(); |
316 | 317 |
$this_tz = $this->getTimezone(); |
317 | 318 |
// Figure out which timezone to use for combination. |
... | ... | |
378 | 379 |
* Overrides base format function, formats this date according to its |
379 | 380 |
* available granularity, unless $force'ed not to limit to granularity. |
380 | 381 |
* |
381 |
* @TODO Add translation into this so translated names will be provided.
|
|
382 |
* @todo Add translation into this so translated names will be provided.
|
|
382 | 383 |
* |
383 | 384 |
* @param string $format |
384 | 385 |
* A date format string. |
385 | 386 |
* @param bool $force |
386 | 387 |
* Whether or not to limit the granularity. Defaults to FALSE. |
387 | 388 |
* |
388 |
* @return string|false
|
|
389 |
* @return string|bool
|
|
389 | 390 |
* Returns the formatted date string on success or FALSE on failure. |
390 | 391 |
*/ |
391 | 392 |
public function format($format, $force = FALSE) { |
... | ... | |
465 | 466 |
public function validGranularity($granularity = NULL, $flexible = FALSE) { |
466 | 467 |
$true = $this->hasGranularity() && (!$granularity || $flexible || $this->hasGranularity($granularity)); |
467 | 468 |
if (!$true && $granularity) { |
469 |
$allowed_values = array( |
|
470 |
'second', |
|
471 |
'minute', |
|
472 |
'hour', |
|
473 |
'day', |
|
474 |
'month', |
|
475 |
'year', |
|
476 |
); |
|
468 | 477 |
foreach ((array) $granularity as $part) { |
469 |
if (!$this->hasGranularity($part) && in_array($part, array( |
|
470 |
'second', |
|
471 |
'minute', |
|
472 |
'hour', |
|
473 |
'day', |
|
474 |
'month', |
|
475 |
'year') |
|
476 |
)) { |
|
478 |
if (!$this->hasGranularity($part) && in_array($part, $allowed_values)) { |
|
477 | 479 |
switch ($part) { |
478 | 480 |
case 'second': |
479 | 481 |
$this->errors[$part] = t('The second is missing.'); |
... | ... | |
536 | 538 |
* @param array $granularity |
537 | 539 |
* An array of date parts. |
538 | 540 |
*/ |
539 |
public function limitGranularity($granularity) { |
|
541 |
public function limitGranularity(array $granularity) {
|
|
540 | 542 |
foreach ($this->granularity as $key => $val) { |
541 | 543 |
if ($val != 'timezone' && !in_array($val, $granularity)) { |
542 | 544 |
unset($this->granularity[$key]); |
... | ... | |
615 | 617 |
$regex1 = str_replace('a', '(.)', $regex1); |
616 | 618 |
preg_match('`^' . $regex1 . '$`', stripslashes($format), $letters); |
617 | 619 |
array_shift($letters); |
618 |
// Extract values. |
|
619 |
$regex2 = preg_replace($patterns, $repl2, $format_regexp, 1); |
|
620 |
$regex2 = str_replace('A', '(AM|PM)', $regex2); |
|
621 |
$regex2 = str_replace('a', '(am|pm)', $regex2); |
|
620 |
|
|
621 |
$ampm_upper = date_ampm_options(FALSE, TRUE); |
|
622 |
$ampm_lower = date_ampm_options(FALSE, FALSE); |
|
623 |
$regex2 = strtr( |
|
624 |
preg_replace($patterns, $repl2, $format_regexp, 1), |
|
625 |
array( |
|
626 |
'A' => '(' . preg_quote($ampm_upper['am'], '`') . '|' . preg_quote($ampm_upper['pm'], '`') . ')', |
|
627 |
'a' => '(' . preg_quote($ampm_lower['am'], '`') . '|' . preg_quote($ampm_lower['pm'], '`') . ')', |
|
628 |
) |
|
629 |
); |
|
622 | 630 |
preg_match('`^' . $regex2 . '$`u', $date, $values); |
623 | 631 |
array_shift($values); |
624 | 632 |
// If we did not find all the values for the patterns in the format, abort. |
... | ... | |
678 | 686 |
break; |
679 | 687 |
|
680 | 688 |
case 'a': |
689 |
$ampm = ($value == $ampm_lower['am'] ? 'am' : 'pm'); |
|
690 |
break; |
|
691 |
|
|
681 | 692 |
case 'A': |
682 |
$ampm = strtolower($value);
|
|
693 |
$ampm = ($value == $ampm_upper['am'] ? 'am' : 'pm');
|
|
683 | 694 |
break; |
684 | 695 |
|
685 | 696 |
case 'g': |
... | ... | |
712 | 723 |
|
713 | 724 |
} |
714 | 725 |
} |
715 |
if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) { |
|
716 |
$final_date['hour'] += 12; |
|
717 |
} |
|
718 |
elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) { |
|
719 |
$final_date['hour'] -= 12; |
|
726 |
|
|
727 |
if (isset($ampm)) { |
|
728 |
if ($ampm == 'pm' && $final_date['hour'] < 12) { |
|
729 |
$final_date['hour'] += 12; |
|
730 |
} |
|
731 |
elseif ($ampm == 'am' && $final_date['hour'] == 12) { |
|
732 |
$final_date['hour'] -= 12; |
|
733 |
} |
|
720 | 734 |
} |
721 | 735 |
|
722 | 736 |
// Blank becomes current time, given TZ. |
... | ... | |
797 | 811 |
* (optional) Whether to force a full date by filling in missing values. |
798 | 812 |
* Defaults to FALSE. |
799 | 813 |
*/ |
800 |
public function toISO($arr, $full = FALSE) { |
|
814 |
public function toISO(array $arr, $full = FALSE) {
|
|
801 | 815 |
// Add empty values to avoid errors. The empty values must create a valid |
802 | 816 |
// date or we will get date slippage, i.e. a value of 2011-00-00 will get |
803 | 817 |
// interpreted as November of 2010 by PHP. |
... | ... | |
933 | 947 |
* @return array |
934 | 948 |
* An array of error messages, keyed by date part. |
935 | 949 |
*/ |
936 |
public function arrayErrors($arr) { |
|
950 |
public function arrayErrors(array $arr) {
|
|
937 | 951 |
$errors = array(); |
938 | 952 |
$now = date_now(); |
939 | 953 |
$default_month = !empty($arr['month']) ? $arr['month'] : $now->format('n'); |
... | ... | |
998 | 1012 |
public function difference($date2_in, $measure = 'seconds', $absolute = TRUE) { |
999 | 1013 |
// Create cloned objects or original dates will be impacted by the |
1000 | 1014 |
// date_modify() operations done in this code. |
1001 |
$date1 = clone($this);
|
|
1002 |
$date2 = clone($date2_in);
|
|
1015 |
$date1 = clone $this;
|
|
1016 |
$date2 = clone $date2_in;
|
|
1003 | 1017 |
if (is_object($date1) && is_object($date2)) { |
1004 | 1018 |
$diff = date_format($date2, 'U') - date_format($date1, 'U'); |
1005 | 1019 |
if ($diff == 0) { |
... | ... | |
1086 | 1100 |
} |
1087 | 1101 |
return NULL; |
1088 | 1102 |
} |
1103 |
|
|
1089 | 1104 |
} |
1090 | 1105 |
|
1091 | 1106 |
/** |
... | ... | |
1102 | 1117 |
* @return bool |
1103 | 1118 |
* TRUE if the element is effectively hidden, FALSE otherwise. |
1104 | 1119 |
*/ |
1105 |
function date_hidden_element($element) { |
|
1106 |
// @TODO What else needs to be tested to see if dates are hidden or disabled?
|
|
1120 |
function date_hidden_element(array $element) {
|
|
1121 |
// @todo What else needs to be tested to see if dates are hidden or disabled?
|
|
1107 | 1122 |
if ((isset($element['#access']) && empty($element['#access'])) |
1108 | 1123 |
|| !empty($element['#programmed']) |
1109 | 1124 |
|| in_array($element['#type'], array('hidden', 'value'))) { |
... | ... | |
1286 | 1301 |
|
1287 | 1302 |
default: |
1288 | 1303 |
$context = ''; |
1289 |
break; |
|
1290 | 1304 |
} |
1291 | 1305 |
foreach (date_week_days_untranslated() as $key => $day) { |
1292 | 1306 |
$weekdays[$key] = t(substr($day, 0, $length), array(), array('context' => $context)); |
... | ... | |
1304 | 1318 |
* @return array |
1305 | 1319 |
* An array of weekdays reordered to match the first day of the week. |
1306 | 1320 |
*/ |
1307 |
function date_week_days_ordered($weekdays) { |
|
1321 |
function date_week_days_ordered(array $weekdays) {
|
|
1308 | 1322 |
$first_day = variable_get('date_first_day', 0); |
1309 | 1323 |
if ($first_day > 0) { |
1310 | 1324 |
for ($i = 1; $i <= $first_day; $i++) { |
... | ... | |
1459 | 1473 |
* An array of AM and PM options. |
1460 | 1474 |
*/ |
1461 | 1475 |
function date_ampm($required = FALSE) { |
1462 |
$none = array('' => ''); |
|
1463 |
$ampm = array( |
|
1464 |
'am' => t('am', array(), array('context' => 'ampm')), |
|
1465 |
'pm' => t('pm', array(), array('context' => 'ampm')), |
|
1466 |
); |
|
1467 |
return !$required ? $none + $ampm : $ampm; |
|
1476 |
$ampm = date_ampm_options(FALSE, FALSE); |
|
1477 |
|
|
1478 |
return !$required ? array('' => '') + $ampm : $ampm; |
|
1479 |
} |
|
1480 |
|
|
1481 |
/** |
|
1482 |
* Constructs an array of AM and PM options without empty option. |
|
1483 |
* |
|
1484 |
* @param bool $key_upper |
|
1485 |
* If TRUE then the array key will be uppercase. |
|
1486 |
* @param bool $label_upper |
|
1487 |
* If TRUE then the array value will be uppercase. |
|
1488 |
* |
|
1489 |
* @return array |
|
1490 |
* An array of AM and PM options. |
|
1491 |
*/ |
|
1492 |
function date_ampm_options($key_upper, $label_upper) { |
|
1493 |
$am = $key_upper ? 'AM' : 'am'; |
|
1494 |
$pm = $key_upper ? 'PM' : 'pm'; |
|
1495 |
|
|
1496 |
if ($label_upper) { |
|
1497 |
return array( |
|
1498 |
$am => t('AM', array(), array('context' => 'ampm')), |
|
1499 |
$pm => t('PM', array(), array('context' => 'ampm')), |
|
1500 |
); |
|
1501 |
} |
|
1502 |
else { |
|
1503 |
return array( |
|
1504 |
$am => t('am', array(), array('context' => 'ampm')), |
|
1505 |
$pm => t('pm', array(), array('context' => 'ampm')), |
|
1506 |
); |
|
1507 |
} |
|
1468 | 1508 |
} |
1469 | 1509 |
|
1470 | 1510 |
/** |
... | ... | |
1543 | 1583 |
* @param array $granularity |
1544 | 1584 |
* An array of date parts. |
1545 | 1585 |
*/ |
1546 |
function date_granularity_sorted($granularity) { |
|
1586 |
function date_granularity_sorted(array $granularity) {
|
|
1547 | 1587 |
return array_intersect(array( |
1548 | 1588 |
'year', |
1549 | 1589 |
'month', |
... | ... | |
1596 | 1636 |
* @return string |
1597 | 1637 |
* The most precise element in a granularity array. |
1598 | 1638 |
*/ |
1599 |
function date_granularity_precision($granularity_array) { |
|
1639 |
function date_granularity_precision(array $granularity_array) {
|
|
1600 | 1640 |
$input = date_granularity_sorted($granularity_array); |
1601 | 1641 |
return array_pop($input); |
1602 | 1642 |
} |
... | ... | |
1837 | 1877 |
* (optional) PHP DateTimeZone object, string or NULL allowed. Optionally |
1838 | 1878 |
* force time to a specific timezone, defaults to user timezone, if set, |
1839 | 1879 |
* otherwise site timezone. Defaults to NULL. |
1840 |
* |
|
1841 | 1880 |
* @param bool $reset |
1842 | 1881 |
* (optional) Static cache reset. |
1843 | 1882 |
* |
... | ... | |
1845 | 1884 |
* The current time as a date object. |
1846 | 1885 |
*/ |
1847 | 1886 |
function date_now($timezone = NULL, $reset = FALSE) { |
1848 |
$static_var = __FUNCTION__ . $timezone; |
|
1849 | 1887 |
if ($timezone instanceof DateTimeZone) { |
1850 | 1888 |
$static_var = __FUNCTION__ . $timezone->getName(); |
1851 | 1889 |
} |
1890 |
else { |
|
1891 |
$static_var = __FUNCTION__ . $timezone; |
|
1892 |
} |
|
1852 | 1893 |
|
1853 | 1894 |
if ($reset) { |
1854 | 1895 |
drupal_static_reset($static_var); |
... | ... | |
1861 | 1902 |
} |
1862 | 1903 |
|
1863 | 1904 |
// Avoid unexpected manipulation of cached $now object |
1864 |
// by subsequent code execution |
|
1905 |
// by subsequent code execution.
|
|
1865 | 1906 |
// @see https://drupal.org/node/2261395 |
1866 | 1907 |
$clone = clone $now; |
1867 | 1908 |
return $clone; |
... | ... | |
2060 | 2101 |
date_modify($min_date, '-' . strval((7 + $day_wday - $first_day) % 7) . ' days'); |
2061 | 2102 |
|
2062 | 2103 |
// Move forwards to the last day of the week. |
2063 |
$max_date = clone($min_date);
|
|
2064 |
date_modify($max_date, '+6 days'); |
|
2104 |
$max_date = clone $min_date;
|
|
2105 |
date_modify($max_date, '+6 days +23 hours +59 minutes +59 seconds');
|
|
2065 | 2106 |
|
2066 | 2107 |
if (date_format($min_date, 'Y') != $year) { |
2067 | 2108 |
$min_date = new DateObject($year . '-01-01 00:00:00'); |
... | ... | |
2097 | 2138 |
} |
2098 | 2139 |
|
2099 | 2140 |
// Move forwards to the last day of the week. |
2100 |
$max_date = clone($min_date);
|
|
2101 |
date_modify($max_date, '+6 days'); |
|
2141 |
$max_date = clone $min_date;
|
|
2142 |
date_modify($max_date, '+6 days +23 hours +59 minutes +59 seconds');
|
|
2102 | 2143 |
return array($min_date, $max_date); |
2103 | 2144 |
} |
2104 | 2145 |
|
... | ... | |
2148 | 2189 |
|
2149 | 2190 |
// Remove the leap week if it's present. |
2150 | 2191 |
if ($date_year > intval($parts[0])) { |
2151 |
$last_date = clone($date);
|
|
2192 |
$last_date = clone $date;
|
|
2152 | 2193 |
date_modify($last_date, '-7 days'); |
2153 | 2194 |
$week = date_format($last_date, 'W') + 1; |
2154 | 2195 |
} |
... | ... | |
2202 | 2243 |
* @return bool |
2203 | 2244 |
* TRUE if the granularity contains a time portion, FALSE otherwise. |
2204 | 2245 |
*/ |
2205 |
function date_has_time($granularity) { |
|
2246 |
function date_has_time(array $granularity) {
|
|
2206 | 2247 |
if (!is_array($granularity)) { |
2207 | 2248 |
$granularity = array(); |
2208 | 2249 |
} |
... | ... | |
2219 | 2260 |
* @return bool |
2220 | 2261 |
* TRUE if the granularity contains a date portion, FALSE otherwise. |
2221 | 2262 |
*/ |
2222 |
function date_has_date($granularity) { |
|
2263 |
function date_has_date(array $granularity) {
|
|
2223 | 2264 |
if (!is_array($granularity)) { |
2224 | 2265 |
$granularity = array(); |
2225 | 2266 |
} |
... | ... | |
2266 | 2307 |
* @return string |
2267 | 2308 |
* The format string with all other elements removed. |
2268 | 2309 |
*/ |
2269 |
function date_limit_format($format, $granularity) { |
|
2310 |
function date_limit_format($format, array $granularity) {
|
|
2270 | 2311 |
// Use the advanced drupal_static() pattern to improve performance. |
2271 | 2312 |
static $drupal_static_fast; |
2272 | 2313 |
if (!isset($drupal_static_fast)) { |
... | ... | |
2436 | 2477 |
* Strips out unwanted granularity elements. |
2437 | 2478 |
* |
2438 | 2479 |
* @param array $granularity |
2439 |
* An array like ('year', 'month', 'day', 'hour', 'minute', 'second');
|
|
2480 |
* An array like ('year', 'month', 'day', 'hour', 'minute', 'second').
|
|
2440 | 2481 |
* |
2441 | 2482 |
* @return array |
2442 | 2483 |
* A reduced set of granularitiy elements. |
2443 | 2484 |
*/ |
2444 |
function date_nongranularity($granularity) { |
|
2485 |
function date_nongranularity(array $granularity) {
|
|
2445 | 2486 |
$options = array( |
2446 | 2487 |
'year', |
2447 | 2488 |
'month', |
... | ... | |
2473 | 2514 |
return array( |
2474 | 2515 |
'date_nav_title' => $base + array( |
2475 | 2516 |
'variables' => array( |
2476 |
'granularity' => NULL, 'view' => NULL, 'link' => NULL, 'format' => NULL |
|
2517 |
'granularity' => NULL, |
|
2518 |
'view' => NULL, |
|
2519 |
'link' => NULL, |
|
2520 |
'format' => NULL, |
|
2477 | 2521 |
), |
2478 | 2522 |
), |
2479 | 2523 |
'date_timezone' => $base + array('render element' => 'element'), |
... | ... | |
2497 | 2541 |
'date_calendar_day' => $base + array('variables' => array('date' => NULL)), |
2498 | 2542 |
'date_time_ago' => $base + array( |
2499 | 2543 |
'variables' => array( |
2500 |
'start_date' => NULL, 'end_date' => NULL, 'interval' => NULL |
|
2544 |
'start_date' => NULL, |
|
2545 |
'end_date' => NULL, |
|
2546 |
'interval' => NULL, |
|
2501 | 2547 |
), |
2502 | 2548 |
), |
2503 | 2549 |
); |
... | ... | |
2562 | 2608 |
case ('none'): |
2563 | 2609 |
default: |
2564 | 2610 |
$timezone = date_default_timezone(); |
2565 |
break; |
|
2566 | 2611 |
} |
2567 | 2612 |
return $timezone; |
2568 | 2613 |
} |
... | ... | |
2679 | 2724 |
* @return string |
2680 | 2725 |
* A min and max year string like '-3:+1'. |
2681 | 2726 |
*/ |
2682 |
function date_range_string($years) { |
|
2727 |
function date_range_string(array $years) {
|
|
2683 | 2728 |
$this_year = date_format(date_now(), 'Y'); |
2684 | 2729 |
|
2685 | 2730 |
if ($years[0] < $this_year) { |
... | ... | |
2776 | 2821 |
* @param string $string2 |
2777 | 2822 |
* A string date in datetime format for the 'end' date. |
2778 | 2823 |
* @param string $granularity |
2779 |
* (optional) The granularity of the date. Defaults to 'second'. |
|
2824 |
* (optional) The granularity of the date. Allowed values are: |
|
2825 |
* - 'second' (default) |
|
2826 |
* - 'minute' |
|
2827 |
* - 'hour' |
|
2780 | 2828 |
* @param int $increment |
2781 |
* (optional) The increment of the date. Defaults to 1. |
|
2829 |
* (optional) The increment of the date. Only allows positive integers. |
|
2830 |
* Defaults to 1. |
|
2782 | 2831 |
* |
2783 | 2832 |
* @return bool |
2784 | 2833 |
* TRUE if the date is all day, FALSE otherwise. |
2785 | 2834 |
*/ |
2786 | 2835 |
function date_is_all_day($string1, $string2, $granularity = 'second', $increment = 1) { |
2836 |
// Both date strings must be present. |
|
2787 | 2837 |
if (empty($string1) || empty($string2)) { |
2788 | 2838 |
return FALSE; |
2789 | 2839 |
} |
2840 |
// The granularity argument only allows three options. |
|
2790 | 2841 |
elseif (!in_array($granularity, array('hour', 'minute', 'second'))) { |
2791 | 2842 |
return FALSE; |
2792 | 2843 |
} |
2844 |
// The increment must be an integer. |
|
2845 |
elseif (!is_int($increment) || $increment === 0) { |
|
2846 |
return FALSE; |
|
2847 |
} |
|
2793 | 2848 |
|
2849 |
// Verify the first date argument is a valid date string. |
|
2794 | 2850 |
preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string1, $matches); |
2795 | 2851 |
$count = count($matches); |
2796 | 2852 |
$date1 = $count > 1 ? $matches[1] : ''; |
... | ... | |
2798 | 2854 |
$hour1 = $count > 3 ? intval($matches[3]) : 0; |
2799 | 2855 |
$min1 = $count > 4 ? intval($matches[4]) : 0; |
2800 | 2856 |
$sec1 = $count > 5 ? intval($matches[5]) : 0; |
2857 |
if (empty($date1) || empty($time1)) { |
|
2858 |
return FALSE; |
|
2859 |
} |
|
2860 |
|
|
2861 |
// Verify the second date argument is a valid date string. |
|
2801 | 2862 |
preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string2, $matches); |
2802 | 2863 |
$count = count($matches); |
2803 | 2864 |
$date2 = $count > 1 ? $matches[1] : ''; |
... | ... | |
2805 | 2866 |
$hour2 = $count > 3 ? intval($matches[3]) : 0; |
2806 | 2867 |
$min2 = $count > 4 ? intval($matches[4]) : 0; |
2807 | 2868 |
$sec2 = $count > 5 ? intval($matches[5]) : 0; |
2808 |
if (empty($date1) || empty($date2)) { |
|
2809 |
return FALSE; |
|
2810 |
} |
|
2811 |
if (empty($time1) || empty($time2)) { |
|
2869 |
if (empty($date2) || empty($time2)) { |
|
2812 | 2870 |
return FALSE; |
2813 | 2871 |
} |
2814 | 2872 |
|
... | ... | |
2817 | 2875 |
$tmp = date_minutes('i', TRUE, $increment); |
2818 | 2876 |
$max_minutes = intval(array_pop($tmp)); |
2819 | 2877 |
|
2820 |
// See if minutes and seconds are the maximum allowed for an increment or the |
|
2878 |
// See if minutes and seconds are the maximum allowed for an increment, or the
|
|
2821 | 2879 |
// maximum possible (59), or 0. |
2822 | 2880 |
switch ($granularity) { |
2823 | 2881 |
case 'second': |
2824 |
$min_match = $time1 == '00:00:00' |
|
2825 |
|| ($hour1 == 0 && $min1 == 0 && $sec1 == 0); |
|
2826 |
$max_match = $time2 == '00:00:00' |
|
2827 |
|| ($hour2 == 23 && in_array($min2, array($max_minutes, 59)) && in_array($sec2, array($max_seconds, 59))) |
|
2828 |
|| ($hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0 && $sec1 == 0 && $sec2 == 0); |
|
2882 |
$min_match = ($hour1 == 0 && $min1 == 0 && $sec1 == 0); |
|
2883 |
$max_match = ($hour2 == 23 && in_array($min2, array($max_minutes, 59)) && in_array($sec2, array($max_seconds, 59))) |
|
2884 |
|| ($date1 != $date2 && $hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0 && $sec1 == 0 && $sec2 == 0); |
|
2829 | 2885 |
break; |
2830 | 2886 |
|
2831 | 2887 |
case 'minute': |
2832 |
$min_match = $time1 == '00:00:00' |
|
2833 |
|| ($hour1 == 0 && $min1 == 0); |
|
2834 |
$max_match = $time2 == '00:00:00' |
|
2835 |
|| ($hour2 == 23 && in_array($min2, array($max_minutes, 59))) |
|
2836 |
|| ($hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0); |
|
2888 |
$min_match = ($hour1 == 0 && $min1 == 0); |
|
2889 |
$max_match = ($hour2 == 23 && in_array($min2, array($max_minutes, 59))) |
|
2890 |
|| ($date1 != $date2 && $hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0); |
|
2837 | 2891 |
break; |
2838 | 2892 |
|
2839 | 2893 |
case 'hour': |
2840 |
$min_match = $time1 == '00:00:00' |
|
2841 |
|| ($hour1 == 0); |
|
2842 |
$max_match = $time2 == '00:00:00' |
|
2843 |
|| ($hour2 == 23) |
|
2844 |
|| ($hour1 == 0 && $hour2 == 0); |
|
2894 |
$min_match = ($hour1 == 0); |
|
2895 |
$max_match = ($hour2 == 23) |
|
2896 |
|| ($date1 != $date2 && $hour1 == 0 && $hour2 == 0); |
|
2845 | 2897 |
break; |
2846 | 2898 |
|
2847 | 2899 |
default: |
... | ... | |
2907 | 2959 |
* Replace specific ISO values using patterns. |
2908 | 2960 |
* |
2909 | 2961 |
* Function will replace ISO values that have the pattern 9999-00-00T00:00:00 |
2910 |
* with a pattern like 9999-01-01T00:00:00, to match the behavior of non-ISO dates |
|
2911 |
* and ensure that date objects created from this value contain a valid month |
|
2912 |
* and day. |
|
2962 |
* with a pattern like 9999-01-01T00:00:00, to match the behavior of non-ISO |
|
2963 |
* dates and ensure that date objects created from this value contain a valid |
|
2964 |
* month and day. |
|
2965 |
* |
|
2913 | 2966 |
* Without this fix, the ISO date '2020-00-00T00:00:00' would be created as |
2914 | 2967 |
* November 30, 2019 (the previous day in the previous month). |
2915 | 2968 |
* |
... | ... | |
2919 | 2972 |
* @return mixed|string |
2920 | 2973 |
* replaced value, or incoming value. |
2921 | 2974 |
* |
2922 |
* @TODO Expand on this to work with all sorts of partial ISO dates.
|
|
2975 |
* @todo Expand on this to work with all sorts of partial ISO dates.
|
|
2923 | 2976 |
*/ |
2924 | 2977 |
function date_make_iso_valid($iso_string) { |
2925 | 2978 |
// If this isn't a value that uses an ISO pattern, there is nothing to do. |
Formats disponibles : Unified diff
Weekly update of contrib modules