]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
installer changes, pe sync
[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   $d = new DateTime($s, new DateTimeZone($from));
59   $d->setTimeZone(new DateTimeZone($to));
60   return($d->format($fmt));
61 }}
62
63 function dob($dob) {
64         list($year,$month,$day) = sscanf($dob,'%4d-%2d-%2d');
65         $y = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
66         $o = datesel('',1920,$y,true,$year,$month,$day);
67         return $o;
68 }
69
70 if(! function_exists('datesel')) {
71 function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
72
73         $o = '';
74         $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
75         if($allow_blank) {
76                 $sel = (($y == '0000') ? " selected=\"selected\" " : "");
77                 $o .= "<option value=\"0000\" $sel ></option>";
78         }
79
80         for($x = $ymax; $x >= $ymin; $x --) {
81                 $sel = (($x == $y) ? " selected=\"selected\" " : "");
82                 $o .= "<option value=\"$x\" $sel>$x</option>";
83         }
84   
85         $o .= "</select> <select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
86         for($x = 0; $x <= 12; $x ++) {
87                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
88                 $y = (($x) ? $x : '');
89                 $o .= "<option value=\"$x\" $sel>$y</option>";
90         }
91
92         $o .= "</select> <select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
93         for($x = 0; $x <= 31; $x ++) {
94                 $sel = (($x == $d) ? " selected=\"selected\" " : "");
95                 $y = (($x) ? $x : '');
96                 $o .= "<option value=\"$x\" $sel>$y</option>";
97         }
98
99         $o .= "</select>";
100         return $o;
101 }}
102
103
104 if(! function_exists('relative_date')) {
105 function relative_date($posted_date) {
106
107         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
108
109         $abs = strtotime($localtime);
110         $etime = time() - $abs;
111     
112         if ($etime < 1) {
113                 return t('less than a second ago');
114         }
115     
116         $a = array( 12 * 30 * 24 * 60 * 60  =>  array( t('year'),   t('years')),
117                     30 * 24 * 60 * 60       =>  array( t('month'),  t('months')),
118                     7  * 24 * 60 * 60       =>  array( t('week'),   t('weeks')),
119                     24 * 60 * 60            =>  array( t('day'),    t('days')),
120                     60 * 60                 =>  array( t('hour'),   t('hours')),
121                     60                      =>  array( t('minute'), t('minutes')),
122                     1                       =>  array( t('second'), t('seconds'))
123         );
124     
125         foreach ($a as $secs => $str) {
126         $d = $etime / $secs;
127         if ($d >= 1) {
128                 $r = round($d);
129                 return $r . ' ' . (($r == 1) ? $str[0] : $str[1]) . t(' ago');
130         }
131     }
132 }}
133
134
135 function age($dob,$owner_tz = '',$viewer_tz = '') {
136         if(! intval($dob))
137                 return 0;
138         if(! $owner_tz)
139                 $owner_tz = date_default_timezone_get();
140         if(! $viewer_tz)
141                 $viewer_tz = date_default_timezone_get();
142
143         $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
144         list($year,$month,$day) = explode("-",$birthdate);
145         $year_diff  = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
146         $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
147         $curr_day   = datetime_convert('UTC',$viewer_tz,'now','d');
148
149         if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
150                 $year_diff--;
151         return $year_diff;
152 }