]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
configurable format for date input selectors
[friendica.git] / include / datetime.php
1 <?php
2
3 // two-level sort for timezones.
4
5 if(! function_exists('timezone_cmp')) {
6 function timezone_cmp($a, $b) {
7         if(strstr($a,'/') && strstr($b,'/')) {
8                 if ( t($a) == t($b)) return 0;
9                 return ( t($a) < t($b)) ? -1 : 1;
10         }
11         if(strstr($a,'/')) return -1;
12         if(strstr($b,'/')) return  1;
13         if ( t($a) == t($b)) return 0;
14         return ( t($a) < t($b)) ? -1 : 1;
15 }}
16
17 // emit a timezone selector grouped (primarily) by continent
18
19 if(! function_exists('select_timezone')) {
20 function select_timezone($current = 'America/Los_Angeles') {
21
22         $timezone_identifiers = DateTimeZone::listIdentifiers();
23         
24         $o ='<select id="timezone_select" name="timezone">';
25
26         usort($timezone_identifiers, 'timezone_cmp');
27         $continent = '';
28         foreach($timezone_identifiers as $value) {
29                 $ex = explode("/", $value);
30                 if(count($ex) > 1) {
31                         if($ex[0] != $continent) {
32                                 if($continent != '')
33                                         $o .= '</optgroup>';
34                                 $continent = $ex[0];
35                                 $o .= '<optgroup label="' . t($continent) . '">';
36                         }
37                         if(count($ex) > 2)
38                                 $city = substr($value,strpos($value,'/')+1);
39                         else
40                                 $city = $ex[1];
41                 }
42                 else {
43                         $city = $ex[0];
44                         if($continent != t('Miscellaneous')) {
45                                 $o .= '</optgroup>';
46                                 $continent = t('Miscellaneous');
47                                 $o .= '<optgroup label="' . t($continent) . '">';       
48                         }
49                 }
50                 $city = str_replace('_', ' ',  t($city));
51                 $selected = (($value == $current) ? " selected=\"selected\" " : "");
52                 $o .= "<option value=\"$value\" $selected >$city</option>";
53         }    
54         $o .= '</optgroup></select>';
55         return $o;
56 }}
57
58 // General purpose date parse/convert function.
59 // $from = source timezone
60 // $to   = dest timezone
61 // $s    = some parseable date/time string
62 // $fmt  = output format
63
64 if(! function_exists('datetime_convert')) {
65 function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d H:i:s") {
66
67         // Slight hackish adjustment so that 'zero' datetime actually returns what is intended
68         // otherwise we end up with -0001-11-30 ...
69         // add 32 days so that we at least get year 00, and then hack around the fact that 
70         // months and days always start with 1. 
71
72         if(substr($s,0,10) == '0000-00-00') {
73                 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
74                 return str_replace('1','0',$d->format($fmt));
75         }
76
77         $d = new DateTime($s, new DateTimeZone($from));
78         $d->setTimeZone(new DateTimeZone($to));
79         return($d->format($fmt));
80 }}
81
82 // wrapper for date selector, tailored for use in birthday fields
83
84 function dob($dob) {
85         list($year,$month,$day) = sscanf($dob,'%4d-%2d-%2d');
86         $y = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
87         $f = get_config('system','birthday_input_format');
88         if(! $f)
89                 $f = 'ymd';
90         $o = datesel($f,'',1920,$y,true,$year,$month,$day);
91         return $o;
92 }
93
94
95 function datesel_format($f) {
96
97         $o = '';
98
99         if(strlen($f)) {
100                 for($x = 0; $x < strlen($f); $x ++) {
101                         switch($f[$x]) {
102                                 case 'y':
103                                         if(strlen($o))
104                                                 $o .= '-';
105                                         $o .= t('year');                                        
106                                         break;
107                                 case 'm':
108                                         if(strlen($o))
109                                                 $o .= '-';
110                                         $o .= t('month');                                       
111                                         break;
112                                 case 'd':
113                                         if(strlen($o))
114                                                 $o .= '-';
115                                         $o .= t('day');
116                                         break;
117                                 default:
118                                         break;
119                         }
120                 }
121         }
122         return $o;
123 }
124
125
126 // returns a date selector.
127 // $f           = format string, e.g. 'ymd' or 'mdy'
128 // $pre         = prefix (if needed) for HTML name and class fields
129 // $ymin        = first year shown in selector dropdown
130 // $ymax        = last year shown in selector dropdown
131 // $allow_blank = allow an empty response on any field
132 // $y           = already selected year
133 // $m           = already selected month
134 // $d           = already selected day
135
136 if(! function_exists('datesel')) {
137 function datesel($f,$pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
138
139         $o = '';
140
141         if(strlen($f)) {
142                 for($z = 0; $z < strlen($f); $z ++) {
143                         if($f[$z] === 'y') {
144
145                                 $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
146                                 if($allow_blank) {
147                                         $sel = (($y == '0000') ? " selected=\"selected\" " : "");
148                                         $o .= "<option value=\"0000\" $sel ></option>";
149                                 }
150
151                                 if($ymax > $ymin) {
152                                         for($x = $ymax; $x >= $ymin; $x --) {
153                                                 $sel = (($x == $y) ? " selected=\"selected\" " : "");
154                                                 $o .= "<option value=\"$x\" $sel>$x</option>";
155                                         }
156                                 }
157                                 else {
158                                         for($x = $ymax; $x <= $ymin; $x ++) {
159                                                 $sel = (($x == $y) ? " selected=\"selected\" " : "");
160                                                 $o .= "<option value=\"$x\" $sel>$x</option>";
161                                         }
162                                 }
163                         }
164                         elseif($f[$z] == 'm') {
165   
166                                 $o .= "</select> <select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
167                                 for($x = (($allow_blank) ? 0 : 1); $x <= 12; $x ++) {
168                                         $sel = (($x == $m) ? " selected=\"selected\" " : "");
169                                         $y = (($x) ? $x : '');
170                                         $o .= "<option value=\"$x\" $sel>$y</option>";
171                                 }
172                         }
173                         elseif($f[$z] == 'd') {
174
175                                 $o .= "</select> <select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
176                                 for($x = (($allow_blank) ? 0 : 1); $x <= 31; $x ++) {
177                                         $sel = (($x == $d) ? " selected=\"selected\" " : "");
178                                         $y = (($x) ? $x : '');
179                                         $o .= "<option value=\"$x\" $sel>$y</option>";
180                                 }
181                         }
182                 }
183         }
184
185         $o .= "</select>";
186         return $o;
187 }}
188
189 if(! function_exists('timesel')) {
190 function timesel($pre,$h,$m) {
191
192         $o = '';
193         $o .= "<select name=\"{$pre}hour\" class=\"{$pre}hour\" size=\"1\">";
194         for($x = 0; $x < 24; $x ++) {
195                 $sel = (($x == $h) ? " selected=\"selected\" " : "");
196                 $o .= "<option value=\"$x\" $sel>$x</option>";
197         }
198         $o .= "</select> : <select name=\"{$pre}minute\" class=\"{$pre}minute\" size=\"1\">";
199         for($x = 0; $x < 60; $x ++) {
200                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
201                 $o .= "<option value=\"$x\" $sel>$x</option>";
202         }
203
204         $o .= "</select>";
205         return $o;
206 }}
207
208
209
210
211
212
213
214
215 // implements "3 seconds ago" etc.
216 // based on $posted_date, (UTC).
217 // Results relative to current timezone
218 // Limited to range of timestamps
219
220 if(! function_exists('relative_date')) {
221 function relative_date($posted_date) {
222
223         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
224
225         $abs = strtotime($localtime);
226     
227     if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
228                  return t('never');
229         }
230
231         $etime = time() - $abs;
232     
233         if ($etime < 1) {
234                 return t('less than a second ago');
235         }
236     
237         $a = array( 12 * 30 * 24 * 60 * 60  =>  array( t('year'),   t('years')),
238                                 30 * 24 * 60 * 60       =>  array( t('month'),  t('months')),
239                                 7  * 24 * 60 * 60       =>  array( t('week'),   t('weeks')),
240                                 24 * 60 * 60            =>  array( t('day'),    t('days')),
241                                 60 * 60                 =>  array( t('hour'),   t('hours')),
242                                 60                      =>  array( t('minute'), t('minutes')),
243                                 1                       =>  array( t('second'), t('seconds'))
244         );
245     
246         foreach ($a as $secs => $str) {
247         $d = $etime / $secs;
248         if ($d >= 1) {
249                 $r = round($d);
250                 return $r . ' ' . (($r == 1) ? $str[0] : $str[1]) . t(' ago');
251         }
252     }
253 }}
254
255
256
257 // Returns age in years, given a date of birth,
258 // the timezone of the person whose date of birth is provided,
259 // and the timezone of the person viewing the result.
260 // Why? Bear with me. Let's say I live in Mittagong, Australia, and my 
261 // birthday is on New Year's. You live in San Bruno, California.
262 // When exactly are you going to see my age increase?
263 // A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start 
264 // celebrating and become a year older. If you wish me happy birthday 
265 // on January 1 (San Bruno time), you'll be a day late. 
266    
267 function age($dob,$owner_tz = '',$viewer_tz = '') {
268         if(! intval($dob))
269                 return 0;
270         if(! $owner_tz)
271                 $owner_tz = date_default_timezone_get();
272         if(! $viewer_tz)
273                 $viewer_tz = date_default_timezone_get();
274
275         $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
276         list($year,$month,$day) = explode("-",$birthdate);
277         $year_diff  = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
278         $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
279         $curr_day   = datetime_convert('UTC',$viewer_tz,'now','d');
280
281         if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
282                 $year_diff--;
283         return $year_diff;
284 }
285
286
287
288 // Get days in month
289 // get_dim($year, $month);
290 // returns number of days.
291 // $month[1] = 'January'; 
292 //   to match human usage.
293
294 if(! function_exists('get_dim')) {
295 function get_dim($y,$m) {
296
297   $dim = array( 0,
298     31, 28, 31, 30, 31, 30,
299     31, 31, 30, 31, 30, 31);
300  
301   if($m != 2)
302     return $dim[$m];
303   if(((($y % 4) == 0) && (($y % 100) != 0)) || (($y % 400) == 0))
304     return 29;
305   return $dim[2];
306 }}
307
308
309 // Returns the first day in month for a given month, year
310 // get_first_dim($year,$month)
311 // returns 0 = Sunday through 6 = Saturday
312 // Months start at 1.
313
314 if(! function_exists('get_first_dim')) {
315 function get_first_dim($y,$m) {
316   $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
317   return datetime_convert('UTC','UTC',$d,'w');
318 }}
319
320 // output a calendar for the given month, year.
321 // if $links are provided (array), e.g. $links[12] => 'http://mylink' , 
322 // date 12 will be linked appropriately. Today's date is also noted by 
323 // altering td class.
324 // Months count from 1.
325
326
327 // TODO: provide (prev,next) links, define class variations for different size calendars
328
329
330 if(! function_exists('cal')) {
331 function cal($y = 0,$m = 0, $links = false, $class='') {
332
333
334         // month table - start at 1 to match human usage.
335
336         $mtab = array(' ',
337           'January','February','March',
338           'April','May','June',
339           'July','August','September',
340           'October','November','December'
341         ); 
342
343         $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
344         $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
345         if(! $y)
346                 $y = $thisyear;
347         if(! $m)
348                 $m = intval($thismonth);
349
350   $dn = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
351   $f = get_first_dim($y,$m);
352   $l = get_dim($y,$m);
353   $d = 1;
354   $dow = 0;
355   $started = false;
356
357   if(($y == $thisyear) && ($m == $thismonth))
358     $tddate = intval(datetime_convert('UTC',date_default_timezone_get(),'now','j'));
359
360         $str_month = day_translate($mtab[$m]);
361   $o = '<table class="calendar' . $class . '">';
362   $o .= "<caption>$str_month $y</caption><tr>";
363   for($a = 0; $a < 7; $a ++)
364      $o .= '<th>' . mb_substr(day_translate($dn[$a]),0,3,'UTF-8') . '</th>';
365   $o .= '</tr><tr>';
366
367   while($d <= $l) {
368     if(($dow == $f) && (! $started))
369       $started = true;
370     $today = (((isset($tddate)) && ($tddate == $d)) ? "class=\"today\" " : '');
371     $o .= "<td $today>";
372         $day = str_replace(' ','&nbsp;',sprintf('%2.2d', $d));
373     if($started) {
374       if(is_array($links) && isset($links[$d]))
375         $o .=  "<a href=\"{$links[$d]}\">$day</a>";
376       else
377         $o .= $day;
378       $d ++;
379     }
380     else
381       $o .= '&nbsp;';
382     $o .= '</td>';
383     $dow ++;
384     if(($dow == 7) && ($d <= $l)) {
385       $dow = 0;
386       $o .= '</tr><tr>';
387     }
388   }
389   if($dow)
390     for($a = $dow; $a < 7; $a ++)
391        $o .= '<td>&nbsp;</td>';
392   $o .= '</tr></table>'."\r\n";  
393   
394   return $o;
395 }}