1
|
Date API
|
2
|
--------
|
3
|
Once the Date API is installed, all functions in the API are available to be
|
4
|
used anywhere by any module.
|
5
|
|
6
|
The API uses the PHP 5.2 date functions to create and manipulate dates.
|
7
|
|
8
|
Example, the following will create a date for the local value in one
|
9
|
timezone, adjust it to a different timezone, then return the offset in seconds
|
10
|
in the new timezone for the input date; The offset will be adjusted for both
|
11
|
the timezone difference and daylight savings time, if necessary:
|
12
|
|
13
|
$date = date_create('2007-03-11 02:00:00', timezone_open('America/Chicago'));
|
14
|
$chicago_time = date_format($date, 'Y-m-d H:i');
|
15
|
|
16
|
print 'At '. $chicago_time .' in Chicago, the timezone offset in seconds
|
17
|
was '. date_offset_get($date);
|
18
|
|
19
|
date_timezone_set($date, timezone_open('Europe/Berlin');
|
20
|
$berlin_time = date_format($date, 'Y-m-d H:i');
|
21
|
|
22
|
print 'It was '. $berlin_time .' in Berlin when it
|
23
|
was '. $chicago_time .' in Chicago.';
|
24
|
print 'At that time in Berlin, the timezone offset in seconds was
|
25
|
'. date_offset_get($date);
|
26
|
|
27
|
A helper class is available, new DateObject($string, $timezone, $format), where
|
28
|
$string is a unixtimestamp, an ISO date, or a string like YYYY-MM-DD HH:MM:SS,
|
29
|
$timezone is the name of the timezone this date is in, and $format is the format
|
30
|
of date it is (DATE_FORMAT_UNIX, DATE_FORMAT_ISO, or DATE_FORMAT_DATETIME). It
|
31
|
creates and return a date object set to the right date and timezone.
|
32
|
|
33
|
Simpletest tests for these functions are included in the package.
|
34
|
|
35
|
Available functions include the following (more documentation is provided in
|
36
|
the files).
|
37
|
|
38
|
|
39
|
Preconfigured arrays
|
40
|
--------------------------------------------------------------------------------
|
41
|
Both translated and untranslated values are available. For example the
|
42
|
'date_week_days_ordered()' function will shift an array of week day names so it
|
43
|
starts with the site's first day of the week, otherwise the weekday names start
|
44
|
with Sunday as the first value, which is the expected order for many php and sql
|
45
|
functions.
|
46
|
|
47
|
date_month_names();
|
48
|
date_month_names_abbr();
|
49
|
date_month_names_untranslated();
|
50
|
date_week_days();
|
51
|
date_week_days_abbr();
|
52
|
date_week_days_untranslated();
|
53
|
date_week_days_ordered();
|
54
|
date_years();
|
55
|
date_hours();
|
56
|
date_minutes();
|
57
|
date_seconds();
|
58
|
date_timezone_names();
|
59
|
date_timezone_abbr();
|
60
|
date_timezone_is_valid();
|
61
|
date_default_timezone();
|
62
|
date_default_timezone_object();
|
63
|
date_ampm();
|
64
|
date_hidden_element();
|
65
|
date_granularity_names();
|
66
|
date_granularity_sorted();
|
67
|
date_granularity_array_from_precision();
|
68
|
date_granularity_precision();
|
69
|
date_granularity_format();
|
70
|
date_now();
|
71
|
|
72
|
|
73
|
Miscellaneous date manipulation functions
|
74
|
--------------------------------------------------------------------------------
|
75
|
Pre-defined constants and functions that will handle pre-1970 and post-2038
|
76
|
dates in both PHP 4 and PHP 5, in any OS. Dates can be converted from one
|
77
|
type to another and date parts can be extracted from any date type.
|
78
|
|
79
|
DATE_DATETIME
|
80
|
DATE_ISO
|
81
|
DATE_UNIX
|
82
|
DATE_ARRAY
|
83
|
DATE_OBJECT
|
84
|
DATE_ICAL
|
85
|
|
86
|
date_is_all_day();
|
87
|
date_increment_round();
|
88
|
date_is_date();
|
89
|
date_pad();
|
90
|
date_has_time();
|
91
|
date_has_date();
|
92
|
date_part_format();
|
93
|
date_limit_format();
|
94
|
date_nongranularity();
|
95
|
date_order_translated();
|
96
|
date_order();
|
97
|
date_range_valid();
|
98
|
date_range_years();
|
99
|
date_range_string();
|
100
|
date_format_type_options();
|
101
|
date_example_date();
|
102
|
date_is_all_day();
|
103
|
date_increment_round();
|
104
|
date_make_iso_valid();
|
105
|
|
106
|
|
107
|
Date calculation and navigation
|
108
|
--------------------------------------------------------------------------------
|
109
|
For example 'date_days_in_month()' identifies the number of days in a month for
|
110
|
a date.
|
111
|
|
112
|
date_days_in_month();
|
113
|
date_days_in_year();
|
114
|
date_iso_weeks_in_year();
|
115
|
date_iso_week_range();
|
116
|
date_weeks_in_year();
|
117
|
date_day_of_week();
|
118
|
date_day_of_week_name();
|
119
|
date_week_range();
|
120
|
date_week();
|
121
|
date_get_timezone();
|
122
|
date_get_timezone_db();
|
123
|
|
124
|
|
125
|
Date regex and format helpers
|
126
|
--------------------------------------------------------------------------------
|
127
|
Pre-defined constants, an array of date format strings and their
|
128
|
equivalent regex strings.
|
129
|
|
130
|
DATE_REGEX_LOOSE is a very loose regex that will pull date parts out
|
131
|
of an ISO date with or without separators, using either 'T' or a space
|
132
|
to separate date and time, and with or without time.
|
133
|
|
134
|
'date_format_date()'' is similar to 'format_date()', except it takes a
|
135
|
date object instead of a timestamp as the first parameter.
|
136
|
|
137
|
DATE_FORMAT_ISO
|
138
|
DATE_FORMAT_DATETIME
|
139
|
DATE_FORMAT_UNIX
|
140
|
DATE_FORMAT_ICAL
|
141
|
|
142
|
DATE_REGEX_ISO
|
143
|
DATE_REGEX_DATETIME
|
144
|
DATE_REGEX_LOOSE
|
145
|
|
146
|
date_format_date();
|
147
|
date_format_patterns();
|
148
|
date_format_interval();
|
149
|
date_format_order();
|
150
|
date_format_type_options();
|
151
|
date_type_format();
|
152
|
|
153
|
|
154
|
Standardized ical parser and creator
|
155
|
--------------------------------------------------------------------------------
|
156
|
The iCal parser is found in 'date_api_ical.inc', which is not included by
|
157
|
default. Include that file if you want to use these functions:
|
158
|
|
159
|
Complete rewrite of ical imports to parse vevents, vlocations, valarms,
|
160
|
and all kinds of timezone options and repeat rules for ical imports.
|
161
|
The function now sticks to parsing the ical into an array that can be used
|
162
|
in various ways. It no longer trys to convert timezones while parsing instead a
|
163
|
'date_ical_date()' function is provided that can be used to convert from the
|
164
|
ical timezone to the local timezone.
|
165
|
iCal properties can be parsed into an array which other modules can manipulate
|
166
|
however they like to create additional events from the results.
|
167
|
The function 'date_api_ical_build_rrule()'' can be used to build an iCal RULE
|
168
|
from $form_values.
|
169
|
|
170
|
date_ical_import();
|
171
|
date_ical_parse();
|
172
|
date_ical_parse_date();
|
173
|
date_ical_parse_rrule();
|
174
|
date_ical_parse_exceptions();
|
175
|
date_ical_parse_duration();
|
176
|
date_ical_parse_text();
|
177
|
date_ical_parse_location();
|
178
|
date_ical_date();
|
179
|
date_ical_escape_text();
|
180
|
date_api_ical_build_rrule();
|
181
|
|
182
|
|
183
|
Helpers for portable date SQL
|
184
|
--------------------------------------------------------------------------------
|
185
|
The SQL functions are found in date_api_sql.inc, which is not included by
|
186
|
default. Include that file if you want to use these functions:
|
187
|
|
188
|
date_sql_concat();
|
189
|
date_sql_coalesce();
|
190
|
date_sql_pad();
|
191
|
|
192
|
|
193
|
Date forms and validators
|
194
|
--------------------------------------------------------------------------------
|
195
|
Reusable, configurable, self-validating FAPI date elements are found in
|
196
|
date_api_elements.inc, which is not included by default. Include it if you want
|
197
|
to use these elements. To use them, create a form element and set the '#type'
|
198
|
to one of the following:
|
199
|
|
200
|
date_select
|
201
|
The date_select element will create a collection of form elements, with a
|
202
|
separate select or textfield for each date part. The whole collection will
|
203
|
get reformatted back into a date value of the requested type during
|
204
|
validation.
|
205
|
|
206
|
date_text
|
207
|
The date_text element will create a textfield that can contain a whole date
|
208
|
or any part of a date as text. The user input value will be re-formatted back
|
209
|
into a date value of the requested type during validation.
|
210
|
|
211
|
date_timezone
|
212
|
The date_timezone element will create a drop-down selector to pick a timezone
|
213
|
name.
|
214
|
|
215
|
The custom date elements require a few other pieces of information to work
|
216
|
correctly, like #date_format and #date_type. See the internal documentation
|
217
|
for more information.
|
218
|
|
219
|
|
220
|
Date Repeat API
|
221
|
--------------------------------------------------------------------------------
|
222
|
An API for repeating dates is available if installed. It can be used by other
|
223
|
modules to create a form element that will allow users to select repeat rules
|
224
|
and store those selections in an iCal RRULE string, and a calculation function
|
225
|
that will parse the RRULE and return an array of dates that match those rules.
|
226
|
The API is implemented in the Date module as a new date widget if the Date
|
227
|
Repeat API is installed.
|
228
|
|
229
|
|
230
|
RDF Integration
|
231
|
--------------------------------------------------------------------------------
|
232
|
To make RDF easier to use, the base date themes ('date_display_single' and
|
233
|
'date_display_range') have been expanded so they pass attributes and RDF
|
234
|
mappings for the field, if any, to the theme. If RDF is installed and no other
|
235
|
mappings are provided, the theme adds RDF information to mark both the Start
|
236
|
and End dates as 'xsd:dateTime' datatypes with the property of 'dc:date'. This
|
237
|
occurs in the theme preprocess layer, in particular via the functions
|
238
|
'template_preprocess_date_display_single()' and
|
239
|
'template_preprocess_date_display_range()'.
|
240
|
|
241
|
To mark these as events instead, you could install the schemaorg module [1],
|
242
|
which will load the schema.org vocabulary. The mark the content type that
|
243
|
contains events as an 'Event', using the UI exposed by that module and set the
|
244
|
event start date field with the 'dateStart' property and tag other fields in the
|
245
|
content type with the appropriate property types. The Date module theme will
|
246
|
wrap the start and end date output with appropriate markup.
|
247
|
|
248
|
If the result is not quite what you need, you should be able to implement your
|
249
|
own theme preprocess functions, e.g. 'MYTHEME_preprocess_date_display_single()'
|
250
|
or 'MYTHEME_preprocess_date_display_range()' and alter the attributes to use the
|
251
|
values you want.
|
252
|
|
253
|
|
254
|
References
|
255
|
--------------------------------------------------------------------------------
|
256
|
1: https://www.drupal.org/project/schemaorg
|