]> git.mxchange.org Git - friendica.git/blob - include/datetime.php
Initial checkin
[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
64
65 if(! function_exists('datesel')) {
66 function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
67
68         $o = '';
69         $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
70         if($allow_blank) {
71                 $sel = (($y == '') ? " selected=\"selected\" " : "");
72                 $o .= "<option value=\"\" $sel></option>";
73         }
74
75         for($x = $ymin; $x <= $ymax; $x ++) {
76                 $sel = (($x == $y) ? " selected=\"selected\" " : "");
77                 $o .= "<option value=\"$x\" $sel>$x</option>";
78         }
79   
80         $o .= "</select>-<select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
81         for($x = 1; $x <= 12; $x ++) {
82                 $sel = (($x == $m) ? " selected=\"selected\" " : "");
83                 $o .= "<option value=\"$x\" $sel>$x</option>";
84         }
85
86         $o .= "</select>-<select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
87         for($x = 1; $x <= 31; $x ++) {
88                 $sel = (($x == $d) ? " selected=\"selected\" " : "");
89                 $o .= "<option value=\"$x\" $sel>$x</option>";
90         }
91
92         $o .= "</select>";
93         return $o;
94 }}
95
96
97 // TODO rewrite this buggy sucker
98 function relative_date($posted_date) {
99
100         $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date); 
101     
102         $in_seconds = strtotime($localtime);
103
104         $diff = time() - $in_seconds;
105     
106         $months = floor($diff/2592000);
107         $diff -= $months*2419200;
108         $weeks = floor($diff/604800);
109         $diff -= $weeks*604800;
110         $days = floor($diff/86400);
111         $diff -= $days*86400;
112         $hours = floor($diff/3600);
113         $diff -= $hours*3600;
114         $minutes = floor($diff/60);
115         $diff -= $minutes*60;
116         $seconds = $diff;
117
118
119     if ($months>0) {
120         // over a month old,
121         return 'over a month ago';
122     } else {
123         if ($weeks>0) {
124             // weeks and days
125             $relative_date .= ($relative_date?', ':'').$weeks.' week'.($weeks!=1 ?'s':'');
126
127         } elseif ($days>0) {
128             // days and hours
129             $relative_date .= ($relative_date?', ':'').$days.' day'.($days!=1?'s':'');
130
131         } elseif ($hours>0) {
132             // hours and minutes
133             $relative_date .= ($relative_date?', ':'').$hours.' hour'.($hours!=1?'s':'');
134
135         } elseif ($minutes>0) {
136             // minutes only
137             $relative_date .= ($relative_date?', ':'').$minutes.' minute'.($minutes!=1?'s':'');
138         } else {
139             // seconds only
140             $relative_date .= ($relative_date?', ':'').$seconds.' second'.($seconds!=1?'s':'');
141         }
142     }
143     // show relative date and add proper verbiage
144     return $relative_date.' ago';
145 }