]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
profile additions
[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 ($a == $b) return 0;
12         return ($a < $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=\"$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=\"$continent\">";        
45                         }
46                 }
47                 $city = str_replace('_', ' ', $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 // TODO rewrite this buggy sucker
105 function relative_date($posted_date) {
106
107         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
108     
109         $in_seconds = strtotime($localtime);
110
111         $diff = time() - $in_seconds;
112     
113         $months = floor($diff/2592000);
114         $diff -= $months*2419200;
115         $weeks = floor($diff/604800);
116         $diff -= $weeks*604800;
117         $days = floor($diff/86400);
118         $diff -= $days*86400;
119         $hours = floor($diff/3600);
120         $diff -= $hours*3600;
121         $minutes = floor($diff/60);
122         $diff -= $minutes*60;
123         $seconds = $diff;
124
125         if($months > 2)
126                 return(datetime_convert('UTC',date_default_timezone_get(),$posted_date,'\o\n Y-m-d \a\t H:i:s'));
127     if ($months>0) {
128         // over a month old,
129         return 'over a month ago';
130     } else {
131         if ($weeks>0) {
132             // weeks and days
133             $relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks!=1 ?'s':'');
134
135         } elseif ($days>0) {
136             // days and hours
137             $relative_date .= ($relative_date?', ':'').$days.' day'.($days!=1?'s':'');
138
139         } elseif ($hours>0) {
140             // hours and minutes
141             $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours!=1?'s':'');
142
143         } elseif ($minutes>0) {
144             // minutes only
145             $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes!=1?'s':'');
146         } else {
147             // seconds only
148             $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds!=1?'s':'');
149         }
150     }
151     // show relative date and add proper verbiage
152     return $relative_date.' ago';
153 }
154
155 function age($dob,$owner_tz = '',$viewer_tz = '') {
156         if(strlen($dob) != 10)
157                 return 0;
158         if(! $owner_tz)
159                 $owner_tz = date_default_timezone_get();
160         if(! $viewer_tz)
161                 $viewer_tz = date_default_timezone_get();
162
163         $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
164         list($year,$month,$day) = explode("-",$birthdate);
165         $year_diff  = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
166         $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
167         $curr_day   = datetime_convert('UTC',$viewer_tz,'now','d');
168
169         if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
170                 $year_diff--;
171         return $year_diff;
172 }