]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
more events framework
[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         $o = datesel('',1920,$y,true,$year,$month,$day);
88         return $o;
89 }
90
91
92 // returns a date selector.
93 // $pre         = prefix (if needed) for HTML name and class fields
94 // $ymin        = first year shown in selector dropdown
95 // $ymax        = last year shown in selector dropdown
96 // $allow_blank = allow an empty response on any field
97 // $y           = already selected year
98 // $m           = already selected month
99 // $d           = already selected day
100
101 if(! function_exists('datesel')) {
102 function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
103
104         $o = '';
105         $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
106         if($allow_blank) {
107                 $sel = (($y == '0000') ? " selected=\"selected\" " : "");
108                 $o .= "<option value=\"0000\" $sel ></option>";
109         }
110
111         if($ymax > $ymin) {
112                 for($x = $ymax; $x >= $ymin; $x --) {
113                         $sel = (($x == $y) ? " selected=\"selected\" " : "");
114                         $o .= "<option value=\"$x\" $sel>$x</option>";
115                 }
116         }
117         else {
118                 for($x = $ymax; $x <= $ymin; $x ++) {
119                         $sel = (($x == $y) ? " selected=\"selected\" " : "");
120                         $o .= "<option value=\"$x\" $sel>$x</option>";
121                 }
122         }
123   
124         $o .= "</select> <select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
125         for($x = 0; $x <= 12; $x ++) {
126                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
127                 $y = (($x) ? $x : '');
128                 $o .= "<option value=\"$x\" $sel>$y</option>";
129         }
130
131         $o .= "</select> <select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
132         for($x = 0; $x <= 31; $x ++) {
133                 $sel = (($x == $d) ? " selected=\"selected\" " : "");
134                 $y = (($x) ? $x : '');
135                 $o .= "<option value=\"$x\" $sel>$y</option>";
136         }
137
138         $o .= "</select>";
139         return $o;
140 }}
141
142 if(! function_exists('timesel')) {
143 function timesel($pre,$h,$m) {
144
145         $o = '';
146         $o .= "<select name=\"{$pre}hour\" class=\"{$pre}hour\" size=\"1\">";
147         for($x = 0; $x < 24; $x ++) {
148                 $sel = (($x == $h) ? " selected=\"selected\" " : "");
149                 $o .= "<option value=\"$x\" $sel>$x</option>";
150         }
151         $o .= "</select> : <select name=\"{$pre}minute\" class=\"{$pre}minute\" size=\"1\">";
152         for($x = 0; $x < 60; $x ++) {
153                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
154                 $o .= "<option value=\"$x\" $sel>$x</option>";
155         }
156
157         $o .= "</select>";
158         return $o;
159 }}
160
161
162
163
164
165
166
167
168 // implements "3 seconds ago" etc.
169 // based on $posted_date, (UTC).
170 // Results relative to current timezone
171 // Limited to range of timestamps
172
173 if(! function_exists('relative_date')) {
174 function relative_date($posted_date) {
175
176         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
177
178         $abs = strtotime($localtime);
179         $etime = time() - $abs;
180     
181         if ($etime < 1) {
182                 return t('less than a second ago');
183         }
184     
185         $a = array( 12 * 30 * 24 * 60 * 60  =>  array( t('year'),   t('years')),
186                     30 * 24 * 60 * 60       =>  array( t('month'),  t('months')),
187                     7  * 24 * 60 * 60       =>  array( t('week'),   t('weeks')),
188                     24 * 60 * 60            =>  array( t('day'),    t('days')),
189                     60 * 60                 =>  array( t('hour'),   t('hours')),
190                     60                      =>  array( t('minute'), t('minutes')),
191                     1                       =>  array( t('second'), t('seconds'))
192         );
193     
194         foreach ($a as $secs => $str) {
195         $d = $etime / $secs;
196         if ($d >= 1) {
197                 $r = round($d);
198                 return $r . ' ' . (($r == 1) ? $str[0] : $str[1]) . t(' ago');
199         }
200     }
201 }}
202
203
204
205 // Returns age in years, given a date of birth,
206 // the timezone of the person whose date of birth is provided,
207 // and the timezone of the person viewing the result.
208 // Why? Bear with me. Let's say I live in Mittagong, Australia, and my 
209 // birthday is on New Year's. You live in San Bruno, California.
210 // When exactly are you going to see my age increase?
211 // A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start 
212 // celebrating and become a year older. If you wish me happy birthday 
213 // on January 1 (San Bruno time), you'll be a day late. 
214    
215 function age($dob,$owner_tz = '',$viewer_tz = '') {
216         if(! intval($dob))
217                 return 0;
218         if(! $owner_tz)
219                 $owner_tz = date_default_timezone_get();
220         if(! $viewer_tz)
221                 $viewer_tz = date_default_timezone_get();
222
223         $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
224         list($year,$month,$day) = explode("-",$birthdate);
225         $year_diff  = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
226         $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
227         $curr_day   = datetime_convert('UTC',$viewer_tz,'now','d');
228
229         if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
230                 $year_diff--;
231         return $year_diff;
232 }
233
234
235
236 // Get days in month
237 // get_dim($year, $month);
238 // returns number of days.
239 // $month[1] = 'January'; 
240 //   to match human usage.
241
242 if(! function_exists('get_dim')) {
243 function get_dim($y,$m) {
244
245   $dim = array( 0,
246     31, 28, 31, 30, 31, 30,
247     31, 31, 30, 31, 30, 31);
248  
249   if($m != 2)
250     return $dim[$m];
251   if(((($y % 4) == 0) && (($y % 100) != 0)) || (($y % 400) == 0))
252     return 29;
253   return $dim[2];
254 }}
255
256
257 // Returns the first day in month for a given month, year
258 // get_first_dim($year,$month)
259 // returns 0 = Sunday through 6 = Saturday
260 // Months start at 1.
261
262 if(! function_exists('get_first_dim')) {
263 function get_first_dim($y,$m) {
264   $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
265   return datetime_convert('UTC','UTC',$d,'w');
266 }}
267
268 // output a calendar for the given month, year.
269 // if $links are provided (array), e.g. $links[12] => 'http://mylink' , 
270 // date 12 will be linked appropriately. Today's date is also noted by 
271 // altering td class.
272 // Months count from 1.
273
274
275 // TODO: provide (prev,next) links, define class variations for different size calendars
276
277
278 if(! function_exists('cal')) {
279 function cal($y = 0,$m = 0, $links = false) {
280
281
282         // month table - start at 1 to match human usage.
283
284         $mtab = array(' ',
285           'January','February','March',
286           'April','May','June',
287           'July','August','September',
288           'October','November','December'
289         ); 
290
291         $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
292         $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
293         if(! $y)
294                 $y = $thisyear;
295         if(! $m)
296                 $m = intval($thismonth);
297
298   $dn = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
299   $f = get_first_dim($y,$m);
300   $l = get_dim($y,$m);
301   $d = 1;
302   $dow = 0;
303   $started = false;
304
305   if(($y == $thisyear) && ($m == $thismonth))
306     $tddate = intval(datetime_convert('UTC',date_default_timezone_get(),'now','j'));
307
308         $str_month = day_translate($mtab[$m]);
309   $o = '<table class="calendar">';
310   $o .= "<caption>$str_month $y</caption><tr>";
311   for($a = 0; $a < 7; $a ++)
312      $o .= '<th>' . mb_substr(day_translate($dn[$a]),0,3,'UTF-8') . '</th>';
313   $o .= '</tr><tr>';
314
315   while($d <= $l) {
316     if(($dow == $f) && (! $started))
317       $started = true;
318     $today = (((isset($tddate)) && ($tddate == $d)) ? "class=\"today\" " : '');
319     $o .= "<td $today>";
320         $day = str_replace(' ','&nbsp;',sprintf('%2.2d', $d));
321     if($started) {
322       if(is_array($links) && isset($links[$d]))
323         $o .=  "<a href=\"{$links[$d]}\">$day</a>";
324       else
325         $o .= $day;
326       $d ++;
327     }
328     else
329       $o .= '&nbsp;';
330     $o .= '</td>';
331     $dow ++;
332     if(($dow == 7) && ($d <= $l)) {
333       $dow = 0;
334       $o .= '</tr><tr>';
335     }
336   }
337   if($dow)
338     for($a = $dow; $a < 7; $a ++)
339        $o .= '<td>&nbsp;</td>';
340   $o .= '</tr></table>'."\r\n";  
341   
342   return $o;
343 }}