forked from qtranslate/qtranslate-xt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qtranslate_date_time.php
295 lines (265 loc) · 9.79 KB
/
qtranslate_date_time.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
function qtranxf_convertDateFormatToStrftimeFormat( $format ) {
$mappings = array(
'd' => '%d',
'D' => '%a',
'j' => '%E',
'l' => '%A',
'N' => '%u',
'S' => '%q',
'w' => '%f',
'z' => '%F',
'W' => '%V',
'F' => '%B',
'm' => '%m',
'M' => '%b',
'n' => '%i',
't' => '%J',
'L' => '%k',
'o' => '%G',
'Y' => '%Y',
'y' => '%y',
'a' => '%P',
'A' => '%p',
'B' => '%K',
'g' => '%l',
'G' => '%L',
'h' => '%I',
'H' => '%H',
'i' => '%M',
's' => '%S',
'u' => '%N',
'e' => '%Q',
'I' => '%o',
'O' => '%O',
'P' => '%s',
'T' => '%v',
'Z' => '%1',
'c' => '%2',
'r' => '%3',
'U' => '%4'
);
$date_parameters = array();
$strftime_parameters = array();
$date_parameters[] = '#%#';
$strftime_parameters[] = '%';
foreach ( $mappings as $df => $sf ) {
$date_parameters[] = '#(([^%\\\\])' . $df . '|^' . $df . ')#';
$strftime_parameters[] = '${2}' . $sf;
}
// convert everything
$format = preg_replace( $date_parameters, $strftime_parameters, $format );
// remove single backslashes from dates
$format = preg_replace( '#\\\\([^\\\\]{1})#', '${1}', $format );
// remove double backslashes from dates
$format = preg_replace( '#\\\\\\\\#', '\\\\', $format );
return $format;
}
function qtranxf_convertFormat( $format, $default_format ) {
global $q_config;
// if one of special language-neutral formats are requested, don't replace it
switch ( $format ) {
case 'Z':
case 'c':
case 'r':
case 'U':
return qtranxf_convertDateFormatToStrftimeFormat( $format );
default:
break;
}
// check for multilang formats
$format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $format );
$default_format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $default_format );
switch ( $q_config['use_strftime'] ) {
case QTX_DATE:
if ( $format == '' ) {
$format = $default_format;
}
return qtranxf_convertDateFormatToStrftimeFormat( $format );
case QTX_DATE_OVERRIDE:
return qtranxf_convertDateFormatToStrftimeFormat( $default_format );
case QTX_STRFTIME:
return $format;
case QTX_STRFTIME_OVERRIDE:
default:
return $default_format;
}
}
function qtranxf_convertDateFormat( $format ) {
global $q_config;
if ( isset( $q_config['date_format'][ $q_config['language'] ] ) ) {
$default_format = $q_config['date_format'][ $q_config['language'] ];
} elseif ( isset( $q_config['date_format'][ $q_config['default_language'] ] ) ) {
$default_format = $q_config['date_format'][ $q_config['default_language'] ];
} else {
$default_format = '';
}
return qtranxf_convertFormat( $format, $default_format );
}
function qtranxf_convertTimeFormat( $format ) {
global $q_config;
if ( isset( $q_config['time_format'][ $q_config['language'] ] ) ) {
$default_format = $q_config['time_format'][ $q_config['language'] ];
} elseif ( isset( $q_config['time_format'][ $q_config['default_language'] ] ) ) {
$default_format = $q_config['time_format'][ $q_config['default_language'] ];
} else {
$default_format = '';
}
return qtranxf_convertFormat( $format, $default_format );
}
// TODO these format functions don't seem to be used by anyone - temporarily commented before removal
//function qtranxf_formatCommentDateTime( $format ) {
// global $comment;
//
// return qtranxf_strftime( qtranxf_convertFormat( $format, $format ), mysql2date( 'U', $comment->comment_date ), '' );
//}
//
//function qtranxf_formatPostDateTime( $format ) {
// global $post;
//
// return qtranxf_strftime( qtranxf_convertFormat( $format, $format ), mysql2date( 'U', $post->post_date ), '' );
//}
//
//function qtranxf_formatPostModifiedDateTime( $format ) {
// global $post;
//
// return qtranxf_strftime( qtranxf_convertFormat( $format, $format ), mysql2date( 'U', $post->post_modified ), '' );
//}
function qtranxf_strftime( $format, $date, $default = '', $before = '', $after = '' ) {
// don't do anything if format is not given
if ( $format == '' ) {
return $default;
}
// add date suffix ability (%q) to strftime
$day = intval( ltrim( strftime( "%d", $date ), '0' ) );
$search = array();
$replace = array();
// date S
$search[] = '/(([^%])%q|^%q)/';
if ( $day == 1 || $day == 21 || $day == 31 ) {
$replace[] = '$2st';
} elseif ( $day == 2 || $day == 22 ) {
$replace[] = '$2nd';
} elseif ( $day == 3 || $day == 23 ) {
$replace[] = '$2rd';
} else {
$replace[] = '$2th';
}
$search[] = '/(([^%])%E|^%E)/';
$replace[] = '${2}' . $day; // date j
$search[] = '/(([^%])%f|^%f)/';
$replace[] = '${2}' . date( 'w', $date ); // date w
$search[] = '/(([^%])%F|^%F)/';
$replace[] = '${2}' . date( 'z', $date ); // date z
$search[] = '/(([^%])%i|^%i)/';
$replace[] = '${2}' . date( 'n', $date ); // date i
$search[] = '/(([^%])%J|^%J)/';
$replace[] = '${2}' . date( 't', $date ); // date t
$search[] = '/(([^%])%k|^%k)/';
$replace[] = '${2}' . date( 'L', $date ); // date L
$search[] = '/(([^%])%K|^%K)/';
$replace[] = '${2}' . date( 'B', $date ); // date B
$search[] = '/(([^%])%l|^%l)/';
$replace[] = '${2}' . date( 'g', $date ); // date g
$search[] = '/(([^%])%L|^%L)/';
$replace[] = '${2}' . date( 'G', $date ); // date G
$search[] = '/(([^%])%N|^%N)/';
$replace[] = '${2}' . date( 'u', $date ); // date u
$search[] = '/(([^%])%Q|^%Q)/';
$replace[] = '${2}' . date( 'e', $date ); // date e
$search[] = '/(([^%])%o|^%o)/';
$replace[] = '${2}' . date( 'I', $date ); // date I
$search[] = '/(([^%])%O|^%O)/';
$replace[] = '${2}' . date( 'O', $date ); // date O
$search[] = '/(([^%])%s|^%s)/';
$replace[] = '${2}' . date( 'P', $date ); // date P
$search[] = '/(([^%])%v|^%v)/';
$replace[] = '${2}' . date( 'T', $date ); // date T
$search[] = '/(([^%])%1|^%1)/';
$replace[] = '${2}' . date( 'Z', $date ); // date Z
$search[] = '/(([^%])%2|^%2)/';
$replace[] = '${2}' . date( 'c', $date ); // date c
$search[] = '/(([^%])%3|^%3)/';
$replace[] = '${2}' . date( 'r', $date ); // date r
$search[] = '/(([^%])%4|^%4)/';
$replace[] = '${2}' . $date; // date U
$format = preg_replace( $search, $replace, $format );
//qtranxf_dbg_log('qtranxf_strftime: $format='.$format.'; $date=',$date);
return $before . strftime( $format, $date ) . $after;
}
function qtranxf_format_date( $format, $mysq_time, $default, $before = '', $after = '' ) {
global $q_config;
$ts = mysql2date( 'U', $mysq_time );
if ( $format == 'U' ) {
return $ts;
}
$format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $format );
if ( ! empty( $format ) && $q_config['use_strftime'] == QTX_STRFTIME ) {
$format = qtranxf_convertDateFormatToStrftimeFormat( $format );
}
return qtranxf_strftime( qtranxf_convertDateFormat( $format ), $ts, $default, $before, $after );
}
function qtranxf_format_time( $format, $mysq_time, $default, $before = '', $after = '' ) {
global $q_config;
$ts = mysql2date( 'U', $mysq_time );
if ( $format == 'U' ) {
return $ts;
}
$format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage( $format );
if ( ! empty( $format ) && $q_config['use_strftime'] == QTX_STRFTIME ) {
$format = qtranxf_convertDateFormatToStrftimeFormat( $format );
}
return qtranxf_strftime( qtranxf_convertTimeFormat( $format ), $ts, $default, $before, $after );
}
function qtranxf_dateFromPostForCurrentLanguage( $old_date, $format = '', $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return $old_date;
}
return qtranxf_format_date( $format, $post->post_date, $old_date );
}
function qtranxf_dateModifiedFromPostForCurrentLanguage( $old_date, $format = '' ) {
global $post;
if ( ! $post ) {
return $old_date;
}
return qtranxf_format_date( $format, $post->post_modified, $old_date );
}
function qtranxf_timeFromPostForCurrentLanguage( $old_date, $format = '', $post = null, $gmt = false ) {
$post = get_post( $post );
if ( ! $post ) {
return $old_date;
}
$post_date = $gmt ? $post->post_date_gmt : $post->post_date;
return qtranxf_format_time( $format, $post_date, $old_date );
}
function qtranxf_timeModifiedFromPostForCurrentLanguage( $old_date, $format = '', $gmt = false ) {
global $post;
if ( ! $post ) {
return $old_date;
}
$post_date = $gmt ? $post->post_modified_gmt : $post->post_modified;
return qtranxf_format_time( $format, $post_date, $old_date );
}
function qtranxf_dateFromCommentForCurrentLanguage( $old_date, $format, $comment = null ) {
if ( ! $comment ) {
global $comment; // TODO drop obsolete compatibility with older WP
}
if ( ! $comment ) {
return $old_date;
}
return qtranxf_format_date( $format, $comment->comment_date, $old_date );
}
function qtranxf_timeFromCommentForCurrentLanguage( $old_date, $format = '', $gmt = false, $translate = true, $comment = null ) {
if ( ! $translate ) {
return $old_date;
}
if ( ! $comment ) {
global $comment; // TODO drop obsolete compatibility with older WP
}
if ( ! $comment ) {
return $old_date;
}
$comment_date = $gmt ? $comment->comment_date_gmt : $comment->comment_date;
return qtranxf_format_time( $format, $comment_date, $old_date );
}