date_api.module

  1. cis7 sites/all/modules/ulmus/date/date_api/date_api.module
  2. cle7 sites/all/modules/ulmus/date/date_api/date_api.module
  3. ecd7 sites/all/modules/ulmus/date/date_api/date_api.module
  4. elmsmedia7 sites/all/modules/ulmus/date/date_api/date_api.module
  5. harmony7 sites/all/modules/ulmus/date/date_api/date_api.module
  6. icor7 sites/all/modules/ulmus/date/date_api/date_api.module
  7. meedjum_blog7 sites/all/modules/ulmus/date/date_api/date_api.module
  8. mooc7 sites/all/modules/ulmus/date/date_api/date_api.module

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.

Functions

Namesort descending Description
date_ampm Constructs an array of AM and PM options.
date_api_database_info Temporary helper to re-create equivalent of content_database_info().
date_api_element_info Implements hook_element_info().
date_api_form_system_regional_settings_alter Implements hook_form_FORM_ID_alter() for system_regional_settings().
date_api_form_system_settings_validate Validate that the option to use ISO weeks matches first day of week choice.
date_api_menu Implements hook_menu().
date_api_status Helper function to retun the status of required date variables.
date_api_theme Implements hook_theme().
date_days Constructs an array of days in a month.
date_days_in_month Identifies the number of days in a month for a date.
date_days_in_year Identifies the number of days in a year for a date.
date_day_of_week Returns day of week for a given date (0 = Sunday).
date_day_of_week_name Returns translated name of the day of week for a given date.
date_default_timezone Returns a timezone name to use as a default.
date_default_timezone_object Returns a timezone object for the default timezone.
date_example_date Creates an example date.
date_format_date Formats a date, using a date type or a custom date format string.
date_format_interval Formats a time interval with granularity, including past and future context.
date_format_order Converts a format to an ordered array of granularity parts.
date_format_patterns Constructs an array of regex replacement strings for date format elements.
date_format_type_options Creates an array of date format types for use as an options list.
date_get_timezone Function to figure out which local timezone applies to a date and select it.
date_get_timezone_db Function to figure out which db timezone applies to a date and select it.
date_granularity_array_from_precision Constructs an array of granularity based on a given precision.
date_granularity_format Constructs a valid DATETIME format string for the granularity of an item.
date_granularity_names Constructs an array of granularity options and their labels.
date_granularity_precision Give a granularity array, return the highest precision.
date_granularity_sorted Sorts a granularity array.
date_has_date Determines if the granularity contains a date portion.
date_has_time Determines if the granularity contains a time portion.
date_help Implements hook_help().
date_hidden_element Determines if the date element needs to be processed.
date_hours Constructs an array of hours.
date_increment_round Helper function to round minutes and seconds to requested value.
date_iso_weeks_in_year Identifies the number of ISO weeks in a year for a date.
date_iso_week_range Calculates the start and end dates for an ISO week.
date_is_all_day Determine if a start/end date combination qualify as 'All day'.
date_is_date Determines if a date object is valid.
date_limit_format Limits a date format to include only elements from a given granularity array.
date_make_iso_valid This function will replace ISO values that have the pattern 9999-00-00T00:00:00 with a pattern like 9999-01-01T00:00:00, to match the behavior of non-ISO dates and ensure that date objects created from this value contain a valid month and day. Without…
date_minutes Constructs an array of minutes.
date_month_names Returns a translated array of month names.
date_month_names_abbr Constructs a translated array of month name abbreviations
date_month_names_untranslated Constructs an untranslated array of month names.
date_nongranularity Strips out unwanted granularity elements.
date_now A date object for the current time.
date_order Creates an array of ordered strings, using English text when possible.
date_order_translated Helper function for converting back and forth from '+1' to 'First'.
date_pad Helper function to left pad date parts with zeros.
date_part_format Helper function to get a format for a specific part of a date field.
date_range_string Converts a min and max year into a string like '-3:+1'.
date_range_valid Tests validity of a date range string.
date_range_years Splits a string like -3:+3 or 2001:2010 into an array of min and max years.
date_seconds Constructs an array of seconds.
date_timezone_abbr Returns an array of system-allowed timezone abbreviations.
date_timezone_is_valid Determines if a timezone string is valid.
date_timezone_names Returns a translated array of timezone names.
date_type_format Helper function for getting the format string for a date type.
date_week The calendar week number for a date.
date_weeks_in_year The number of calendar weeks in a year.
date_week_days Returns a translated array of week names.
date_week_days_abbr Constructs a translated array of week day abbreviations.
date_week_days_ordered Reorders weekdays to match the first day of the week.
date_week_days_untranslated Constructs an untranslated array of week days.
date_week_range Calculates the start and end dates for a calendar week.
date_years Constructs an array of years.

Constants

Classes

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

File

