You are here

README.txt in Date 7

INFORMATION FOR DEVELOPERS

Once the Date API is installed, all functions in the API are available to be used 
anywhere by any module. 

The API uses the PHP 5.2 date functions to create and manipulate dates.

Example, the following will create a date for the local value in one 
timezone, adjust it to a different timezone, then return the offset in seconds 
in the new timezone for the input date; The offset will be adjusted for both 
the timezone difference and daylight savings time, if necessary:

$date = date_create('2007-03-11 02:00:00', timezone_open('America/Chicago'));
$chicago_time = date_format($date, 'Y-m-d H:i');

print 'At '. $chicago_time .' in Chicago, the timezone offset in seconds 
  was '. date_offset_get($date);

date_timezone_set($date, timezone_open('Europe/Berlin');
$berlin_time = date_format($date, 'Y-m-d H:i');

print 'It was '. $berlin_time .' in Berlin when it 
  was '. $chicago_time .' in Chicago.';
print 'At that time in Berlin, the timezone offset in seconds was 
  '. date_offset_get($date);

A helper class is available, new DateObject($string, $timezone, $format),
where $string is a unixtimestamp, an ISO date, or a string like YYYY-MM-DD HH:MM:SS,
$timezone is the name of the timezone this date is in, and $format is the format
of date it is (DATE_FORMAT_UNIX, DATE_FORMAT_ISO, or DATE_FORMAT_DATETIME). 
It creates and return a date object set to the right date and timezone.

Simpletest tests for these functions are included in the package.

Available functions include the following (more documentation is provided in 
the files):

============================================================================
Preconfigured arrays
============================================================================
Both translated and untranslated values are available. The date_week_days_ordered()
function will shift an array of week day names so it starts with the site's
first day of the week, otherwise the weekday names start with Sunday as the first
value, the expected order for many php and sql functions.

date_month_names();
date_month_names_abbr();
date_month_names_untranslated();
date_week_days();
date_week_days_abbr();
date_week_days_untranslated();
date_week_days_ordered();
date_years();
date_hours();
date_minutes();
date_seconds();
date_timezone_names();
date_ampm();

============================================================================
Miscellaneous date manipulation functions 
============================================================================
Pre-defined constants and functions that will handle pre-1970 and post-2038 
dates in both PHP 4 and PHP 5, in any OS. Dates can be converted from one 
type to another and date parts can be extracted from any date type.

DATE_DATETIME
DATE_ISO
DATE_UNIX
DATE_ARRAY
DATE_OBJECT
DATE_ICAL

date_convert()
date_is_valid();
date_part_is_valid();
date_part_extract();

============================================================================
Date calculation and navigation
============================================================================
date_difference() will find the time difference between any two days, measured
in seconds, minutes, hours, days, months, weeks, or years.

date_days_in_month();
date_days_in_year();
date_weeks_in_year();
date_last_day_of_month();
date_day_of_week();
date_day_of_week_name();
date_difference();

============================================================================
Date regex and format helpers 
============================================================================
Pre-defined constants, an array of date format strings and their 
equivalent regex strings.

DATE_REGEX_LOOSE is a very loose regex that will pull date parts out
of an ISO date with or without separators, using either 'T' or a space
to separate date and time, and with or without time.

date_format_date() is similar to format_date(), except it takes a 
date object instead of a timestamp as the first parameter.

DATE_FORMAT_ISO
DATE_FORMAT_DATETIME
DATE_FORMAT_UNIX
DATE_FORMAT_ICAL

DATE_REGEX_ISO
DATE_REGEX_DATETIME
DATE_REGEX_LOOSE

date_format_date();
date_short_formats();
date_medium_formats();
date_long_formats();
date_format_patterns();

============================================================================
Standardized ical parser and creator 
============================================================================
The iCal parser is found in date_api_ical.inc, which is not included by default. 
Include that file if you want to use these functions:

Complete rewrite of ical imports to parse vevents, vlocations, valarms, 
and all kinds of timezone options and repeat rules for ical imports. 
The function now sticks to parsing the ical into an array that can be used 
in various ways. It no longer trys to convert timezones while parsing, 
instead a date_ical_date_format() function is provided that can be used to 
convert from the ical timezone to whatever timezone is desired in the 
results. Repeat rules are parsed into an array which other modules can 
manipulate however they like to create additional events from the results.

date_ical_export();
date_ical_import();
date_ical_date_format();

============================================================================
Helpers for portable date SQL 
============================================================================
The SQL functions are found in date_api_sql.inc, which is not included by default. 
Include that file if you want to use these functions:

date_sql();
date_server_zone_adj();
date_sql_concat();
date_sql_pad();

============================================================================
Date forms and validators
============================================================================
Reusable, configurable, self-validating FAPI date elements are found in 
date_api_elements.inc, which is not included by default. Include it
if you want to use these elements. To use them, create a form element
and set the '#type' to one of the following:

date_select 
  The date_select element will create a collection of form elements, with a
  separate select or textfield for each date part. The whole collection will
  get re-formatted back into a date value of the requested type during validation.

date_text
 The date_text element will create a textfield that can contain a whole
 date or any part of a date as text. The user input value will be re-formatted
 back into a date value of the requested type during validation.

date_timezone
 The date_timezone element will create a drop-down selector to pick a
 timezone name.

The custom date elements require a few other pieces of information to work
correctly, like #date_format and #date_type. See the internal documentation
for more information.

============================================================================
Date Popup Module
============================================================================

A new module is included in the package that will enable a popup jQuery 
calendar date picker and timepicker in date and time fields.

It is implemented as a custom form element, so set '#type' to 'date_popup'
to use this element. See the internal documentation for more information.

============================================================================
Date Repeat API
============================================================================

An API for repeating dates is available if installed. It can be used by 
other modules to create a form element that will allow users to select
repeat rules and store those selections in an iCal RRULE string, and a
calculation function that will parse the RRULE and return an array of dates
that match those rules. The API is implemented in the Date module as a
new date widget if the Date Repeat API is installed.

============================================================================
Install file for dependent modules
============================================================================

The following code is an example of what should go in the .install file for
any module that uses the new Date API. This is needed to be sure the system 
is not using an earlier version of the API that didn't include all these new 
features. Testing for version '5.2' will pick up any version on or after the 
change to the new API.

/**
 * Implementation of hook_requirements().
 */
function calendar_requirements($phase) {
  $requirements = array();
  $t = get_t();

  // This is the minimum required version for the Date API so that it will 
     work with this module.
  $required_version = 5.2;

  // Make sure the matching version of date_api is installed.
  // Use info instead of an error at install time since the problem may
  // just be that they were installed in the wrong order.
  switch ($phase) {
    case 'runtime':
      if (variable_get('date_api_version', 0) < $required_version) {
        $requirements['calendar_api_version'] = array(
          'title' => $t('Calendar requirements'),
          'value' => $t('The Calendar module requires a more current version 
             of the Date API. Please check for a newer version.'),
          'severity' => REQUIREMENT_ERROR,
          );
      }
      break;
     case 'install':
      if (variable_get('date_api_version', 0) < $required_version) {
        $requirements['calendar_api_version'] = array(
          'title' => $t('Calendar requirements'),
          'value' => $t('The Calendar module requires the latest version 
             of the Date API, be sure you are installing the latest versions 
             of both modules.'),
          'severity' => REQUIREMENT_INFO,
          );
      }
      break;
  }
  return $requirements;
}

/**
 * Implementation of hook_install().
 */
function calendar_install() {
  // Make sure this module loads after date_api.
  db_query("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");
}

/**
 * Implementation of hook_update().
 */
function calendar_update_5000() {
  $ret = array();
  $ret[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");
  return $ret;
}

File

README.txt
View source
  1. INFORMATION FOR DEVELOPERS
  2. Once the Date API is installed, all functions in the API are available to be used
  3. anywhere by any module.
  4. The API uses the PHP 5.2 date functions to create and manipulate dates.
  5. Example, the following will create a date for the local value in one
  6. timezone, adjust it to a different timezone, then return the offset in seconds
  7. in the new timezone for the input date; The offset will be adjusted for both
  8. the timezone difference and daylight savings time, if necessary:
  9. $date = date_create('2007-03-11 02:00:00', timezone_open('America/Chicago'));
  10. $chicago_time = date_format($date, 'Y-m-d H:i');
  11. print 'At '. $chicago_time .' in Chicago, the timezone offset in seconds
  12. was '. date_offset_get($date);
  13. date_timezone_set($date, timezone_open('Europe/Berlin');
  14. $berlin_time = date_format($date, 'Y-m-d H:i');
  15. print 'It was '. $berlin_time .' in Berlin when it
  16. was '. $chicago_time .' in Chicago.';
  17. print 'At that time in Berlin, the timezone offset in seconds was
  18. '. date_offset_get($date);
  19. A helper class is available, new DateObject($string, $timezone, $format),
  20. where $string is a unixtimestamp, an ISO date, or a string like YYYY-MM-DD HH:MM:SS,
  21. $timezone is the name of the timezone this date is in, and $format is the format
  22. of date it is (DATE_FORMAT_UNIX, DATE_FORMAT_ISO, or DATE_FORMAT_DATETIME).
  23. It creates and return a date object set to the right date and timezone.
  24. Simpletest tests for these functions are included in the package.
  25. Available functions include the following (more documentation is provided in
  26. the files):
  27. ============================================================================
  28. Preconfigured arrays
  29. ============================================================================
  30. Both translated and untranslated values are available. The date_week_days_ordered()
  31. function will shift an array of week day names so it starts with the site's
  32. first day of the week, otherwise the weekday names start with Sunday as the first
  33. value, the expected order for many php and sql functions.
  34. date_month_names();
  35. date_month_names_abbr();
  36. date_month_names_untranslated();
  37. date_week_days();
  38. date_week_days_abbr();
  39. date_week_days_untranslated();
  40. date_week_days_ordered();
  41. date_years();
  42. date_hours();
  43. date_minutes();
  44. date_seconds();
  45. date_timezone_names();
  46. date_ampm();
  47. ============================================================================
  48. Miscellaneous date manipulation functions
  49. ============================================================================
  50. Pre-defined constants and functions that will handle pre-1970 and post-2038
  51. dates in both PHP 4 and PHP 5, in any OS. Dates can be converted from one
  52. type to another and date parts can be extracted from any date type.
  53. DATE_DATETIME
  54. DATE_ISO
  55. DATE_UNIX
  56. DATE_ARRAY
  57. DATE_OBJECT
  58. DATE_ICAL
  59. date_convert()
  60. date_is_valid();
  61. date_part_is_valid();
  62. date_part_extract();
  63. ============================================================================
  64. Date calculation and navigation
  65. ============================================================================
  66. date_difference() will find the time difference between any two days, measured
  67. in seconds, minutes, hours, days, months, weeks, or years.
  68. date_days_in_month();
  69. date_days_in_year();
  70. date_weeks_in_year();
  71. date_last_day_of_month();
  72. date_day_of_week();
  73. date_day_of_week_name();
  74. date_difference();
  75. ============================================================================
  76. Date regex and format helpers
  77. ============================================================================
  78. Pre-defined constants, an array of date format strings and their
  79. equivalent regex strings.
  80. DATE_REGEX_LOOSE is a very loose regex that will pull date parts out
  81. of an ISO date with or without separators, using either 'T' or a space
  82. to separate date and time, and with or without time.
  83. date_format_date() is similar to format_date(), except it takes a
  84. date object instead of a timestamp as the first parameter.
  85. DATE_FORMAT_ISO
  86. DATE_FORMAT_DATETIME
  87. DATE_FORMAT_UNIX
  88. DATE_FORMAT_ICAL
  89. DATE_REGEX_ISO
  90. DATE_REGEX_DATETIME
  91. DATE_REGEX_LOOSE
  92. date_format_date();
  93. date_short_formats();
  94. date_medium_formats();
  95. date_long_formats();
  96. date_format_patterns();
  97. ============================================================================
  98. Standardized ical parser and creator
  99. ============================================================================
  100. The iCal parser is found in date_api_ical.inc, which is not included by default.
  101. Include that file if you want to use these functions:
  102. Complete rewrite of ical imports to parse vevents, vlocations, valarms,
  103. and all kinds of timezone options and repeat rules for ical imports.
  104. The function now sticks to parsing the ical into an array that can be used
  105. in various ways. It no longer trys to convert timezones while parsing,
  106. instead a date_ical_date_format() function is provided that can be used to
  107. convert from the ical timezone to whatever timezone is desired in the
  108. results. Repeat rules are parsed into an array which other modules can
  109. manipulate however they like to create additional events from the results.
  110. date_ical_export();
  111. date_ical_import();
  112. date_ical_date_format();
  113. ============================================================================
  114. Helpers for portable date SQL
  115. ============================================================================
  116. The SQL functions are found in date_api_sql.inc, which is not included by default.
  117. Include that file if you want to use these functions:
  118. date_sql();
  119. date_server_zone_adj();
  120. date_sql_concat();
  121. date_sql_pad();
  122. ============================================================================
  123. Date forms and validators
  124. ============================================================================
  125. Reusable, configurable, self-validating FAPI date elements are found in
  126. date_api_elements.inc, which is not included by default. Include it
  127. if you want to use these elements. To use them, create a form element
  128. and set the '#type' to one of the following:
  129. date_select
  130. The date_select element will create a collection of form elements, with a
  131. separate select or textfield for each date part. The whole collection will
  132. get re-formatted back into a date value of the requested type during validation.
  133. date_text
  134. The date_text element will create a textfield that can contain a whole
  135. date or any part of a date as text. The user input value will be re-formatted
  136. back into a date value of the requested type during validation.
  137. date_timezone
  138. The date_timezone element will create a drop-down selector to pick a
  139. timezone name.
  140. The custom date elements require a few other pieces of information to work
  141. correctly, like #date_format and #date_type. See the internal documentation
  142. for more information.
  143. ============================================================================
  144. Date Popup Module
  145. ============================================================================
  146. A new module is included in the package that will enable a popup jQuery
  147. calendar date picker and timepicker in date and time fields.
  148. It is implemented as a custom form element, so set '#type' to 'date_popup'
  149. to use this element. See the internal documentation for more information.
  150. ============================================================================
  151. Date Repeat API
  152. ============================================================================
  153. An API for repeating dates is available if installed. It can be used by
  154. other modules to create a form element that will allow users to select
  155. repeat rules and store those selections in an iCal RRULE string, and a
  156. calculation function that will parse the RRULE and return an array of dates
  157. that match those rules. The API is implemented in the Date module as a
  158. new date widget if the Date Repeat API is installed.
  159. ============================================================================
  160. Install file for dependent modules
  161. ============================================================================
  162. The following code is an example of what should go in the .install file for
  163. any module that uses the new Date API. This is needed to be sure the system
  164. is not using an earlier version of the API that didn't include all these new
  165. features. Testing for version '5.2' will pick up any version on or after the
  166. change to the new API.
  167. /**
  168. * Implementation of hook_requirements().
  169. */
  170. function calendar_requirements($phase) {
  171. $requirements = array();
  172. $t = get_t();
  173. // This is the minimum required version for the Date API so that it will
  174. work with this module.
  175. $required_version = 5.2;
  176. // Make sure the matching version of date_api is installed.
  177. // Use info instead of an error at install time since the problem may
  178. // just be that they were installed in the wrong order.
  179. switch ($phase) {
  180. case 'runtime':
  181. if (variable_get('date_api_version', 0) < $required_version) {
  182. $requirements['calendar_api_version'] = array(
  183. 'title' => $t('Calendar requirements'),
  184. 'value' => $t('The Calendar module requires a more current version
  185. of the Date API. Please check for a newer version.'),
  186. 'severity' => REQUIREMENT_ERROR,
  187. );
  188. }
  189. break;
  190. case 'install':
  191. if (variable_get('date_api_version', 0) < $required_version) {
  192. $requirements['calendar_api_version'] = array(
  193. 'title' => $t('Calendar requirements'),
  194. 'value' => $t('The Calendar module requires the latest version
  195. of the Date API, be sure you are installing the latest versions
  196. of both modules.'),
  197. 'severity' => REQUIREMENT_INFO,
  198. );
  199. }
  200. break;
  201. }
  202. return $requirements;
  203. }
  204. /**
  205. * Implementation of hook_install().
  206. */
  207. function calendar_install() {
  208. // Make sure this module loads after date_api.
  209. db_query("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");
  210. }
  211. /**
  212. * Implementation of hook_update().
  213. */
  214. function calendar_update_5000() {
  215. $ret = array();
  216. $ret[] = update_sql("UPDATE {system} SET weight = 1 WHERE name = 'calendar'");
  217. return $ret;
  218. }