]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
mistpark 2.0 infrasturcture lands
[friendica.git] / include / datetime.php
1 <?php
2
3 if(! function_exists('timezone_cmp')) {
4 function timezone_cmp($a, $b) {
5         if(strstr($a,'/') && strstr($b,'/')) {
6                 if ($a == $b) return 0;
7                 return ($a < $b) ? -1 : 1;
8         }
9         if(strstr($a,'/')) return -1;
10         if(strstr($b,'/')) return  1;
11         if ( t($a) == t($b)) return 0;
12         return ( t($a) < t($b)) ? -1 : 1;
13 }}
14
15
16 if(! function_exists('select_timezone')) {
17 function select_timezone($current = 'America/Los_Angeles') {
18
19         $timezone_identifiers = DateTimeZone::listIdentifiers();
20         
21         $o ='<select id="timezone_select" name="timezone">';
22
23         usort($timezone_identifiers, 'timezone_cmp');
24         $continent = '';
25         foreach($timezone_identifiers as $value) {
26                 $ex = explode("/", $value);
27                 if(count($ex) > 1) {
28                         if($ex[0] != $continent) {
29                                 if($continent != '')
30                                         $o .= '</optgroup>';
31                                 $continent = $ex[0];
32                                 $o .= '<optgroup label="' . t($continent) . '">';
33                         }
34                         if(count($ex) > 2)
35                                 $city = substr($value,strpos($value,'/')+1);
36                         else
37                                 $city = $ex[1];
38                 }
39                 else {
40                         $city = $ex[0];
41                         if($continent != 'Miscellaneous') {
42                                 $o .= '</optgroup>';
43                                 $continent = 'Miscellaneous';
44                                 $o .= '<optgroup label="' . t($continent) . '">';       
45                         }
46                 }
47                 $city = str_replace('_', ' ',  t($city));
48                 $selected = (($value == $current) ? " selected=\"selected\" " : "");
49                 $o .= "<option value=\"$value\" $selected >$city</option>";
50         }    
51         $o .= '</optgroup></select>';
52         return $o;
53 }}
54
55
56 if(! function_exists('datetime_convert')) {
57 function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d H:i:s") {
58
59         // Slight hackish adjustment so that 'zero' datetime actually returns what is intended
60         // otherwise we end up with -0001-11-30 ...
61         // add 32 days so that we at least get year 00, and then hack around the fact that 
62         // months and days always start with 1. 
63
64         if(substr($s,0,10) == '0000-00-00') {
65                 $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
66                 return str_replace('1','0',$d->format($fmt));
67         }
68
69         $d = new DateTime($s, new DateTimeZone($from));
70         $d->setTimeZone(new DateTimeZone($to));
71         return($d->format($fmt));
72 }}
73
74 function dob($dob) {
75         list($year,$month,$day) = sscanf($dob,'%4d-%2d-%2d');
76         $y = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
77         $o = datesel('',1920,$y,true,$year,$month,$day);
78         return $o;
79 }
80
81 if(! function_exists('datesel')) {
82 function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
83
84         $o = '';
85         $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
86         if($allow_blank) {
87                 $sel = (($y == '0000') ? " selected=\"selected\" " : "");
88                 $o .= "<option value=\"0000\" $sel ></option>";
89         }
90
91         for($x = $ymax; $x >= $ymin; $x --) {
92                 $sel = (($x == $y) ? " selected=\"selected\" " : "");
93                 $o .= "<option value=\"$x\" $sel>$x</option>";
94         }
95   
96         $o .= "</select> <select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
97         for($x = 0; $x <= 12; $x ++) {
98                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
99                 $y = (($x) ? $x : '');
100                 $o .= "<option value=\"$x\" $sel>$y</option>";
101         }
102
103         $o .= "</select> <select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
104         for($x = 0; $x <= 31; $x ++) {
105                 $sel = (($x == $d) ? " selected=\"selected\" " : "");
106                 $y = (($x) ? $x : '');
107                 $o .= "<option value=\"$x\" $sel>$y</option>";
108         }
109
110         $o .= "</select>";
111         return $o;
112 }}
113
114
115 if(! function_exists('relative_date')) {
116 function relative_date($posted_date) {
117
118         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
119
120         $abs = strtotime($localtime);
121         $etime = time() - $abs;
122     
123         if ($etime < 1) {
124                 return t('less than a second ago');
125         }
126     
127         $a = array( 12 * 30 * 24 * 60 * 60  =>  array( t('year'),   t('years')),
128                     30 * 24 * 60 * 60       =>  array( t('month'),  t('months')),
129                     7  * 24 * 60 * 60       =>  array( t('week'),   t('weeks')),
130                     24 * 60 * 60            =>  array( t('day'),    t('days')),
131                     60 * 60                 =>  array( t('hour'),   t('hours')),
132                     60                      =>  array( t('minute'), t('minutes')),
133                     1                       =>  array( t('second'), t('seconds'))
134         );
135     
136         foreach ($a as $secs => $str) {
137         $d = $etime / $secs;
138         if ($d >= 1) {
139                 $r = round($d);
140                 return $r . ' ' . (($r == 1) ? $str[0] : $str[1]) . t(' ago');
141         }
142     }
143 }}
144
145
146 function age($dob,$owner_tz = '',$viewer_tz = '') {
147         if(! intval($dob))
148                 return 0;
149         if(! $owner_tz)
150                 $owner_tz = date_default_timezone_get();
151         if(! $viewer_tz)
152                 $viewer_tz = date_default_timezone_get();
153
154         $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
155         list($year,$month,$day) = explode("-",$birthdate);
156         $year_diff  = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
157         $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
158         $curr_day   = datetime_convert('UTC',$viewer_tz,'now','d');
159
160         if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
161                 $year_diff--;
162         return $year_diff;
163 }