sites/all/modules/ulmus/date/date_api/date_api.module
View source
  1. <?php
  2. /**
  3. * @file
  4. * This module will make the date API available to other modules.
  5. * Designed to provide a light but flexible assortment of functions
  6. * and constants, with more functionality in additional files that
  7. * are not loaded unless other modules specifically include them.
  8. */
  9. /**
  10. * Set up some constants.
  11. *
  12. * Includes standard date types, format strings, strict regex strings for ISO
  13. * and DATETIME formats (seconds are optional).
  14. *
  15. * The loose regex will find any variety of ISO date and time, with or
  16. * without time, with or without dashes and colons separating the elements,
  17. * and with either a 'T' or a space separating date and time.
  18. */
  19. define('DATE_ISO', 'date');
  20. define('DATE_UNIX', 'datestamp');
  21. define('DATE_DATETIME', 'datetime');
  22. define('DATE_ARRAY', 'array');
  23. define('DATE_OBJECT', 'object');
  24. define('DATE_ICAL', 'ical');
  25. define('DATE_FORMAT_ISO', "Y-m-d\TH:i:s");
  26. define('DATE_FORMAT_UNIX', "U");
  27. define('DATE_FORMAT_DATETIME', "Y-m-d H:i:s");
  28. define('DATE_FORMAT_ICAL', "Ymd\THis");
  29. define('DATE_FORMAT_ICAL_DATE', "Ymd");
  30. define('DATE_FORMAT_DATE', 'Y-m-d');
  31. define('DATE_REGEX_ISO', '/(\d{4})?(-(\d{2}))?(-(\d{2}))?([T\s](\d{2}))?(:(\d{2}))?(:(\d{2}))?/');
  32. define('DATE_REGEX_DATETIME', '/(\d{4})-(\d{2})-(\d{2})\s(\d{2}):(\d{2}):?(\d{2})?/');
  33. define('DATE_REGEX_LOOSE', '/(\d{4})-?(\d{1,2})-?(\d{1,2})([T\s]?(\d{2}):?(\d{2}):?(\d{2})?(\.\d+)?(Z|[\+\-]\d{2}:?\d{2})?)?/');
  34. define('DATE_REGEX_ICAL_DATE', '/(\d{4})(\d{2})(\d{2})/');
  35. define('DATE_REGEX_ICAL_DATETIME', '/(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})(Z)?/');
  36. /**
  37. * Core DateTime extension module used for as many date operations as possible.
  38. */
  39. /**
  40. * Implements hook_help().
  41. */
  42. function date_help($path, $arg) {
  43. switch ($path) {
  44. case 'admin/help#date':
  45. $output = '';
  46. $messages = date_api_status();
  47. $output = '<h2>Date API Status</h2>';
  48. if (!empty($messages['success'])) {
  49. $output .= '<ul><li>' . implode('</li><li>', $messages['success']) . '</li></ul>';
  50. }
  51. if (!empty($messages['errors'])) {
  52. $output .= '<h3>Errors</h3><ul class="error"><li>' . implode('</li><li>', $messages['errors']) . '</li></ul>';
  53. }
  54. if (module_exists('date_tools')) {
  55. $output .= '<h3>Date Tools</h3>' . t('Dates and calendars can be complicated to set up. The !date_wizard makes it easy to create a simple date content type and with a date field. ', array('!date_wizard' => l(t('Date wizard'), 'admin/config/date/tools/date_wizard')));
  56. }
  57. else {
  58. $output .= '<h3>Date Tools</h3>' . t('Dates and calendars can be complicated to set up. If you enable the Date Tools module, it provides a Date Wizard that makes it easy to create a simple date content type with a date field. ');
  59. }
  60. $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>';
  61. return $output;
  62. break;
  63. }
  64. }
  65. /**
  66. * Helper function to retun the status of required date variables.
  67. */
  68. function date_api_status() {
  69. $t = get_t();
  70. $error_messages = array();
  71. $success_messages = array();
  72. $value = variable_get('date_default_timezone');
  73. if (isset($value)) {
  74. $success_messages[] = $t('The timezone has been set to <a href="@regional_settings">@timezone</a>.', array('@regional_settings' => url('admin/config/regional/settings'), '@timezone' => $value));
  75. }
  76. else {
  77. $error_messages[] = $t('The Date API requires that you set up the <a href="@regional_settings">site timezone</a> to function correctly.', array('@regional_settings' => url('admin/config/regional/settings')));
  78. }
  79. $value = variable_get('date_first_day');
  80. if (isset($value)) {
  81. $days = date_week_days();
  82. $success_messages[] = $t('The first day of the week has been set to <a href="@regional_settings">@day</a>.', array('@regional_settings' => url('admin/config/regional/settings'), '@day' => $days[$value]));
  83. }
  84. else {
  85. $error_messages[] = $t('The Date API requires that you set up the <a href="@regional_settings">site first day of week settings</a> to function correctly.', array('@regional_settings' => url('admin/config/regional/settings')));
  86. }
  87. $value = variable_get('date_format_medium');
  88. if (isset($value)) {
  89. $now = date_now();
  90. $success_messages[] = $t('The medium date format type has been set to to @value. You may find it helpful to add new format types like Date, Time, Month, or Year, with appropriate formats, at <a href="@regional_date_time">Date and time</a> settings.', array('@value' => $now->format($value), '@regional_date_time' => url('admin/config/regional/date-time')));
  91. }
  92. else {
  93. $error_messages[] = $t('The Date API requires that you set up the <a href="@regional_date_time">system date formats</a> to function correctly.', array('@regional_date_time' => url('admin/config/regional/date-time')));
  94. }
  95. return array('errors', $error_messages, 'success' => $success_messages);
  96. }
  97. /**
  98. * Implements hook_menu().
  99. *
  100. * Creates a 'Date API' section on the administration page for Date
  101. * modules to use for their configuration and settings.
  102. */
  103. function date_api_menu() {
  104. $items['admin/config/date'] = array(
  105. 'title' => 'Date API',
  106. 'description' => 'Settings for modules the use the Date API.',
  107. 'position' => 'left',
  108. 'weight' => -10,
  109. 'page callback' => 'system_admin_menu_block_page',
  110. 'access arguments' => array('administer site configuration'),
  111. 'file' => 'system.admin.inc',
  112. 'file path' => drupal_get_path('module', 'system'),
  113. );
  114. return $items;
  115. }
  116. /**
  117. * Extend PHP DateTime class with granularity handling, merge functionality and
  118. * slightly more flexible initialization parameters.
  119. *
  120. * This class is a Drupal independent extension of the >= PHP 5.2 DateTime
  121. * class.
  122. *
  123. * @see FeedsDateTimeElement class
  124. */
  125. class DateObject extends DateTime {
  126. public $granularity = array();
  127. public $errors = array();
  128. protected static $allgranularity = array('year', 'month', 'day', 'hour', 'minute', 'second', 'timezone');
  129. private $serializedTime;
  130. private $serializedTimezone;
  131. /**
  132. * Prepares the object during serialization.
  133. *
  134. * We are extending a core class and core classes cannot be serialized.
  135. *
  136. * @return array
  137. * Returns an array with the names of the variables that were serialized.
  138. *
  139. * @see http://bugs.php.net/41334
  140. * @see http://bugs.php.net/39821
  141. */
  142. public function __sleep() {
  143. $this->serializedTime = $this->format('c');
  144. $this->serializedTimezone = $this->getTimezone()->getName();
  145. return array('serializedTime', 'serializedTimezone');
  146. }
  147. /**
  148. * Re-builds the object using local variables.
  149. */
  150. public function __wakeup() {
  151. $this->__construct($this->serializedTime, new DateTimeZone($this->serializedTimezone));
  152. }
  153. /**
  154. * Returns the date object as a string.
  155. *
  156. * @return string
  157. * The date object formatted as a string.
  158. */
  159. public function __toString() {
  160. return $this->format(DATE_FORMAT_DATETIME) . ' ' . $this->getTimeZone()->getName();
  161. }
  162. /**
  163. * Constructs a date object.
  164. *
  165. * @param string $time
  166. * A date/time string or array. Defaults to 'now'.
  167. * @param object|string|null $tz
  168. * PHP DateTimeZone object, string or NULL allowed. Defaults to NULL.
  169. * @param string $format
  170. * PHP date() type format for parsing. Doesn't support timezones; if you
  171. * have a timezone, send NULL and the default constructor method will
  172. * hopefully parse it. $format is recommended in order to use negative or
  173. * large years, which php's parser fails on.
  174. */
  175. public function __construct($time = 'now', $tz = NULL, $format = NULL) {
  176. $this->timeOnly = FALSE;
  177. $this->dateOnly = FALSE;
  178. // Store the raw time input so it is available for validation.
  179. $this->originalTime = $time;
  180. // Allow string timezones.
  181. if (!empty($tz) && !is_object($tz)) {
  182. $tz = new DateTimeZone($tz);
  183. }
  184. // Default to the site timezone when not explicitly provided.
  185. elseif (empty($tz)) {
  186. $tz = date_default_timezone_object();
  187. }
  188. // Special handling for Unix timestamps expressed in the local timezone.
  189. // Create a date object in UTC and convert it to the local timezone. Don't
  190. // try to turn things like '2010' with a format of 'Y' into a timestamp.
  191. if (is_numeric($time) && (empty($format) || $format == 'U')) {
  192. // Assume timestamp.
  193. $time = "@" . $time;
  194. $date = new DateObject($time, 'UTC');
  195. if ($tz->getName() != 'UTC') {
  196. $date->setTimezone($tz);
  197. }
  198. $time = $date->format(DATE_FORMAT_DATETIME);
  199. $format = DATE_FORMAT_DATETIME;
  200. $this->addGranularity('timezone');
  201. }
  202. elseif (is_array($time)) {
  203. // Assume we were passed an indexed array.
  204. if (empty($time['year']) && empty($time['month']) && empty($time['day'])) {
  205. $this->timeOnly = TRUE;
  206. }
  207. if (empty($time['hour']) && empty($time['minute']) && empty($time['second'])) {
  208. $this->dateOnly = TRUE;
  209. }
  210. $this->errors = $this->arrayErrors($time);
  211. // Make this into an ISO date, forcing a full ISO date even if some values
  212. // are missing.
  213. $time = $this->toISO($time, TRUE);
  214. // We checked for errors already, skip parsing the input values.
  215. $format = NULL;
  216. }
  217. else {
  218. // Make sure dates like 2010-00-00T00:00:00 get converted to
  219. // 2010-01-01T00:00:00 before creating a date object
  220. // to avoid unintended changes in the month or day.
  221. $time = date_make_iso_valid($time);
  222. }
  223. // The parse function will also set errors on the date parts.
  224. if (!empty($format)) {
  225. $arg = self::$allgranularity;
  226. $element = array_pop($arg);
  227. while (!$this->parse($time, $tz, $format) && $element != 'year') {
  228. $element = array_pop($arg);
  229. $format = date_limit_format($format, $arg);
  230. }
  231. if ($element == 'year') {
  232. return FALSE;
  233. }
  234. }
  235. elseif (is_string($time)) {
  236. // PHP < 5.3 doesn't like the GMT- notation for parsing timezones.
  237. $time = str_replace("GMT-", "-", $time);
  238. $time = str_replace("GMT+", "+", $time);
  239. // We are going to let the parent dateObject do a best effort attempt to
  240. // turn this string into a valid date. It might fail and we want to
  241. // control the error messages.
  242. try {
  243. @parent::__construct($time, $tz);
  244. }
  245. catch (Exception $e) {
  246. $this->errors['date'] = $e;
  247. return;
  248. }
  249. if (empty($this->granularity)) {
  250. $this->setGranularityFromTime($time, $tz);
  251. }
  252. }
  253. // If we haven't got a valid timezone name yet, we need to set one or
  254. // we will get undefined index errors.
  255. // This can happen if $time had an offset or no timezone.
  256. if (!$this->getTimezone() || !preg_match('/[a-zA-Z]/', $this->getTimezone()->getName())) {
  257. // If the original $tz has a name, use it.
  258. if (preg_match('/[a-zA-Z]/', $tz->getName())) {
  259. $this->setTimezone($tz);
  260. }
  261. // We have no information about the timezone so must fallback to a default.
  262. else {
  263. $this->setTimezone(new DateTimeZone("UTC"));
  264. $this->errors['timezone'] = t('No valid timezone name was provided.');
  265. }
  266. }
  267. }
  268. /**
  269. * Merges two date objects together using the current date values as defaults.
  270. *
  271. * @param object $other
  272. * Another date object to merge with.
  273. *
  274. * @return object
  275. * A merged date object.
  276. */
  277. public function merge(FeedsDateTime $other) {
  278. $other_tz = $other->getTimezone();
  279. $this_tz = $this->getTimezone();
  280. // Figure out which timezone to use for combination.
  281. $use_tz = ($this->hasGranularity('timezone') || !$other->hasGranularity('timezone')) ? $this_tz : $other_tz;
  282. $this2 = clone $this;
  283. $this2->setTimezone($use_tz);
  284. $other->setTimezone($use_tz);
  285. $val = $this2->toArray(TRUE);
  286. $otherval = $other->toArray();
  287. foreach (self::$allgranularity as $g) {
  288. if ($other->hasGranularity($g) && !$this2->hasGranularity($g)) {
  289. // The other class has a property we don't; steal it.
  290. $this2->addGranularity($g);
  291. $val[$g] = $otherval[$g];
  292. }
  293. }
  294. $other->setTimezone($other_tz);
  295. $this2->setDate($val['year'], $val['month'], $val['day']);
  296. $this2->setTime($val['hour'], $val['minute'], $val['second']);
  297. return $this2;
  298. }
  299. /**
  300. * Sets the time zone for the current date.
  301. *
  302. * Overrides default DateTime function. Only changes output values if
  303. * actually had time granularity. This should be used as a "converter" for
  304. * output, to switch tzs.
  305. *
  306. * In order to set a timezone for a datetime that doesn't have such
  307. * granularity, merge() it with one that does.
  308. *
  309. * @param object $tz
  310. * A timezone object.
  311. * @param bool $force
  312. * Whether or not to skip a date with no time. Defaults to FALSE.
  313. */
  314. public function setTimezone($tz, $force = FALSE) {
  315. // PHP 5.2.6 has a fatal error when setting a date's timezone to itself.
  316. // http://bugs.php.net/bug.php?id=45038
  317. if (version_compare(PHP_VERSION, '5.2.7', '<') && $tz == $this->getTimezone()) {
  318. $tz = new DateTimeZone($tz->getName());
  319. }
  320. if (!$this->hasTime() || !$this->hasGranularity('timezone') || $force) {
  321. // This has no time or timezone granularity, so timezone doesn't mean
  322. // much. We set the timezone using the method, which will change the
  323. // day/hour, but then we switch back.
  324. $arr = $this->toArray(TRUE);
  325. parent::setTimezone($tz);
  326. $this->setDate($arr['year'], $arr['month'], $arr['day']);
  327. $this->setTime($arr['hour'], $arr['minute'], $arr['second']);
  328. $this->addGranularity('timezone');
  329. return;
  330. }
  331. return parent::setTimezone($tz);
  332. }
  333. /**
  334. * Returns date formatted according to given format.
  335. *
  336. * Overrides base format function, formats this date according to its
  337. * available granularity, unless $force'ed not to limit to granularity.
  338. *
  339. * @TODO Add translation into this so translated names will be provided.
  340. *
  341. * @param string $format
  342. * A date format string.
  343. * @param bool $force
  344. * Whether or not to limit the granularity. Defaults to FALSE.
  345. *
  346. * @return string|false
  347. * Returns the formatted date string on success or FALSE on failure.
  348. */
  349. public function format($format, $force = FALSE) {
  350. return parent::format($force ? $format : date_limit_format($format, $this->granularity));
  351. }
  352. /**
  353. * Adds a granularity entry to the array.
  354. *
  355. * @param string $g
  356. * A single date part.
  357. */
  358. public function addGranularity($g) {
  359. $this->granularity[] = $g;
  360. $this->granularity = array_unique($this->granularity);
  361. }
  362. /**
  363. * Removes a granularity entry from the array.
  364. *
  365. * @param string $g
  366. * A single date part.
  367. */
  368. public function removeGranularity($g) {
  369. if ($key = array_search($g, $this->granularity)) {
  370. unset($this->granularity[$key]);
  371. }
  372. }
  373. /**
  374. * Checks granularity array for a given entry.
  375. *
  376. * @param array|null $g
  377. * An array of date parts. Defaults to NULL.
  378. *
  379. * @returns bool
  380. * TRUE if the date part is present in the date's granularity.
  381. */
  382. public function hasGranularity($g = NULL) {
  383. if ($g === NULL) {
  384. // Just want to know if it has something valid means no lower
  385. // granularities without higher ones.
  386. $last = TRUE;
  387. foreach (self::$allgranularity as $arg) {
  388. if ($arg == 'timezone') {
  389. continue;
  390. }
  391. if (in_array($arg, $this->granularity) && !$last) {
  392. return FALSE;
  393. }
  394. $last = in_array($arg, $this->granularity);
  395. }
  396. return in_array('year', $this->granularity);
  397. }
  398. if (is_array($g)) {
  399. foreach ($g as $gran) {
  400. if (!in_array($gran, $this->granularity)) {
  401. return FALSE;
  402. }
  403. }
  404. return TRUE;
  405. }
  406. return in_array($g, $this->granularity);
  407. }
  408. /**
  409. * Determines if a a date is valid for a given granularity.
  410. *
  411. * @param array|null $granularity
  412. * An array of date parts. Defaults to NULL.
  413. * @param bool $flexible
  414. * TRUE if the granuliarty is flexible, FALSE otherwise. Defaults to FALSE.
  415. *
  416. * @return bool
  417. * Whether a date is valid for a given granularity.
  418. */
  419. public function validGranularity($granularity = NULL, $flexible = FALSE) {
  420. $true = $this->hasGranularity() && (!$granularity || $flexible || $this->hasGranularity($granularity));
  421. if (!$true && $granularity) {
  422. foreach ((array) $granularity as $part) {
  423. if (!$this->hasGranularity($part) && in_array($part, array('second', 'minute', 'hour', 'day', 'month', 'year'))) {
  424. $this->errors[$part] = t("The $part is missing.");
  425. }
  426. }
  427. }
  428. return $true;
  429. }
  430. /**
  431. * Returns whether this object has time set.
  432. *
  433. * Used primarily for timezone conversion and formatting.
  434. *
  435. * @return bool
  436. * TRUE if the date contains time parts, FALSE otherwise.
  437. */
  438. public function hasTime() {
  439. return $this->hasGranularity('hour');
  440. }
  441. /**
  442. * Returns whether the input values included a year.
  443. *
  444. * Useful to use pseudo date objects when we only are interested in the time.
  445. *
  446. * @todo $this->completeDate does not actually exist?
  447. */
  448. public function completeDate() {
  449. return $this->completeDate;
  450. }
  451. /**
  452. * Removes unwanted date parts from a date.
  453. *
  454. * In common usage we should not unset timezone through this.
  455. *
  456. * @param array $granularity
  457. * An array of date parts.
  458. */
  459. public function limitGranularity($granularity) {
  460. foreach ($this->granularity as $key => $val) {
  461. if ($val != 'timezone' && !in_array($val, $granularity)) {
  462. unset($this->granularity[$key]);
  463. }
  464. }
  465. }
  466. /**
  467. * Determines the granularity of a date based on the constructor's arguments.
  468. *
  469. * @param string $time
  470. * A date string.
  471. * @param bool $tz
  472. * TRUE if the date has a timezone, FALSE otherwise.
  473. */
  474. protected function setGranularityFromTime($time, $tz) {
  475. $this->granularity = array();
  476. $temp = date_parse($time);
  477. // Special case for 'now'.
  478. if ($time == 'now') {
  479. $this->granularity = array('year', 'month', 'day', 'hour', 'minute', 'second');
  480. }
  481. else {
  482. // This PHP date_parse() method currently doesn't have resolution down to
  483. // seconds, so if there is some time, all will be set.
  484. foreach (self::$allgranularity as $g) {
  485. if ((isset($temp[$g]) && is_numeric($temp[$g])) || ($g == 'timezone' && (isset($temp['zone_type']) && $temp['zone_type'] > 0))) {
  486. $this->granularity[] = $g;
  487. }
  488. }
  489. }
  490. if ($tz) {
  491. $this->addGranularity('timezone');
  492. }
  493. }
  494. /**
  495. * Converts a date string into a date object.
  496. *
  497. * @param string $date
  498. * The date string to parse.
  499. * @param object $tz
  500. * A timezone object.
  501. * @param string $format
  502. * The date format string.
  503. *
  504. * @return object
  505. * Returns the date object.
  506. */
  507. protected function parse($date, $tz, $format) {
  508. $array = date_format_patterns();
  509. foreach ($array as $key => $value) {
  510. // The letter with no preceding '\'.
  511. $patterns[] = "`(^|[^\\\\\\\\])" . $key . "`";
  512. // A single character.
  513. $repl1[] = '${1}(.)';
  514. // The.
  515. $repl2[] = '${1}(' . $value . ')';
  516. }
  517. $patterns[] = "`\\\\\\\\([" . implode(array_keys($array)) . "])`";
  518. $repl1[] = '${1}';
  519. $repl2[] = '${1}';
  520. $format_regexp = preg_quote($format);
  521. // Extract letters.
  522. $regex1 = preg_replace($patterns, $repl1, $format_regexp, 1);
  523. $regex1 = str_replace('A', '(.)', $regex1);
  524. $regex1 = str_replace('a', '(.)', $regex1);
  525. preg_match('`^' . $regex1 . '$`', stripslashes($format), $letters);
  526. array_shift($letters);
  527. // Extract values.
  528. $regex2 = preg_replace($patterns, $repl2, $format_regexp, 1);
  529. $regex2 = str_replace('A', '(AM|PM)', $regex2);
  530. $regex2 = str_replace('a', '(am|pm)', $regex2);
  531. preg_match('`^' . $regex2 . '$`u', $date, $values);
  532. array_shift($values);
  533. // If we did not find all the values for the patterns in the format, abort.
  534. if (count($letters) != count($values)) {
  535. $this->errors['invalid'] = t('The value @date does not match the expected format.', array('@date' => $date));
  536. return FALSE;
  537. }
  538. $this->granularity = array();
  539. $final_date = array('hour' => 0, 'minute' => 0, 'second' => 0, 'month' => 1, 'day' => 1, 'year' => 0);
  540. foreach ($letters as $i => $letter) {
  541. $value = $values[$i];
  542. switch ($letter) {
  543. case 'd':
  544. case 'j':
  545. $final_date['day'] = intval($value);
  546. $this->addGranularity('day');
  547. break;
  548. case 'n':
  549. case 'm':
  550. $final_date['month'] = intval($value);
  551. $this->addGranularity('month');
  552. break;
  553. case 'F':
  554. $array_month_long = array_flip(date_month_names());
  555. $final_date['month'] = array_key_exists($value, $array_month_long) ? $array_month_long[$value] : -1;
  556. $this->addGranularity('month');
  557. break;
  558. case 'M':
  559. $array_month = array_flip(date_month_names_abbr());
  560. $final_date['month'] = array_key_exists($value, $array_month) ? $array_month[$value] : -1;
  561. $this->addGranularity('month');
  562. break;
  563. case 'Y':
  564. $final_date['year'] = $value;
  565. $this->addGranularity('year');
  566. if (strlen($value) < 4) {
  567. $this->errors['year'] = t('The year is invalid. Please check that entry includes four digits.');
  568. }
  569. break;
  570. case 'y':
  571. $year = $value;
  572. // If no century, we add the current one ("06" => "2006").
  573. $final_date['year'] = str_pad($year, 4, substr(date("Y"), 0, 2), STR_PAD_LEFT);
  574. $this->addGranularity('year');
  575. break;
  576. case 'a':
  577. case 'A':
  578. $ampm = strtolower($value);
  579. break;
  580. case 'g':
  581. case 'h':
  582. case 'G':
  583. case 'H':
  584. $final_date['hour'] = intval($value);
  585. $this->addGranularity('hour');
  586. break;
  587. case 'i':
  588. $final_date['minute'] = intval($value);
  589. $this->addGranularity('minute');
  590. break;
  591. case 's':
  592. $final_date['second'] = intval($value);
  593. $this->addGranularity('second');
  594. break;
  595. case 'U':
  596. parent::__construct($value, $tz ? $tz : new DateTimeZone("UTC"));
  597. $this->addGranularity('year');
  598. $this->addGranularity('month');
  599. $this->addGranularity('day');
  600. $this->addGranularity('hour');
  601. $this->addGranularity('minute');
  602. $this->addGranularity('second');
  603. return $this;
  604. break;
  605. }
  606. }
  607. if (isset($ampm) && $ampm == 'pm' && $final_date['hour'] < 12) {
  608. $final_date['hour'] += 12;
  609. }
  610. elseif (isset($ampm) && $ampm == 'am' && $final_date['hour'] == 12) {
  611. $final_date['hour'] -= 12;
  612. }
  613. // Blank becomes current time, given TZ.
  614. parent::__construct('', $tz ? $tz : new DateTimeZone("UTC"));
  615. if ($tz) {
  616. $this->addGranularity('timezone');
  617. }
  618. // SetDate expects an integer value for the year, results can be unexpected
  619. // if we feed it something like '0100' or '0000'.
  620. $final_date['year'] = intval($final_date['year']);
  621. $this->errors += $this->arrayErrors($final_date);
  622. $granularity = drupal_map_assoc($this->granularity);
  623. // If the input value is '0000-00-00', PHP's date class will later
  624. // incorrectly convert it to something like '-0001-11-30' if we do setDate()
  625. // here. If we don't do setDate() here, it will default to the current date
  626. // and we will lose any way to tell that there was no date in the orignal
  627. // input values. So set a flag we can use later to tell that this date
  628. // object was created using only time values, and that the date values are
  629. // artifical.
  630. if (empty($final_date['year']) && empty($final_date['month']) && empty($final_date['day'])) {
  631. $this->timeOnly = TRUE;
  632. }
  633. elseif (empty($this->errors)) {
  634. // setDate() expects a valid year, month, and day.
  635. // Set some defaults for dates that don't use this to
  636. // keep PHP from interpreting it as the last day of
  637. // the previous month or last month of the previous year.
  638. if (empty($granularity['month'])) {
  639. $final_date['month'] = 1;
  640. }
  641. if (empty($granularity['day'])) {
  642. $final_date['day'] = 1;
  643. }
  644. $this->setDate($final_date['year'], $final_date['month'], $final_date['day']);
  645. }
  646. if (!isset($final_date['hour']) && !isset($final_date['minute']) && !isset($final_date['second'])) {
  647. $this->dateOnly = TRUE;
  648. }
  649. elseif (empty($this->errors)) {
  650. $this->setTime($final_date['hour'], $final_date['minute'], $final_date['second']);
  651. }
  652. return $this;
  653. }
  654. /**
  655. * Returns all standard date parts in an array.
  656. *
  657. * Will return '' for parts in which it lacks granularity.
  658. *
  659. * @param bool $force
  660. * Whether or not to limit the granularity. Defaults to FALSE.
  661. *
  662. * @return array
  663. * An array of formatted date part values, keyed by date parts.
  664. */
  665. public function toArray($force = FALSE) {
  666. return array(
  667. 'year' => $this->format('Y', $force),
  668. 'month' => $this->format('n', $force),
  669. 'day' => $this->format('j', $force),
  670. 'hour' => intval($this->format('H', $force)),
  671. 'minute' => intval($this->format('i', $force)),
  672. 'second' => intval($this->format('s', $force)),
  673. 'timezone' => $this->format('e', $force),
  674. );
  675. }
  676. /**
  677. * Creates an ISO date from an array of values.
  678. *
  679. * @param array $arr
  680. * An array of date values keyed by date part.
  681. * @param bool $full
  682. * (optional) Whether to force a full date by filling in missing values.
  683. * Defaults to FALSE.
  684. */
  685. public function toISO($arr, $full = FALSE) {
  686. // Add empty values to avoid errors. The empty values must create a valid
  687. // date or we will get date slippage, i.e. a value of 2011-00-00 will get
  688. // interpreted as November of 2010 by PHP.
  689. if ($full) {
  690. $arr += array('year' => 0, 'month' => 1, 'day' => 1, 'hour' => 0, 'minute' => 0, 'second' => 0);
  691. }
  692. else {
  693. $arr += array('year' => '', 'month' => '', 'day' => '', 'hour' => '', 'minute' => '', 'second' => '');
  694. }
  695. $datetime = '';
  696. if ($arr['year'] !== '') {
  697. $datetime = date_pad(intval($arr['year']), 4);
  698. if ($full || $arr['month'] !== '') {
  699. $datetime .= '-' . date_pad(intval($arr['month']));
  700. if ($full || $arr['day'] !== '') {
  701. $datetime .= '-' . date_pad(intval($arr['day']));
  702. }
  703. }
  704. }
  705. if ($arr['hour'] !== '') {
  706. $datetime .= $datetime ? 'T' : '';
  707. $datetime .= date_pad(intval($arr['hour']));
  708. if ($full || $arr['minute'] !== '') {
  709. $datetime .= ':' . date_pad(intval($arr['minute']));
  710. if ($full || $arr['second'] !== '') {
  711. $datetime .= ':' . date_pad(intval($arr['second']));
  712. }
  713. }
  714. }
  715. return $datetime;
  716. }
  717. /**
  718. * Forces an incomplete date to be valid.
  719. *
  720. * E.g., add a valid year, month, and day if only the time has been defined.
  721. *
  722. * @param array|string $date
  723. * An array of date parts or a datetime string with values to be massaged
  724. * into a valid date object.
  725. * @param string $format
  726. * (optional) The format of the date. Defaults to NULL.
  727. * @param string $default
  728. * (optional) If the fallback should use the first value of the date part,
  729. * or the current value of the date part. Defaults to 'first'.
  730. */
  731. public function setFuzzyDate($date, $format = NULL, $default = 'first') {
  732. $timezone = $this->getTimeZone() ? $this->getTimeZone()->getName() : NULL;
  733. $comp = new DateObject($date, $timezone, $format);
  734. $arr = $comp->toArray(TRUE);
  735. foreach ($arr as $key => $value) {
  736. // Set to intval here and then test that it is still an integer.
  737. // Needed because sometimes valid integers come through as strings.
  738. $arr[$key] = $this->forceValid($key, intval($value), $default, $arr['month'], $arr['year']);
  739. }
  740. $this->setDate($arr['year'], $arr['month'], $arr['day']);
  741. $this->setTime($arr['hour'], $arr['minute'], $arr['second']);
  742. }
  743. /**
  744. * Converts a date part into something that will produce a valid date.
  745. *
  746. * @param string $part
  747. * The date part.
  748. * @param int $value
  749. * The date value for this part.
  750. * @param string $default
  751. * (optional) If the fallback should use the first value of the date part,
  752. * or the current value of the date part. Defaults to 'first'.
  753. * @param int $month
  754. * (optional) Used when the date part is less than 'month' to specify the
  755. * date. Defaults to NULL.
  756. * @param int $year
  757. * (optional) Used when the date part is less than 'year' to specify the
  758. * date. Defaults to NULL.
  759. *
  760. * @return int
  761. * A valid date value.
  762. */
  763. protected function forceValid($part, $value, $default = 'first', $month = NULL, $year = NULL) {
  764. $now = date_now();
  765. switch ($part) {
  766. case 'year':
  767. $fallback = $now->format('Y');
  768. return !is_int($value) || empty($value) || $value < variable_get('date_min_year', 1) || $value > variable_get('date_max_year', 4000) ? $fallback : $value;
  769. break;
  770. case 'month':
  771. $fallback = $default == 'first' ? 1 : $now->format('n');
  772. return !is_int($value) || empty($value) || $value <= 0 || $value > 12 ? $fallback : $value;
  773. break;
  774. case 'day':
  775. $fallback = $default == 'first' ? 1 : $now->format('j');
  776. $max_day = isset($year) && isset($month) ? date_days_in_month($year, $month) : 31;
  777. return !is_int($value) || empty($value) || $value <= 0 || $value > $max_day ? $fallback : $value;
  778. break;
  779. case 'hour':
  780. $fallback = $default == 'first' ? 0 : $now->format('G');
  781. return !is_int($value) || $value < 0 || $value > 23 ? $fallback : $value;
  782. break;
  783. case 'minute':
  784. $fallback = $default == 'first' ? 0 : $now->format('i');
  785. return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
  786. break;
  787. case 'second':
  788. $fallback = $default == 'first' ? 0 : $now->format('s');
  789. return !is_int($value) || $value < 0 || $value > 59 ? $fallback : $value;
  790. break;
  791. }
  792. }
  793. /**
  794. * Finds possible errors in an array of date part values.
  795. *
  796. * The forceValid() function will change an invalid value to a valid one, so
  797. * we just need to see if the value got altered.
  798. *
  799. * @param array $arr
  800. * An array of date values, keyed by date part.
  801. *
  802. * @return array
  803. * An array of error messages, keyed by date part.
  804. */
  805. public function arrayErrors($arr) {
  806. $errors = array();
  807. $now = date_now();
  808. $default_month = !empty($arr['month']) ? $arr['month'] : $now->format('n');
  809. $default_year = !empty($arr['year']) ? $arr['year'] : $now->format('Y');
  810. $this->granularity = array();
  811. foreach ($arr as $part => $value) {
  812. // Explicitly set the granularity to the values in the input array.
  813. if (is_numeric($value)) {
  814. $this->addGranularity($part);
  815. }
  816. // Avoid false errors when a numeric value is input as a string by casting
  817. // as an integer.
  818. $value = intval($value);
  819. if (!empty($value) && $this->forceValid($part, $value, 'now', $default_month, $default_year) != $value) {
  820. // Use a switch/case to make translation easier by providing a different
  821. // message for each part.
  822. switch ($part) {
  823. case 'year':
  824. $errors['year'] = t('The year is invalid.');
  825. break;
  826. case 'month':
  827. $errors['month'] = t('The month is invalid.');
  828. break;
  829. case 'day':
  830. $errors['day'] = t('The day is invalid.');
  831. break;
  832. case 'hour':
  833. $errors['hour'] = t('The hour is invalid.');
  834. break;
  835. case 'minute':
  836. $errors['minute'] = t('The minute is invalid.');
  837. break;
  838. case 'second':
  839. $errors['second'] = t('The second is invalid.');
  840. break;
  841. }
  842. }
  843. }
  844. if ($this->hasTime()) {
  845. $this->addGranularity('timezone');
  846. }
  847. return $errors;
  848. }
  849. /**
  850. * Computes difference between two days using a given measure.
  851. *
  852. * @param object $date2_in
  853. * The stop date.
  854. * @param string $measure
  855. * (optional) A granularity date part. Defaults to 'seconds'.
  856. * @param boolean $absolute
  857. * (optional) Indicate whether the absolute value of the difference should
  858. * be returned or if the sign should be retained. Defaults to TRUE.
  859. */
  860. public function difference($date2_in, $measure = 'seconds', $absolute = TRUE) {
  861. // Create cloned objects or original dates will be impacted by the
  862. // date_modify() operations done in this code.
  863. $date1 = clone($this);
  864. $date2 = clone($date2_in);
  865. if (is_object($date1) && is_object($date2)) {
  866. $diff = date_format($date2, 'U') - date_format($date1, 'U');
  867. if ($diff == 0) {
  868. return 0;
  869. }
  870. elseif ($diff < 0 && $absolute) {
  871. // Make sure $date1 is the smaller date.
  872. $temp = $date2;
  873. $date2 = $date1;
  874. $date1 = $temp;
  875. $diff = date_format($date2, 'U') - date_format($date1, 'U');
  876. }
  877. $year_diff = intval(date_format($date2, 'Y') - date_format($date1, 'Y'));
  878. switch ($measure) {
  879. // The easy cases first.
  880. case 'seconds':
  881. return $diff;
  882. case 'minutes':
  883. return $diff / 60;
  884. case 'hours':
  885. return $diff / 3600;
  886. case 'years':
  887. return $year_diff;
  888. case 'months':
  889. $format = 'n';
  890. $item1 = date_format($date1, $format);
  891. $item2 = date_format($date2, $format);
  892. if ($year_diff == 0) {
  893. return intval($item2 - $item1);
  894. }
  895. elseif ($year_diff < 0) {
  896. $item_diff = 0 - $item1;
  897. $item_diff -= intval((abs($year_diff) - 1) * 12);
  898. return $item_diff - (12 - $item2);
  899. }
  900. else {
  901. $item_diff = 12 - $item1;
  902. $item_diff += intval(($year_diff - 1) * 12);
  903. return $item_diff + $item2;
  904. }
  905. break;
  906. case 'days':
  907. $format = 'z';
  908. $item1 = date_format($date1, $format);
  909. $item2 = date_format($date2, $format);
  910. if ($year_diff == 0) {
  911. return intval($item2 - $item1);
  912. }
  913. elseif ($year_diff < 0) {
  914. $item_diff = 0 - $item1;
  915. for ($i = 1; $i < abs($year_diff); $i++) {
  916. date_modify($date1, '-1 year');
  917. $item_diff -= date_days_in_year($date1);
  918. }
  919. return $item_diff - (date_days_in_year($date2) - $item2);
  920. }
  921. else {
  922. $item_diff = date_days_in_year($date1) - $item1;
  923. for ($i = 1; $i < $year_diff; $i++) {
  924. date_modify($date1, '+1 year');
  925. $item_diff += date_days_in_year($date1);
  926. }
  927. return $item_diff + $item2;
  928. }
  929. break;
  930. case 'weeks':
  931. $week_diff = date_format($date2, 'W') - date_format($date1, 'W');
  932. $year_diff = date_format($date2, 'o') - date_format($date1, 'o');
  933. $sign = ($year_diff < 0) ? -1 : 1;
  934. for ($i = 1; $i <= abs($year_diff); $i++) {
  935. date_modify($date1, (($sign > 0) ? '+': '-').'1 year');
  936. $week_diff += (date_iso_weeks_in_year($date1) * $sign);
  937. }
  938. return $week_diff;
  939. }
  940. }
  941. return NULL;
  942. }
  943. }
  944. /**
  945. * Determines if the date element needs to be processed.
  946. *
  947. * Helper function to see if date element has been hidden by FAPI to see if it
  948. * needs to be processed or just pass the value through. This is needed since
  949. * normal date processing explands the date element into parts and then
  950. * reconstructs it, which is not needed or desirable if the field is hidden.
  951. *
  952. * @param array $element
  953. * The date element to check.
  954. *
  955. * @return bool
  956. * TRUE if the element is effectively hidden, FALSE otherwise.
  957. */
  958. function date_hidden_element($element) {
  959. // @TODO What else needs to be tested to see if dates are hidden or disabled?
  960. if ((isset($element['#access']) && empty($element['#access']))
  961. || !empty($element['#programmed'])
  962. || in_array($element['#type'], array('hidden', 'value'))) {
  963. return TRUE;
  964. }
  965. return FALSE;
  966. }
  967. /**
  968. * Helper function for getting the format string for a date type.
  969. *
  970. * @param string $type
  971. * A date type format name.
  972. *
  973. * @return string
  974. * A date type format, like 'Y-m-d H:i:s'.
  975. */
  976. function date_type_format($type) {
  977. switch ($type) {
  978. case DATE_ISO:
  979. return DATE_FORMAT_ISO;
  980. case DATE_UNIX:
  981. return DATE_FORMAT_UNIX;
  982. case DATE_DATETIME:
  983. return DATE_FORMAT_DATETIME;
  984. case DATE_ICAL:
  985. return DATE_FORMAT_ICAL;
  986. }
  987. }
  988. /**
  989. * Constructs an untranslated array of month names.
  990. *
  991. * Needed for CSS, translation functions, strtotime(), and other places
  992. * that use the English versions of these words.
  993. *
  994. * @return array
  995. * An array of month names.
  996. */
  997. function date_month_names_untranslated() {
  998. static $month_names;
  999. if (empty($month_names)) {
  1000. $month_names = array(
  1001. 1 => 'January',
  1002. 2 => 'February',
  1003. 3 => 'March',
  1004. 4 => 'April',
  1005. 5 => 'May',
  1006. 6 => 'June',
  1007. 7 => 'July',
  1008. 8 => 'August',
  1009. 9 => 'September',
  1010. 10 => 'October',
  1011. 11 => 'November',
  1012. 12 => 'December',
  1013. );
  1014. }
  1015. return $month_names;
  1016. }
  1017. /**
  1018. * Returns a translated array of month names.
  1019. *
  1020. * @param bool $required
  1021. * (optional) If FALSE, the returned array will include a blank value.
  1022. * Defaults to FALSE.
  1023. *
  1024. * @return array
  1025. * An array of month names.
  1026. */
  1027. function date_month_names($required = FALSE) {
  1028. $month_names = array();
  1029. foreach (date_month_names_untranslated() as $key => $month) {
  1030. $month_names[$key] = t($month, array(), array('context' => 'Long month name'));
  1031. }
  1032. $none = array('' => '');
  1033. return !$required ? $none + $month_names : $month_names;
  1034. }
  1035. /**
  1036. * Constructs a translated array of month name abbreviations
  1037. *
  1038. * @param bool $required
  1039. * (optional) If FALSE, the returned array will include a blank value.
  1040. * Defaults to FALSE.
  1041. * @param int $length
  1042. * (optional) The length of the abbreviation. Defaults to 3.
  1043. *
  1044. * @return array
  1045. * An array of month abbreviations.
  1046. */
  1047. function date_month_names_abbr($required = FALSE, $length = 3) {
  1048. $month_names = array();
  1049. foreach (date_month_names_untranslated() as $key => $month) {
  1050. if ($length == 3) {
  1051. $month_names[$key] = t(substr($month, 0, $length), array());
  1052. }
  1053. else {
  1054. $month_names[$key] = t(substr($month, 0, $length), array(), array('context' => 'month_abbr'));
  1055. }
  1056. }
  1057. $none = array('' => '');
  1058. return !$required ? $none + $month_names : $month_names;
  1059. }
  1060. /**
  1061. * Constructs an untranslated array of week days.
  1062. *
  1063. * Needed for CSS, translation functions, strtotime(), and other places
  1064. * that use the English versions of these words.
  1065. *
  1066. * @param bool $refresh
  1067. * (optional) Whether to refresh the list. Defaults to TRUE.
  1068. *
  1069. * @return array
  1070. * An array of week day names
  1071. */
  1072. function date_week_days_untranslated($refresh = TRUE) {
  1073. static $weekdays;
  1074. if ($refresh || empty($weekdays)) {
  1075. $weekdays = array(
  1076. 'Sunday',
  1077. 'Monday',
  1078. 'Tuesday',
  1079. 'Wednesday',
  1080. 'Thursday',
  1081. 'Friday',
  1082. 'Saturday',
  1083. );
  1084. }
  1085. return $weekdays;
  1086. }
  1087. /**
  1088. * Returns a translated array of week names.
  1089. *
  1090. * @param bool $required
  1091. * (optional) If FALSE, the returned array will include a blank value.
  1092. * Defaults to FALSE.
  1093. *
  1094. * @return array
  1095. * An array of week day names
  1096. */
  1097. function date_week_days($required = FALSE, $refresh = TRUE) {
  1098. $weekdays = array();
  1099. foreach (date_week_days_untranslated() as $key => $day) {
  1100. $weekdays[$key] = t($day, array(), array('context' => ''));
  1101. }
  1102. $none = array('' => '');
  1103. return !$required ? $none + $weekdays : $weekdays;
  1104. }
  1105. /**
  1106. * Constructs a translated array of week day abbreviations.
  1107. *
  1108. * @param bool $required
  1109. * (optional) If FALSE, the returned array will include a blank value.
  1110. * Defaults to FALSE.
  1111. * @param bool $refresh
  1112. * (optional) Whether to refresh the list. Defaults to TRUE.
  1113. * @param int $length
  1114. * (optional) The length of the abbreviation. Defaults to 3.
  1115. *
  1116. * @return array
  1117. * An array of week day abbreviations
  1118. */
  1119. function date_week_days_abbr($required = FALSE, $refresh = TRUE, $length = 3) {
  1120. $weekdays = array();
  1121. switch ($length) {
  1122. case 1:
  1123. $context = 'day_abbr1';
  1124. break;
  1125. case 2:
  1126. $context = 'day_abbr2';
  1127. break;
  1128. default:
  1129. $context = '';
  1130. break;
  1131. }
  1132. foreach (date_week_days_untranslated() as $key => $day) {
  1133. $weekdays[$key] = t(substr($day, 0, $length), array(), array('context' => $context));
  1134. }
  1135. $none = array('' => '');
  1136. return !$required ? $none + $weekdays : $weekdays;
  1137. }
  1138. /**
  1139. * Reorders weekdays to match the first day of the week.
  1140. *
  1141. * @param array $weekdays
  1142. * An array of weekdays.
  1143. *
  1144. * @return array
  1145. * An array of weekdays reordered to match the first day of the week.
  1146. */
  1147. function date_week_days_ordered($weekdays) {
  1148. $first_day = variable_get('date_first_day', 0);
  1149. if ($first_day > 0) {
  1150. for ($i = 1; $i <= $first_day; $i++) {
  1151. $last = array_shift($weekdays);
  1152. array_push($weekdays, $last);
  1153. }
  1154. }
  1155. return $weekdays;
  1156. }
  1157. /**
  1158. * Constructs an array of years.
  1159. *
  1160. * @param int $min
  1161. * The minimum year in the array.
  1162. * @param int $max
  1163. * The maximum year in the array.
  1164. * @param bool $required
  1165. * (optional) If FALSE, the returned array will include a blank value.
  1166. * Defaults to FALSE.
  1167. *
  1168. * @return array
  1169. * An array of years in the selected range.
  1170. */
  1171. function date_years($min = 0, $max = 0, $required = FALSE) {
  1172. // Ensure $min and $max are valid values.
  1173. if (empty($min)) {
  1174. $min = intval(date('Y', REQUEST_TIME) - 3);
  1175. }
  1176. if (empty($max)) {
  1177. $max = intval(date('Y', REQUEST_TIME) + 3);
  1178. }
  1179. $none = array(0 => '');
  1180. return !$required ? $none + drupal_map_assoc(range($min, $max)) : drupal_map_assoc(range($min, $max));
  1181. }
  1182. /**
  1183. * Constructs an array of days in a month.
  1184. *
  1185. * @param bool $required
  1186. * (optional) If FALSE, the returned array will include a blank value.
  1187. * Defaults to FALSE.
  1188. * @param int $month
  1189. * (optional) The month in which to find the number of days.
  1190. * @param int $year
  1191. * (optional) The year in which to find the number of days.
  1192. *
  1193. * @return array
  1194. * An array of days for the selected month.
  1195. */
  1196. function date_days($required = FALSE, $month = NULL, $year = NULL) {
  1197. // If we have a month and year, find the right last day of the month.
  1198. if (!empty($month) && !empty($year)) {
  1199. $date = new DateObject($year . '-' . $month . '-01 00:00:00', 'UTC');
  1200. $max = $date->format('t');
  1201. }
  1202. // If there is no month and year given, default to 31.
  1203. if (empty($max)) {
  1204. $max = 31;
  1205. }
  1206. $none = array(0 => '');
  1207. return !$required ? $none + drupal_map_assoc(range(1, $max)) : drupal_map_assoc(range(1, $max));
  1208. }
  1209. /**
  1210. * Constructs an array of hours.
  1211. *
  1212. * @param string $format
  1213. * A date format string.
  1214. * @param bool $required
  1215. * (optional) If FALSE, the returned array will include a blank value.
  1216. * Defaults to FALSE.
  1217. *
  1218. * @return array
  1219. * An array of hours in the selected format.
  1220. */
  1221. function date_hours($format = 'H', $required = FALSE) {
  1222. $hours = array();
  1223. if ($format == 'h' || $format == 'g') {
  1224. $min = 1;
  1225. $max = 12;
  1226. }
  1227. else {
  1228. $min = 0;
  1229. $max = 23;
  1230. }
  1231. for ($i = $min; $i <= $max; $i++) {
  1232. $hours[$i] = $i < 10 && ($format == 'H' || $format == 'h') ? "0$i" : $i;
  1233. }
  1234. $none = array('' => '');
  1235. return !$required ? $none + $hours : $hours;
  1236. }
  1237. /**
  1238. * Constructs an array of minutes.
  1239. *
  1240. * @param string $format
  1241. * A date format string.
  1242. * @param bool $required
  1243. * (optional) If FALSE, the returned array will include a blank value.
  1244. * Defaults to FALSE.
  1245. *
  1246. * @return array
  1247. * An array of minutes in the selected format.
  1248. */
  1249. function date_minutes($format = 'i', $required = FALSE, $increment = 1) {
  1250. $minutes = array();
  1251. // Ensure $increment has a value so we don't loop endlessly.
  1252. if (empty($increment)) {
  1253. $increment = 1;
  1254. }
  1255. for ($i = 0; $i < 60; $i += $increment) {
  1256. $minutes[$i] = $i < 10 && $format == 'i' ? "0$i" : $i;
  1257. }
  1258. $none = array('' => '');
  1259. return !$required ? $none + $minutes : $minutes;
  1260. }
  1261. /**
  1262. * Constructs an array of seconds.
  1263. *
  1264. * @param string $format
  1265. * A date format string.
  1266. * @param bool $required
  1267. * (optional) If FALSE, the returned array will include a blank value.
  1268. * Defaults to FALSE.
  1269. *
  1270. * @return array
  1271. * An array of seconds in the selected format.
  1272. */
  1273. function date_seconds($format = 's', $required = FALSE, $increment = 1) {
  1274. $seconds = array();
  1275. // Ensure $increment has a value so we don't loop endlessly.
  1276. if (empty($increment)) {
  1277. $increment = 1;
  1278. }
  1279. for ($i = 0; $i < 60; $i += $increment) {
  1280. $seconds[$i] = $i < 10 && $format == 's' ? "0$i" : $i;
  1281. }
  1282. $none = array('' => '');
  1283. return !$required ? $none + $seconds : $seconds;
  1284. }
  1285. /**
  1286. * Constructs an array of AM and PM options.
  1287. *
  1288. * @param bool $required
  1289. * (optional) If FALSE, the returned array will include a blank value.
  1290. * Defaults to FALSE.
  1291. *
  1292. * @return array
  1293. * An array of AM and PM options.
  1294. */
  1295. function date_ampm($required = FALSE) {
  1296. $none = array('' => '');
  1297. $ampm = array(
  1298. 'am' => t('am', array(), array('context' => 'ampm')),
  1299. 'pm' => t('pm', array(), array('context' => 'ampm')),
  1300. );
  1301. return !$required ? $none + $ampm : $ampm;
  1302. }
  1303. /**
  1304. * Constructs an array of regex replacement strings for date format elements.
  1305. *
  1306. * @param bool $strict
  1307. * Whether or not to force 2 digits for elements that sometimes allow either
  1308. * 1 or 2 digits.
  1309. *
  1310. * @return array
  1311. * An array of date() format letters and their regex equivalents.
  1312. */
  1313. function date_format_patterns($strict = FALSE) {
  1314. return array(
  1315. 'd' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1316. 'm' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1317. 'h' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1318. 'H' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1319. 'i' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1320. 's' => '\d{' . ($strict ? '2' : '1,2') . '}',
  1321. 'j' => '\d{1,2}',
  1322. 'N' => '\d',
  1323. 'S' => '\w{2}',
  1324. 'w' => '\d',
  1325. 'z' => '\d{1,3}',
  1326. 'W' => '\d{1,2}',
  1327. 'n' => '\d{1,2}',
  1328. 't' => '\d{2}',
  1329. 'L' => '\d',
  1330. 'o' => '\d{4}',
  1331. 'Y' => '-?\d{1,6}',
  1332. 'y' => '\d{2}',
  1333. 'B' => '\d{3}',
  1334. 'g' => '\d{1,2}',
  1335. 'G' => '\d{1,2}',
  1336. 'e' => '\w*',
  1337. 'I' => '\d',
  1338. 'T' => '\w*',
  1339. 'U' => '\d*',
  1340. 'z' => '[+-]?\d*',
  1341. 'O' => '[+-]?\d{4}',
  1342. // Using S instead of w and 3 as well as 4 to pick up non-ASCII chars like
  1343. // German umlaut. Per http://drupal.org/node/1101284, we may need as little
  1344. // as 2 and as many as 5 characters in some languages.
  1345. 'D' => '\S{2,5}',
  1346. 'l' => '\S*',
  1347. 'M' => '\S{2,5}',
  1348. 'F' => '\S*',
  1349. 'P' => '[+-]?\d{2}\:\d{2}',
  1350. 'O' => '[+-]\d{4}',
  1351. 'c' => '(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})([+-]?\d{2}\:\d{2})',
  1352. 'r' => '(\w{3}), (\d{2})\s(\w{3})\s(\d{2,4})\s(\d{2}):(\d{2}):(\d{2})([+-]?\d{4})?',
  1353. );
  1354. }
  1355. /**
  1356. * Constructs an array of granularity options and their labels.
  1357. *
  1358. * @return array
  1359. * An array of translated date parts, keyed by their machine name.
  1360. */
  1361. function date_granularity_names() {
  1362. return array(
  1363. 'year' => t('Year', array(), array('context' => 'datetime')),
  1364. 'month' => t('Month', array(), array('context' => 'datetime')),
  1365. 'day' => t('Day', array(), array('context' => 'datetime')),
  1366. 'hour' => t('Hour', array(), array('context' => 'datetime')),
  1367. 'minute' => t('Minute', array(), array('context' => 'datetime')),
  1368. 'second' => t('Second', array(), array('context' => 'datetime')),
  1369. );
  1370. }
  1371. /**
  1372. * Sorts a granularity array.
  1373. *
  1374. * @param array $granularity
  1375. * An array of date parts.
  1376. */
  1377. function date_granularity_sorted($granularity) {
  1378. return array_intersect(array('year', 'month', 'day', 'hour', 'minute', 'second'), $granularity);
  1379. }
  1380. /**
  1381. * Constructs an array of granularity based on a given precision.
  1382. *
  1383. * @param string $precision
  1384. * A granularity item.
  1385. *
  1386. * @return array
  1387. * A granularity array containing the given precision and all those above it.
  1388. * For example, passing in 'month' will return array('year', 'month').
  1389. */
  1390. function date_granularity_array_from_precision($precision) {
  1391. $granularity_array = array('year', 'month', 'day', 'hour', 'minute', 'second');
  1392. switch ($precision) {
  1393. case 'year':
  1394. return array_slice($granularity_array, -6, 1);
  1395. case 'month':
  1396. return array_slice($granularity_array, -6, 2);
  1397. case 'day':
  1398. return array_slice($granularity_array, -6, 3);
  1399. case 'hour':
  1400. return array_slice($granularity_array, -6, 4);
  1401. case 'minute':
  1402. return array_slice($granularity_array, -6, 5);
  1403. default:
  1404. return $granularity_array;
  1405. }
  1406. }
  1407. /**
  1408. * Give a granularity array, return the highest precision.
  1409. *
  1410. * @param array $granularity_array
  1411. * An array of date parts.
  1412. *
  1413. * @return string
  1414. * The most precise element in a granularity array.
  1415. */
  1416. function date_granularity_precision($granularity_array) {
  1417. $input = date_granularity_sorted($granularity_array);
  1418. return array_pop($input);
  1419. }
  1420. /**
  1421. * Constructs a valid DATETIME format string for the granularity of an item.
  1422. *
  1423. * @todo This function is no longer used as of
  1424. * http://drupalcode.org/project/date.git/commit/07efbb5.
  1425. */
  1426. function date_granularity_format($granularity) {
  1427. if (is_array($granularity)) {
  1428. $granularity = date_granularity_precision($granularity);
  1429. }
  1430. $format = 'Y-m-d H:i:s';
  1431. switch ($granularity) {
  1432. case 'year':
  1433. return substr($format, 0, 1);
  1434. case 'month':
  1435. return substr($format, 0, 3);
  1436. case 'day':
  1437. return substr($format, 0, 5);
  1438. case 'hour';
  1439. return substr($format, 0, 7);
  1440. case 'minute':
  1441. return substr($format, 0, 9);
  1442. default:
  1443. return $format;
  1444. }
  1445. }
  1446. /**
  1447. * Returns a translated array of timezone names.
  1448. *
  1449. * Cache the untranslated array, make the translated array a static variable.
  1450. *
  1451. * @param bool $required
  1452. * (optional) If FALSE, the returned array will include a blank value.
  1453. * Defaults to FALSE.
  1454. * @param bool $refresh
  1455. * (optional) Whether to refresh the list. Defaults to TRUE.
  1456. *
  1457. * @return array
  1458. * An array of timezone names.
  1459. */
  1460. function date_timezone_names($required = FALSE, $refresh = FALSE) {
  1461. static $zonenames;
  1462. if (empty($zonenames) || $refresh) {
  1463. $cached = cache_get('date_timezone_identifiers_list');
  1464. $zonenames = !empty($cached) ? $cached->data : array();
  1465. if ($refresh || empty($cached) || empty($zonenames)) {
  1466. $data = timezone_identifiers_list();
  1467. asort($data);
  1468. foreach ($data as $delta => $zone) {
  1469. // Because many timezones exist in PHP only for backward compatibility
  1470. // reasons and should not be used, the list is filtered by a regular
  1471. // expression.
  1472. if (preg_match('!^((Africa|America|Antarctica|Arctic|Asia|Atlantic|Australia|Europe|Indian|Pacific)/|UTC$)!', $zone)) {
  1473. $zonenames[$zone] = $zone;
  1474. }
  1475. }
  1476. if (!empty($zonenames)) {
  1477. cache_set('date_timezone_identifiers_list', $zonenames);
  1478. }
  1479. }
  1480. foreach ($zonenames as $zone) {
  1481. $zonenames[$zone] = t('!timezone', array('!timezone' => t($zone)));
  1482. }
  1483. }
  1484. $none = array('' => '');
  1485. return !$required ? $none + $zonenames : $zonenames;
  1486. }
  1487. /**
  1488. * Returns an array of system-allowed timezone abbreviations.
  1489. *
  1490. * Cache an array of just the abbreviation names because the whole
  1491. * timezone_abbreviations_list() is huge, so we don't want to retrieve it more
  1492. * than necessary.
  1493. *
  1494. * @param bool $refresh
  1495. * (optional) Whether to refresh the list. Defaults to TRUE.
  1496. *
  1497. * @return array
  1498. * An array of allowed timezone abbreviations.
  1499. */
  1500. function date_timezone_abbr($refresh = FALSE) {
  1501. $cached = cache_get('date_timezone_abbreviations');
  1502. $data = isset($cached->data) ? $cached->data : array();
  1503. if (empty($data) || $refresh) {
  1504. $data = array_keys(timezone_abbreviations_list());
  1505. cache_set('date_timezone_abbreviations', $data);
  1506. }
  1507. return $data;
  1508. }
  1509. /**
  1510. * Formats a date, using a date type or a custom date format string.
  1511. *
  1512. * Reworked from Drupal's format_date function to handle pre-1970 and
  1513. * post-2038 dates and accept a date object instead of a timestamp as input.
  1514. * Translates formatted date results, unlike PHP function date_format().
  1515. * Should only be used for display, not input, because it can't be parsed.
  1516. *
  1517. * @param object $date
  1518. * A date object.
  1519. * @param string $type
  1520. * (optional) The date format to use. Can be 'small', 'medium' or 'large' for
  1521. * the preconfigured date formats. If 'custom' is specified, then $format is
  1522. * required as well. Defaults to 'medium'.
  1523. * @param string $format
  1524. * (optional) A PHP date format string as required by date(). A backslash
  1525. * should be used before a character to avoid interpreting the character as
  1526. * part of a date format. Defaults to an empty string.
  1527. * @param string $langcode
  1528. * (optional) Language code to translate to. Defaults to NULL.
  1529. *
  1530. * @return string
  1531. * A translated date string in the requested format.
  1532. *
  1533. * @see format_date()
  1534. */
  1535. function date_format_date($date, $type = 'medium', $format = '', $langcode = NULL) {
  1536. if (empty($date)) {
  1537. return '';
  1538. }
  1539. if ($type != 'custom') {
  1540. $format = variable_get('date_format_' . $type);
  1541. }
  1542. if ($type != 'custom' && empty($format)) {
  1543. $format = variable_get('date_format_medium', 'D, m/d/Y - H:i');
  1544. }
  1545. $format = date_limit_format($format, $date->granularity);
  1546. $max = strlen($format);
  1547. $datestring = '';
  1548. for ($i = 0; $i < $max; $i++) {
  1549. $c = $format[$i];
  1550. switch ($c) {
  1551. case 'l':
  1552. $datestring .= t($date->format('l'), array(), array('context' => '', 'langcode' => $langcode));
  1553. break;
  1554. case 'D':
  1555. $datestring .= t($date->format('D'), array(), array('context' => '', 'langcode' => $langcode));
  1556. break;
  1557. case 'F':
  1558. $datestring .= t($date->format('F'), array(), array('context' => 'Long month name', 'langcode' => $langcode));
  1559. break;
  1560. case 'M':
  1561. $datestring .= t($date->format('M'), array(), array('langcode' => $langcode));
  1562. break;
  1563. case 'A':
  1564. case 'a':
  1565. $datestring .= t($date->format($c), array(), array('context' => 'ampm', 'langcode' => $langcode));
  1566. break;
  1567. // The timezone name translations can use t().
  1568. case 'e':
  1569. case 'T':
  1570. $datestring .= t($date->format($c));
  1571. break;
  1572. // Remaining date parts need no translation.
  1573. case 'O':
  1574. $datestring .= sprintf('%s%02d%02d', (date_offset_get($date) < 0 ? '-' : '+'), abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
  1575. break;
  1576. case 'P':
  1577. $datestring .= sprintf('%s%02d:%02d', (date_offset_get($date) < 0 ? '-' : '+'), abs(date_offset_get($date) / 3600), abs(date_offset_get($date) % 3600) / 60);
  1578. break;
  1579. case 'Z':
  1580. $datestring .= date_offset_get($date);
  1581. break;
  1582. case '\\':
  1583. $datestring .= $format[++$i];
  1584. break;
  1585. case 'r':
  1586. $datestring .= date_format_date($date, 'custom', 'D, d M Y H:i:s O', $langcode);
  1587. break;
  1588. default:
  1589. if (strpos('BdcgGhHiIjLmnNosStTuUwWYyz', $c) !== FALSE) {
  1590. $datestring .= $date->format($c);
  1591. }
  1592. else {
  1593. $datestring .= $c;
  1594. }
  1595. }
  1596. }
  1597. return $datestring;
  1598. }
  1599. /**
  1600. * Formats a time interval with granularity, including past and future context.
  1601. *
  1602. * @param object $date
  1603. * The current date object.
  1604. * @param int $granularity
  1605. * (optional) Number of units to display in the string. Defaults to 2.
  1606. *
  1607. * @return string
  1608. * A translated string representation of the interval.
  1609. *
  1610. * @see format_interval()
  1611. */
  1612. function date_format_interval($date, $granularity = 2, $display_ago = TRUE) {
  1613. // If no date is sent, then return nothing.
  1614. if (empty($date)) {
  1615. return NULL;
  1616. }
  1617. $interval = REQUEST_TIME - $date->format('U');
  1618. if ($interval > 0) {
  1619. return $display_ago ? t('!time ago', array('!time' => format_interval($interval, $granularity))) :
  1620. t('!time', array('!time' => format_interval($interval, $granularity)));
  1621. }
  1622. else {
  1623. return format_interval(abs($interval), $granularity);
  1624. }
  1625. }
  1626. /**
  1627. * A date object for the current time.
  1628. *
  1629. * @param object $timezone
  1630. * (optional) Optionally force time to a specific timezone, defaults to user
  1631. * timezone, if set, otherwise site timezone. Defaults to NULL.
  1632. *
  1633. * @param boolean $reset [optional]
  1634. * Static cache reset
  1635. *
  1636. * @return object
  1637. * The current time as a date object.
  1638. */
  1639. function date_now($timezone = NULL, $reset = FALSE) {
  1640. $now = &drupal_static(__FUNCTION__);
  1641. if (!isset($now) || $reset) {
  1642. $now = new DateObject('now', $timezone);
  1643. }
  1644. return $now;
  1645. }
  1646. /**
  1647. * Determines if a timezone string is valid.
  1648. *
  1649. * @param string $timezone
  1650. * A potentially invalid timezone string.
  1651. *
  1652. * @return bool
  1653. * TRUE if the timezone is valid, FALSE otherwise.
  1654. */
  1655. function date_timezone_is_valid($timezone) {
  1656. static $timezone_names;
  1657. if (empty($timezone_names)) {
  1658. $timezone_names = array_keys(date_timezone_names(TRUE));
  1659. }
  1660. return in_array($timezone, $timezone_names);
  1661. }
  1662. /**
  1663. * Returns a timezone name to use as a default.
  1664. *
  1665. * @param bool $check_user
  1666. * (optional) Whether or not to check for a user-configured timezone.
  1667. * Defaults to TRUE.
  1668. *
  1669. * @return string
  1670. * The default timezone for a user, if available, otherwise the site.
  1671. */
  1672. function date_default_timezone($check_user = TRUE) {
  1673. global $user;
  1674. if ($check_user && variable_get('configurable_timezones', 1) && !empty($user->timezone)) {
  1675. return $user->timezone;
  1676. }
  1677. else {
  1678. $default = variable_get('date_default_timezone', '');
  1679. return empty($default) ? 'UTC' : $default;
  1680. }
  1681. }
  1682. /**
  1683. * Returns a timezone object for the default timezone.
  1684. *
  1685. * @param bool $check_user
  1686. * (optional) Whether or not to check for a user-configured timezone.
  1687. * Defaults to TRUE.
  1688. *
  1689. * @return object
  1690. * The default timezone for a user, if available, otherwise the site.
  1691. */
  1692. function date_default_timezone_object($check_user = TRUE) {
  1693. return timezone_open(date_default_timezone($check_user));
  1694. }
  1695. /**
  1696. * Identifies the number of days in a month for a date.
  1697. */
  1698. function date_days_in_month($year, $month) {
  1699. // Pick a day in the middle of the month to avoid timezone shifts.
  1700. $datetime = date_pad($year, 4) . '-' . date_pad($month) . '-15 00:00:00';
  1701. $date = new DateObject($datetime);
  1702. return $date->format('t');
  1703. }
  1704. /**
  1705. * Identifies the number of days in a year for a date.
  1706. *
  1707. * @param mixed $date
  1708. * (optional) The current date object, or a date string. Defaults to NULL.
  1709. *
  1710. * @return integer
  1711. * The number of days in the year.
  1712. */
  1713. function date_days_in_year($date = NULL) {
  1714. if (empty($date)) {
  1715. $date = date_now();
  1716. }
  1717. elseif (!is_object($date)) {
  1718. $date = new DateObject($date);
  1719. }
  1720. if (is_object($date)) {
  1721. if ($date->format('L')) {
  1722. return 366;
  1723. }
  1724. else {
  1725. return 365;
  1726. }
  1727. }
  1728. return NULL;
  1729. }
  1730. /**
  1731. * Identifies the number of ISO weeks in a year for a date.
  1732. *
  1733. * December 28 is always in the last ISO week of the year.
  1734. *
  1735. * @param mixed $date
  1736. * (optional) The current date object, or a date string. Defaults to NULL.
  1737. *
  1738. * @return integer
  1739. * The number of ISO weeks in a year.
  1740. */
  1741. function date_iso_weeks_in_year($date = NULL) {
  1742. if (empty($date)) {
  1743. $date = date_now();
  1744. }
  1745. elseif (!is_object($date)) {
  1746. $date = new DateObject($date);
  1747. }
  1748. if (is_object($date)) {
  1749. date_date_set($date, $date->format('Y'), 12, 28);
  1750. return $date->format('W');
  1751. }
  1752. return NULL;
  1753. }
  1754. /**
  1755. * Returns day of week for a given date (0 = Sunday).
  1756. *
  1757. * @param mixed $date
  1758. * (optional) A date, default is current local day. Defaults to NULL.
  1759. *
  1760. * @return int
  1761. * The number of the day in the week.
  1762. */
  1763. function date_day_of_week($date = NULL) {
  1764. if (empty($date)) {
  1765. $date = date_now();
  1766. }
  1767. elseif (!is_object($date)) {
  1768. $date = new DateObject($date);
  1769. }
  1770. if (is_object($date)) {
  1771. return $date->format('w');
  1772. }
  1773. return NULL;
  1774. }
  1775. /**
  1776. * Returns translated name of the day of week for a given date.
  1777. *
  1778. * @param mixed $date
  1779. * (optional) A date, default is current local day. Defaults to NULL.
  1780. * @param string $abbr
  1781. * (optional) Whether to return the abbreviated name for that day.
  1782. * Defaults to TRUE.
  1783. *
  1784. * @return string
  1785. * The name of the day in the week for that date.
  1786. */
  1787. function date_day_of_week_name($date = NULL, $abbr = TRUE) {
  1788. if (!is_object($date)) {
  1789. $date = new DateObject($date);
  1790. }
  1791. $dow = date_day_of_week($date);
  1792. $days = $abbr ? date_week_days_abbr() : date_week_days();
  1793. return $days[$dow];
  1794. }
  1795. /**
  1796. * Calculates the start and end dates for a calendar week.
  1797. *
  1798. * The dates are adjusted to use the chosen first day of week for this site.
  1799. *
  1800. * @param int $week
  1801. * The week value.
  1802. * @param int $year
  1803. * The year value.
  1804. *
  1805. * @return array
  1806. * A numeric array containing the start and end dates of a week.
  1807. */
  1808. function date_week_range($week, $year) {
  1809. if (variable_get('date_api_use_iso8601', FALSE)) {
  1810. return date_iso_week_range($week, $year);
  1811. }
  1812. $min_date = new DateObject($year . '-01-01 00:00:00');
  1813. $min_date->setTimezone(date_default_timezone_object());
  1814. // Move to the right week.
  1815. date_modify($min_date, '+' . strval(7 * ($week - 1)) . ' days');
  1816. // Move backwards to the first day of the week.
  1817. $first_day = variable_get('date_first_day', 0);
  1818. $day_wday = date_format($min_date, 'w');
  1819. date_modify($min_date, '-' . strval((7 + $day_wday - $first_day) % 7) . ' days');
  1820. // Move forwards to the last day of the week.
  1821. $max_date = clone($min_date);
  1822. date_modify($max_date, '+7 days');
  1823. if (date_format($min_date, 'Y') != $year) {
  1824. $min_date = new DateObject($year . '-01-01 00:00:00');
  1825. }
  1826. return array($min_date, $max_date);
  1827. }
  1828. /**
  1829. * Calculates the start and end dates for an ISO week.
  1830. *
  1831. * @param int $week
  1832. * The week value.
  1833. * @param int $year
  1834. * The year value.
  1835. *
  1836. * @return array
  1837. * A numeric array containing the start and end dates of an ISO week.
  1838. */
  1839. function date_iso_week_range($week, $year) {
  1840. // Get to the last ISO week of the previous year.
  1841. $min_date = new DateObject(($year - 1) . '-12-28 00:00:00');
  1842. date_timezone_set($min_date, date_default_timezone_object());
  1843. // Find the first day of the first ISO week in the year.
  1844. date_modify($min_date, '+1 Monday');
  1845. // Jump ahead to the desired week for the beginning of the week range.
  1846. if ($week > 1) {
  1847. date_modify($min_date, '+ ' . ($week - 1) . ' weeks');
  1848. }
  1849. // Move forwards to the last day of the week.
  1850. $max_date = clone($min_date);
  1851. date_modify($max_date, '+7 days');
  1852. return array($min_date, $max_date);
  1853. }
  1854. /**
  1855. * The number of calendar weeks in a year.
  1856. *
  1857. * PHP week functions return the ISO week, not the calendar week.
  1858. *
  1859. * @param int $year
  1860. * A year value.
  1861. *
  1862. * @return int
  1863. * Number of calendar weeks in selected year.
  1864. */
  1865. function date_weeks_in_year($year) {
  1866. $date = new DateObject(($year + 1) . '-01-01 12:00:00', 'UTC');
  1867. date_modify($date, '-1 day');
  1868. return date_week($date->format('Y-m-d'));
  1869. }
  1870. /**
  1871. * The calendar week number for a date.
  1872. *
  1873. * PHP week functions return the ISO week, not the calendar week.
  1874. *
  1875. * @param string $date
  1876. * A date string in the format Y-m-d.
  1877. *
  1878. * @return int
  1879. * The calendar week number.
  1880. */
  1881. function date_week($date) {
  1882. $date = substr($date, 0, 10);
  1883. $parts = explode('-', $date);
  1884. $date = new DateObject($date . ' 12:00:00', 'UTC');
  1885. // If we are using ISO weeks, this is easy.
  1886. if (variable_get('date_api_use_iso8601', FALSE)) {
  1887. return intval($date->format('W'));
  1888. }
  1889. $year_date = new DateObject($parts[0] . '-01-01 12:00:00', 'UTC');
  1890. $week = intval($date->format('W'));
  1891. $year_week = intval(date_format($year_date, 'W'));
  1892. $date_year = intval($date->format('o'));
  1893. // Remove the leap week if it's present.
  1894. if ($date_year > intval($parts[0])) {
  1895. $last_date = clone($date);
  1896. date_modify($last_date, '-7 days');
  1897. $week = date_format($last_date, 'W') + 1;
  1898. }
  1899. elseif ($date_year < intval($parts[0])) {
  1900. $week = 0;
  1901. }
  1902. if ($year_week != 1) {
  1903. $week++;
  1904. }
  1905. // Convert to ISO-8601 day number, to match weeks calculated above.
  1906. $iso_first_day = 1 + (variable_get('date_first_day', 0) + 6) % 7;
  1907. // If it's before the starting day, it's the previous week.
  1908. if (intval($date->format('N')) < $iso_first_day) {
  1909. $week--;
  1910. }
  1911. // If the year starts before, it's an extra week at the beginning.
  1912. if (intval(date_format($year_date, 'N')) < $iso_first_day) {
  1913. $week++;
  1914. }
  1915. return $week;
  1916. }
  1917. /**
  1918. * Helper function to left pad date parts with zeros.
  1919. *
  1920. * Provided because this is needed so often with dates.
  1921. *
  1922. * @param int $value
  1923. * The value to pad.
  1924. * @param int $size
  1925. * (optional) Total size expected, usually 2 or 4. Defaults to 2.
  1926. *
  1927. * @return string
  1928. * The padded value.
  1929. */
  1930. function date_pad($value, $size = 2) {
  1931. return sprintf("%0" . $size . "d", $value);
  1932. }
  1933. /**
  1934. * Determines if the granularity contains a time portion.
  1935. *
  1936. * @param array $granularity
  1937. * An array of allowed date parts, all others will be removed.
  1938. *
  1939. * @return bool
  1940. * TRUE if the granularity contains a time portion, FALSE otherwise.
  1941. */
  1942. function date_has_time($granularity) {
  1943. if (!is_array($granularity)) {
  1944. $granularity = array();
  1945. }
  1946. return (bool) count(array_intersect($granularity, array('hour', 'minute', 'second')));
  1947. }
  1948. /**
  1949. * Determines if the granularity contains a date portion.
  1950. *
  1951. * @param array $granularity
  1952. * An array of allowed date parts, all others will be removed.
  1953. *
  1954. * @return bool
  1955. * TRUE if the granularity contains a date portion, FALSE otherwise.
  1956. */
  1957. function date_has_date($granularity) {
  1958. if (!is_array($granularity)) {
  1959. $granularity = array();
  1960. }
  1961. return (bool) count(array_intersect($granularity, array('year', 'month', 'day')));
  1962. }
  1963. /**
  1964. * Helper function to get a format for a specific part of a date field.
  1965. *
  1966. * @param string $part
  1967. * The date field part, either 'time' or 'date'.
  1968. * @param string $format
  1969. * A date format string.
  1970. *
  1971. * @return string
  1972. * The date format for the given part.
  1973. */
  1974. function date_part_format($part, $format) {
  1975. switch ($part) {
  1976. case 'date':
  1977. return date_limit_format($format, array('year', 'month', 'day'));
  1978. case 'time':
  1979. return date_limit_format($format, array('hour', 'minute', 'second'));
  1980. default:
  1981. return date_limit_format($format, array($part));
  1982. }
  1983. }
  1984. /**
  1985. * Limits a date format to include only elements from a given granularity array.
  1986. *
  1987. * Example:
  1988. * date_limit_format('F j, Y - H:i', array('year', 'month', 'day'));
  1989. * returns 'F j, Y'
  1990. *
  1991. * @param string $format
  1992. * A date format string.
  1993. * @param array $granularity
  1994. * An array of allowed date parts, all others will be removed.
  1995. *
  1996. * @return string
  1997. * The format string with all other elements removed.
  1998. */
  1999. function date_limit_format($format, $granularity) {
  2000. // Use the advanced drupal_static() pattern to improve performance.
  2001. static $drupal_static_fast;
  2002. if (!isset($drupal_static_fast)) {
  2003. $drupal_static_fast['formats'] = &drupal_static(__FUNCTION__);
  2004. }
  2005. $formats = &$drupal_static_fast['formats'];
  2006. $format_granularity_cid = $format .'|'. implode(',', $granularity);
  2007. if (isset($formats[$format_granularity_cid])) {
  2008. return $formats[$format_granularity_cid];
  2009. }
  2010. // If punctuation has been escaped, remove the escaping. Done using strtr()
  2011. // because it is easier than getting the escape character extracted using
  2012. // preg_replace().
  2013. $replace = array(
  2014. '\-' => '-',
  2015. '\:' => ':',
  2016. "\'" => "'",
  2017. '\. ' => ' . ',
  2018. '\,' => ',',
  2019. );
  2020. $format = strtr($format, $replace);
  2021. // Get the 'T' out of ISO date formats that don't have both date and time.
  2022. if (!date_has_time($granularity) || !date_has_date($granularity)) {
  2023. $format = str_replace('\T', ' ', $format);
  2024. $format = str_replace('T', ' ', $format);
  2025. }
  2026. $regex = array();
  2027. if (!date_has_time($granularity)) {
  2028. $regex[] = '((?<!\\\\)[a|A])';
  2029. }
  2030. // Create regular expressions to remove selected values from string.
  2031. // Use (?<!\\\\) to keep escaped letters from being removed.
  2032. foreach (date_nongranularity($granularity) as $element) {
  2033. switch ($element) {
  2034. case 'year':
  2035. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[Yy])';
  2036. break;
  2037. case 'day':
  2038. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[l|D|d|dS|j|jS|N|w|W|z]{1,2})';
  2039. break;
  2040. case 'month':
  2041. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[FMmn])';
  2042. break;
  2043. case 'hour':
  2044. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[HhGg])';
  2045. break;
  2046. case 'minute':
  2047. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[i])';
  2048. break;
  2049. case 'second':
  2050. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[s])';
  2051. break;
  2052. case 'timezone':
  2053. $regex[] = '([\-/\.,:]?\s?(?<!\\\\)[TOZPe])';
  2054. break;
  2055. }
  2056. }
  2057. // Remove empty parentheses, brackets, pipes.
  2058. $regex[] = '(\(\))';
  2059. $regex[] = '(\[\])';
  2060. $regex[] = '(\|\|)';
  2061. // Remove selected values from string.
  2062. $format = trim(preg_replace($regex, array(), $format));
  2063. // Remove orphaned punctuation at the beginning of the string.
  2064. $format = preg_replace('`^([\-/\.,:\'])`', '', $format);
  2065. // Remove orphaned punctuation at the end of the string.
  2066. $format = preg_replace('([\-/,:\']$)', '', $format);
  2067. $format = preg_replace('(\\$)', '', $format);
  2068. // Trim any whitespace from the result.
  2069. $format = trim($format);
  2070. // After removing the non-desired parts of the format, test if the only things
  2071. // left are escaped, non-date, characters. If so, return nothing.
  2072. // Using S instead of w to pick up non-ASCII characters.
  2073. $test = trim(preg_replace('(\\\\\S{1,3})u', '', $format));
  2074. if (empty($test)) {
  2075. $format = '';
  2076. }
  2077. // Store the return value in the static array for performance.
  2078. $formats[$format_granularity_cid] = $format;
  2079. return $format;
  2080. }
  2081. /**
  2082. * Converts a format to an ordered array of granularity parts.
  2083. *
  2084. * Example:
  2085. * date_format_order('m/d/Y H:i')
  2086. * returns
  2087. * array(
  2088. * 0 => 'month',
  2089. * 1 => 'day',
  2090. * 2 => 'year',
  2091. * 3 => 'hour',
  2092. * 4 => 'minute',
  2093. * );
  2094. *
  2095. * @param string $format
  2096. * A date format string.
  2097. *
  2098. * @return array
  2099. * An array of ordered granularity elements from the given format string.
  2100. */
  2101. function date_format_order($format) {
  2102. $order = array();
  2103. if (empty($format)) {
  2104. return $order;
  2105. }
  2106. $max = strlen($format);
  2107. for ($i = 0; $i <= $max; $i++) {
  2108. if (!isset($format[$i])) {
  2109. break;
  2110. }
  2111. switch ($format[$i]) {
  2112. case 'd':
  2113. case 'j':
  2114. $order[] = 'day';
  2115. break;
  2116. case 'F':
  2117. case 'M':
  2118. case 'm':
  2119. case 'n':
  2120. $order[] = 'month';
  2121. break;
  2122. case 'Y':
  2123. case 'y':
  2124. $order[] = 'year';
  2125. break;
  2126. case 'g':
  2127. case 'G':
  2128. case 'h':
  2129. case 'H':
  2130. $order[] = 'hour';
  2131. break;
  2132. case 'i':
  2133. $order[] = 'minute';
  2134. break;
  2135. case 's':
  2136. $order[] = 'second';
  2137. break;
  2138. }
  2139. }
  2140. return $order;
  2141. }
  2142. /**
  2143. * Strips out unwanted granularity elements.
  2144. *
  2145. * @param array $granularity
  2146. * An array like ('year', 'month', 'day', 'hour', 'minute', 'second');
  2147. *
  2148. * @return array
  2149. * A reduced set of granularitiy elements.
  2150. */
  2151. function date_nongranularity($granularity) {
  2152. return array_diff(array('year', 'month', 'day', 'hour', 'minute', 'second', 'timezone'), (array) $granularity);
  2153. }
  2154. /**
  2155. * Implements hook_element_info().
  2156. */
  2157. function date_api_element_info() {
  2158. module_load_include('inc', 'date_api', 'date_api_elements');
  2159. return _date_api_element_info();
  2160. }
  2161. /**
  2162. * Implements hook_theme().
  2163. */
  2164. function date_api_theme($existing, $type, $theme, $path) {
  2165. $base = array(
  2166. 'file' => 'theme.inc',
  2167. 'path' => "$path/theme",
  2168. );
  2169. return array(
  2170. 'date_nav_title' => $base + array('variables' => array('granularity' => NULL, 'view' => NULL, 'link' => NULL, 'format' => NULL)),
  2171. 'date_timezone' => $base + array('render element' => 'element'),
  2172. 'date_select' => $base + array('render element' => 'element'),
  2173. 'date_text' => $base + array('render element' => 'element'),
  2174. 'date_select_element' => $base + array('render element' => 'element'),
  2175. 'date_textfield_element' => $base + array('render element' => 'element'),
  2176. 'date_part_hour_prefix' => $base + array('render element' => 'element'),
  2177. 'date_part_minsec_prefix' => $base + array('render element' => 'element'),
  2178. 'date_part_label_year' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2179. 'date_part_label_month' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2180. 'date_part_label_day' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2181. 'date_part_label_hour' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2182. 'date_part_label_minute' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2183. 'date_part_label_second' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2184. 'date_part_label_ampm' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2185. 'date_part_label_timezone' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2186. 'date_part_label_date' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2187. 'date_part_label_time' => $base + array('variables' => array('date_part' => NULL, 'element' => NULL)),
  2188. 'date_views_filter_form' => $base + array('template' => 'date-views-filter-form', 'render element' => 'form'),
  2189. 'date_calendar_day' => $base + array('variables' => array('date' => NULL)),
  2190. 'date_time_ago' => $base + array('variables' => array('start_date' => NULL, 'end_date' => NULL, 'interval' => NULL)),
  2191. );
  2192. }
  2193. /**
  2194. * Function to figure out which local timezone applies to a date and select it.
  2195. *
  2196. * @param string $handling
  2197. * The timezone handling.
  2198. * @param string $timezone
  2199. * (optional) A timezone string. Defaults to an empty string.
  2200. *
  2201. * @return string
  2202. * The timezone string.
  2203. */
  2204. function date_get_timezone($handling, $timezone = '') {
  2205. switch ($handling) {
  2206. case 'date':
  2207. $timezone = !empty($timezone) ? $timezone : date_default_timezone();
  2208. break;
  2209. case 'utc':
  2210. $timezone = 'UTC';
  2211. break;
  2212. default:
  2213. $timezone = date_default_timezone();
  2214. }
  2215. return $timezone > '' ? $timezone : date_default_timezone();
  2216. }
  2217. /**
  2218. * Function to figure out which db timezone applies to a date and select it.
  2219. *
  2220. * @param string $handling
  2221. * The timezone handling.
  2222. * @param string $timezone
  2223. * (optional) A timezone string. Defaults to an empty string.
  2224. *
  2225. * @return string
  2226. * The timezone string.
  2227. */
  2228. function date_get_timezone_db($handling, $timezone = '') {
  2229. switch ($handling) {
  2230. case 'none':
  2231. $timezone = date_default_timezone();
  2232. break;
  2233. default:
  2234. $timezone = 'UTC';
  2235. break;
  2236. }
  2237. return $timezone > '' ? $timezone : 'UTC';
  2238. }
  2239. /**
  2240. * Helper function for converting back and forth from '+1' to 'First'.
  2241. */
  2242. function date_order_translated() {
  2243. return array(
  2244. '+1' => t('First', array(), array('context' => 'date_order')),
  2245. '+2' => t('Second', array(), array('context' => 'date_order')),
  2246. '+3' => t('Third', array(), array('context' => 'date_order')),
  2247. '+4' => t('Fourth', array(), array('context' => 'date_order')),
  2248. '+5' => t('Fifth', array(), array('context' => 'date_order')),
  2249. '-1' => t('Last', array(), array('context' => 'date_order_reverse')),
  2250. '-2' => t('Next to last', array(), array('context' => 'date_order_reverse')),
  2251. '-3' => t('Third from last', array(), array('context' => 'date_order_reverse')),
  2252. '-4' => t('Fourth from last', array(), array('context' => 'date_order_reverse')),
  2253. '-5' => t('Fifth from last', array(), array('context' => 'date_order_reverse')),
  2254. );
  2255. }
  2256. /**
  2257. * Creates an array of ordered strings, using English text when possible.
  2258. */
  2259. function date_order() {
  2260. return array(
  2261. '+1' => 'First',
  2262. '+2' => 'Second',
  2263. '+3' => 'Third',
  2264. '+4' => 'Fourth',
  2265. '+5' => 'Fifth',
  2266. '-1' => 'Last',
  2267. '-2' => '-2',
  2268. '-3' => '-3',
  2269. '-4' => '-4',
  2270. '-5' => '-5',
  2271. );
  2272. }
  2273. /**
  2274. * Tests validity of a date range string.
  2275. *
  2276. * @param string $string
  2277. * A min and max year string like '-3:+1'a.
  2278. *
  2279. * @return bool
  2280. * TRUE if the date range is valid, FALSE otherwise.
  2281. */
  2282. function date_range_valid($string) {
  2283. $matches = preg_match('@^(\-[0-9]+|[0-9]{4}):([\+|\-][0-9]+|[0-9]{4})$@', $string);
  2284. return $matches < 1 ? FALSE : TRUE;
  2285. }
  2286. /**
  2287. * Splits a string like -3:+3 or 2001:2010 into an array of min and max years.
  2288. *
  2289. * Center the range around the current year, if any, but expand it far
  2290. * enough so it will pick up the year value in the field in case
  2291. * the value in the field is outside the initial range.
  2292. *
  2293. * @param string $string
  2294. * A min and max year string like '-3:+1'.
  2295. * @param object $date
  2296. * (optional) A date object. Defaults to NULL.
  2297. *
  2298. * @return array
  2299. * A numerically indexed array, containing a minimum and maximum year.
  2300. */
  2301. function date_range_years($string, $date = NULL) {
  2302. $this_year = date_format(date_now(), 'Y');
  2303. list($min_year, $max_year) = explode(':', $string);
  2304. // Valid patterns would be -5:+5, 0:+1, 2008:2010.
  2305. $plus_pattern = '@[\+|\-][0-9]{1,4}@';
  2306. $year_pattern = '@^[0-9]{4}@';
  2307. if (!preg_match($year_pattern, $min_year, $matches)) {
  2308. if (preg_match($plus_pattern, $min_year, $matches)) {
  2309. $min_year = $this_year + $matches[0];
  2310. }
  2311. else {
  2312. $min_year = $this_year;
  2313. }
  2314. }
  2315. if (!preg_match($year_pattern, $max_year, $matches)) {
  2316. if (preg_match($plus_pattern, $max_year, $matches)) {
  2317. $max_year = $this_year + $matches[0];
  2318. }
  2319. else {
  2320. $max_year = $this_year;
  2321. }
  2322. }
  2323. // We expect the $min year to be less than the $max year.
  2324. // Some custom values for -99:+99 might not obey that.
  2325. if ($min_year > $max_year) {
  2326. $temp = $max_year;
  2327. $max_year = $min_year;
  2328. $min_year = $temp;
  2329. }
  2330. // If there is a current value, stretch the range to include it.
  2331. $value_year = is_object($date) ? $date->format('Y') : '';
  2332. if (!empty($value_year)) {
  2333. $min_year = min($value_year, $min_year);
  2334. $max_year = max($value_year, $max_year);
  2335. }
  2336. return array($min_year, $max_year);
  2337. }
  2338. /**
  2339. * Converts a min and max year into a string like '-3:+1'.
  2340. *
  2341. * @param array $years
  2342. * A numerically indexed array, containing a minimum and maximum year.
  2343. *
  2344. * @return string
  2345. * A min and max year string like '-3:+1'.
  2346. */
  2347. function date_range_string($years) {
  2348. $this_year = date_format(date_now(), 'Y');
  2349. if ($years[0] < $this_year) {
  2350. $min = '-' . ($this_year - $years[0]);
  2351. }
  2352. else {
  2353. $min = '+' . ($years[0] - $this_year);
  2354. }
  2355. if ($years[1] < $this_year) {
  2356. $max = '-' . ($this_year - $years[1]);
  2357. }
  2358. else {
  2359. $max = '+' . ($years[1] - $this_year);
  2360. }
  2361. return $min . ':' . $max;
  2362. }
  2363. /**
  2364. * Temporary helper to re-create equivalent of content_database_info().
  2365. */
  2366. function date_api_database_info($field, $revision = FIELD_LOAD_CURRENT) {
  2367. return array(
  2368. 'columns' => $field['storage']['details']['sql'][$revision],
  2369. 'table' => _field_sql_storage_tablename($field),
  2370. );
  2371. }
  2372. /**
  2373. * Implements hook_form_FORM_ID_alter() for system_regional_settings().
  2374. *
  2375. * Add a form element to configure whether or not week numbers are ISO-8601, the
  2376. * default is FALSE (US/UK/AUS norm).
  2377. */
  2378. function date_api_form_system_regional_settings_alter(&$form, &$form_state, $form_id) {
  2379. $form['locale']['date_api_use_iso8601'] = array(
  2380. '#type' => 'checkbox',
  2381. '#title' => t('Use ISO-8601 week numbers'),
  2382. '#default_value' => variable_get('date_api_use_iso8601', FALSE),
  2383. '#description' => t('IMPORTANT! If checked, First day of week MUST be set to Monday'),
  2384. );
  2385. $form['#validate'][] = 'date_api_form_system_settings_validate';
  2386. }
  2387. /**
  2388. * Validate that the option to use ISO weeks matches first day of week choice.
  2389. */
  2390. function date_api_form_system_settings_validate(&$form, &$form_state) {
  2391. $form_values = $form_state['values'];
  2392. if ($form_values['date_api_use_iso8601'] && $form_values['date_first_day'] != 1) {
  2393. form_set_error('date_first_day', t('When using ISO-8601 week numbers, the first day of the week must be set to Monday.'));
  2394. }
  2395. }
  2396. /**
  2397. * Creates an array of date format types for use as an options list.
  2398. */
  2399. function date_format_type_options() {
  2400. $options = array();
  2401. $format_types = system_get_date_types();
  2402. if (!empty($format_types)) {
  2403. foreach ($format_types as $type => $type_info) {
  2404. $options[$type] = $type_info['title'] . ' (' . date_format_date(date_example_date(), $type) . ')';
  2405. }
  2406. }
  2407. return $options;
  2408. }
  2409. /**
  2410. * Creates an example date.
  2411. *
  2412. * This ensures a clear difference between month and day, and 12 and 24 hours.
  2413. */
  2414. function date_example_date() {
  2415. $now = date_now();
  2416. if (date_format($now, 'M') == date_format($now, 'F')) {
  2417. date_modify($now, '+1 month');
  2418. }
  2419. if (date_format($now, 'm') == date_format($now, 'd')) {
  2420. date_modify($now, '+1 day');
  2421. }
  2422. if (date_format($now, 'H') == date_format($now, 'h')) {
  2423. date_modify($now, '+12 hours');
  2424. }
  2425. return $now;
  2426. }
  2427. /**
  2428. * Determine if a start/end date combination qualify as 'All day'.
  2429. *
  2430. * @param string $string1
  2431. * A string date in datetime format for the 'start' date.
  2432. * @param string $string2
  2433. * A string date in datetime format for the 'end' date.
  2434. * @param string $granularity
  2435. * (optional) The granularity of the date. Defaults to 'second'.
  2436. * @param int $increment
  2437. * (optional) The increment of the date. Defaults to 1.
  2438. *
  2439. * @return bool
  2440. * TRUE if the date is all day, FALSE otherwise.
  2441. */
  2442. function date_is_all_day($string1, $string2, $granularity = 'second', $increment = 1) {
  2443. if (empty($string1) || empty($string2)) {
  2444. return FALSE;
  2445. }
  2446. elseif (!in_array($granularity, array('hour', 'minute', 'second'))) {
  2447. return FALSE;
  2448. }
  2449. preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string1, $matches);
  2450. $count = count($matches);
  2451. $date1 = $count > 1 ? $matches[1] : '';
  2452. $time1 = $count > 2 ? $matches[2] : '';
  2453. $hour1 = $count > 3 ? intval($matches[3]) : 0;
  2454. $min1 = $count > 4 ? intval($matches[4]) : 0;
  2455. $sec1 = $count > 5 ? intval($matches[5]) : 0;
  2456. preg_match('/([0-9]{4}-[0-9]{2}-[0-9]{2}) (([0-9]{2}):([0-9]{2}):([0-9]{2}))/', $string2, $matches);
  2457. $count = count($matches);
  2458. $date2 = $count > 1 ? $matches[1] : '';
  2459. $time2 = $count > 2 ? $matches[2] : '';
  2460. $hour2 = $count > 3 ? intval($matches[3]) : 0;
  2461. $min2 = $count > 4 ? intval($matches[4]) : 0;
  2462. $sec2 = $count > 5 ? intval($matches[5]) : 0;
  2463. if (empty($date1) || empty($date2)) {
  2464. return FALSE;
  2465. }
  2466. if (empty($time1) || empty($time2)) {
  2467. return FALSE;
  2468. }
  2469. $tmp = date_seconds('s', TRUE, $increment);
  2470. $max_seconds = intval(array_pop($tmp));
  2471. $tmp = date_minutes('i', TRUE, $increment);
  2472. $max_minutes = intval(array_pop($tmp));
  2473. // See if minutes and seconds are the maximum allowed for an increment or the
  2474. // maximum possible (59), or 0.
  2475. switch ($granularity) {
  2476. case 'second':
  2477. $min_match = $time1 == '00:00:00'
  2478. || ($hour1 == 0 && $min1 == 0 && $sec1 == 0);
  2479. $max_match = $time2 == '00:00:00'
  2480. || ($hour2 == 23 && in_array($min2, array($max_minutes, 59)) && in_array($sec2, array($max_seconds, 59)))
  2481. || ($hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0 && $sec1 == 0 && $sec2 == 0);
  2482. break;
  2483. case 'minute':
  2484. $min_match = $time1 == '00:00:00'
  2485. || ($hour1 == 0 && $min1 == 0);
  2486. $max_match = $time2 == '00:00:00'
  2487. || ($hour2 == 23 && in_array($min2, array($max_minutes, 59)))
  2488. || ($hour1 == 0 && $hour2 == 0 && $min1 == 0 && $min2 == 0);
  2489. break;
  2490. case 'hour':
  2491. $min_match = $time1 == '00:00:00'
  2492. || ($hour1 == 0);
  2493. $max_match = $time2 == '00:00:00'
  2494. || ($hour2 == 23)
  2495. || ($hour1 == 0 && $hour2 == 0);
  2496. break;
  2497. default:
  2498. $min_match = TRUE;
  2499. $max_match = FALSE;
  2500. }
  2501. if ($min_match && $max_match) {
  2502. return TRUE;
  2503. }
  2504. return FALSE;
  2505. }
  2506. /**
  2507. * Helper function to round minutes and seconds to requested value.
  2508. */
  2509. function date_increment_round(&$date, $increment) {
  2510. // Round minutes and seconds, if necessary.
  2511. if (is_object($date) && $increment > 1) {
  2512. $day = intval(date_format($date, 'j'));
  2513. $hour = intval(date_format($date, 'H'));
  2514. $second = intval(round(intval(date_format($date, 's')) / $increment) * $increment);
  2515. $minute = intval(date_format($date, 'i'));
  2516. if ($second == 60) {
  2517. $minute += 1;
  2518. $second = 0;
  2519. }
  2520. $minute = intval(round($minute / $increment) * $increment);
  2521. if ($minute == 60) {
  2522. $hour += 1;
  2523. $minute = 0;
  2524. }
  2525. date_time_set($date, $hour, $minute, $second);
  2526. if ($hour == 24) {
  2527. $day += 1;
  2528. $hour = 0;
  2529. $year = date_format($date, 'Y');
  2530. $month = date_format($date, 'n');
  2531. date_date_set($date, $year, $month, $day);
  2532. }
  2533. }
  2534. return $date;
  2535. }
  2536. /**
  2537. * Determines if a date object is valid.
  2538. *
  2539. * @param object $date
  2540. * The date object to check.
  2541. *
  2542. * @return bool
  2543. * TRUE if the date is a valid date object, FALSE otherwise.
  2544. */
  2545. function date_is_date($date) {
  2546. if (empty($date) || !is_object($date) || !empty($date->errors)) {
  2547. return FALSE;
  2548. }
  2549. return TRUE;
  2550. }
  2551. /**
  2552. * This function will replace ISO values that have the pattern 9999-00-00T00:00:00
  2553. * with a pattern like 9999-01-01T00:00:00, to match the behavior of non-ISO
  2554. * dates and ensure that date objects created from this value contain a valid month
  2555. * and day. Without this fix, the ISO date '2020-00-00T00:00:00' would be created as
  2556. * November 30, 2019 (the previous day in the previous month).
  2557. *
  2558. * @param string $iso_string
  2559. * An ISO string that needs to be made into a complete, valid date.
  2560. *
  2561. * @TODO Expand on this to work with all sorts of partial ISO dates.
  2562. */
  2563. function date_make_iso_valid($iso_string) {
  2564. // If this isn't a value that uses an ISO pattern, there is nothing to do.
  2565. if (is_numeric($iso_string) || !preg_match(DATE_REGEX_ISO, $iso_string)) {
  2566. return $iso_string;
  2567. }
  2568. // First see if month and day parts are '-00-00'.
  2569. if (substr($iso_string, 4, 6) == '-00-00') {
  2570. return preg_replace('/([\d]{4}-)(00-00)(T[\d]{2}:[\d]{2}:[\d]{2})/', '${1}01-01${3}', $iso_string);
  2571. }
  2572. // Then see if the day part is '-00'.
  2573. elseif (substr($iso_string, 7, 3) == '-00') {
  2574. return preg_replace('/([\d]{4}-[\d]{2}-)(00)(T[\d]{2}:[\d]{2}:[\d]{2})/', '${1}01${3}', $iso_string);
  2575. }
  2576. // Fall through, no changes required.
  2577. return $iso_string;
  2578. }