Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

348 lines
9.8 KiB

11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
11 years ago
  1. <?php
  2. // two-level sort for timezones.
  3. if(! function_exists('timezone_cmp')) {
  4. function timezone_cmp($a, $b) {
  5. if(strstr($a,'/') && strstr($b,'/')) {
  6. if ( t($a) == t($b)) return 0;
  7. return ( t($a) < t($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. // emit a timezone selector grouped (primarily) by continent
  15. if(! function_exists('select_timezone')) {
  16. function select_timezone($current = 'America/Los_Angeles') {
  17. $timezone_identifiers = DateTimeZone::listIdentifiers();
  18. $o ='<select id="timezone_select" name="timezone">';
  19. usort($timezone_identifiers, 'timezone_cmp');
  20. $continent = '';
  21. foreach($timezone_identifiers as $value) {
  22. $ex = explode("/", $value);
  23. if(count($ex) > 1) {
  24. if($ex[0] != $continent) {
  25. if($continent != '')
  26. $o .= '</optgroup>';
  27. $continent = $ex[0];
  28. $o .= '<optgroup label="' . t($continent) . '">';
  29. }
  30. if(count($ex) > 2)
  31. $city = substr($value,strpos($value,'/')+1);
  32. else
  33. $city = $ex[1];
  34. }
  35. else {
  36. $city = $ex[0];
  37. if($continent != t('Miscellaneous')) {
  38. $o .= '</optgroup>';
  39. $continent = t('Miscellaneous');
  40. $o .= '<optgroup label="' . t($continent) . '">';
  41. }
  42. }
  43. $city = str_replace('_', ' ', t($city));
  44. $selected = (($value == $current) ? " selected=\"selected\" " : "");
  45. $o .= "<option value=\"$value\" $selected >$city</option>";
  46. }
  47. $o .= '</optgroup></select>';
  48. return $o;
  49. }}
  50. // General purpose date parse/convert function.
  51. // $from = source timezone
  52. // $to = dest timezone
  53. // $s = some parseable date/time string
  54. // $fmt = output format
  55. if(! function_exists('datetime_convert')) {
  56. function datetime_convert($from = 'UTC', $to = 'UTC', $s = 'now', $fmt = "Y-m-d H:i:s") {
  57. // Slight hackish adjustment so that 'zero' datetime actually returns what is intended
  58. // otherwise we end up with -0001-11-30 ...
  59. // add 32 days so that we at least get year 00, and then hack around the fact that
  60. // months and days always start with 1.
  61. if(substr($s,0,10) == '0000-00-00') {
  62. $d = new DateTime($s . ' + 32 days', new DateTimeZone('UTC'));
  63. return str_replace('1','0',$d->format($fmt));
  64. }
  65. $d = new DateTime($s, new DateTimeZone($from));
  66. $d->setTimeZone(new DateTimeZone($to));
  67. return($d->format($fmt));
  68. }}
  69. // wrapper for date selector, tailored for use in birthday fields
  70. function dob($dob) {
  71. list($year,$month,$day) = sscanf($dob,'%4d-%2d-%2d');
  72. $y = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  73. $o = datesel('',1920,$y,true,$year,$month,$day);
  74. return $o;
  75. }
  76. // returns a date selector.
  77. // $pre = prefix (if needed) for HTML name and class fields
  78. // $ymin = first year shown in selector dropdown
  79. // $ymax = last year shown in selector dropdown
  80. // $allow_blank = allow an empty response on any field
  81. // $y = already selected year
  82. // $m = already selected month
  83. // $d = already selected day
  84. if(! function_exists('datesel')) {
  85. function datesel($pre,$ymin,$ymax,$allow_blank,$y,$m,$d) {
  86. $o = '';
  87. $o .= "<select name=\"{$pre}year\" class=\"{$pre}year\" size=\"1\">";
  88. if($allow_blank) {
  89. $sel = (($y == '0000') ? " selected=\"selected\" " : "");
  90. $o .= "<option value=\"0000\" $sel ></option>";
  91. }
  92. if($ymax > $ymin) {
  93. for($x = $ymax; $x >= $ymin; $x --) {
  94. $sel = (($x == $y) ? " selected=\"selected\" " : "");
  95. $o .= "<option value=\"$x\" $sel>$x</option>";
  96. }
  97. }
  98. else {
  99. for($x = $ymax; $x <= $ymin; $x ++) {
  100. $sel = (($x == $y) ? " selected=\"selected\" " : "");
  101. $o .= "<option value=\"$x\" $sel>$x</option>";
  102. }
  103. }
  104. $o .= "</select> <select name=\"{$pre}month\" class=\"{$pre}month\" size=\"1\">";
  105. for($x = (($allow_blank) ? 0 : 1); $x <= 12; $x ++) {
  106. $sel = (($x == $m) ? " selected=\"selected\" " : "");
  107. $y = (($x) ? $x : '');
  108. $o .= "<option value=\"$x\" $sel>$y</option>";
  109. }
  110. $o .= "</select> <select name=\"{$pre}day\" class=\"{$pre}day\" size=\"1\">";
  111. for($x = (($allow_blank) ? 0 : 1); $x <= 31; $x ++) {
  112. $sel = (($x == $d) ? " selected=\"selected\" " : "");
  113. $y = (($x) ? $x : '');
  114. $o .= "<option value=\"$x\" $sel>$y</option>";
  115. }
  116. $o .= "</select>";
  117. return $o;
  118. }}
  119. if(! function_exists('timesel')) {
  120. function timesel($pre,$h,$m) {
  121. $o = '';
  122. $o .= "<select name=\"{$pre}hour\" class=\"{$pre}hour\" size=\"1\">";
  123. for($x = 0; $x < 24; $x ++) {
  124. $sel = (($x == $h) ? " selected=\"selected\" " : "");
  125. $o .= "<option value=\"$x\" $sel>$x</option>";
  126. }
  127. $o .= "</select> : <select name=\"{$pre}minute\" class=\"{$pre}minute\" size=\"1\">";
  128. for($x = 0; $x < 60; $x ++) {
  129. $sel = (($x == $m) ? " selected=\"selected\" " : "");
  130. $o .= "<option value=\"$x\" $sel>$x</option>";
  131. }
  132. $o .= "</select>";
  133. return $o;
  134. }}
  135. // implements "3 seconds ago" etc.
  136. // based on $posted_date, (UTC).
  137. // Results relative to current timezone
  138. // Limited to range of timestamps
  139. if(! function_exists('relative_date')) {
  140. function relative_date($posted_date) {
  141. $localtime = datetime_convert('UTC',date_default_timezone_get(),$posted_date);
  142. $abs = strtotime($localtime);
  143. if (is_null($posted_date) || $posted_date === '0000-00-00 00:00:00' || $abs === False) {
  144. return t('never');
  145. }
  146. $etime = time() - $abs;
  147. if ($etime < 1) {
  148. return t('less than a second ago');
  149. }
  150. $a = array( 12 * 30 * 24 * 60 * 60 => array( t('year'), t('years')),
  151. 30 * 24 * 60 * 60 => array( t('month'), t('months')),
  152. 7 * 24 * 60 * 60 => array( t('week'), t('weeks')),
  153. 24 * 60 * 60 => array( t('day'), t('days')),
  154. 60 * 60 => array( t('hour'), t('hours')),
  155. 60 => array( t('minute'), t('minutes')),
  156. 1 => array( t('second'), t('seconds'))
  157. );
  158. foreach ($a as $secs => $str) {
  159. $d = $etime / $secs;
  160. if ($d >= 1) {
  161. $r = round($d);
  162. return $r . ' ' . (($r == 1) ? $str[0] : $str[1]) . t(' ago');
  163. }
  164. }
  165. }}
  166. // Returns age in years, given a date of birth,
  167. // the timezone of the person whose date of birth is provided,
  168. // and the timezone of the person viewing the result.
  169. // Why? Bear with me. Let's say I live in Mittagong, Australia, and my
  170. // birthday is on New Year's. You live in San Bruno, California.
  171. // When exactly are you going to see my age increase?
  172. // A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start
  173. // celebrating and become a year older. If you wish me happy birthday
  174. // on January 1 (San Bruno time), you'll be a day late.
  175. function age($dob,$owner_tz = '',$viewer_tz = '') {
  176. if(! intval($dob))
  177. return 0;
  178. if(! $owner_tz)
  179. $owner_tz = date_default_timezone_get();
  180. if(! $viewer_tz)
  181. $viewer_tz = date_default_timezone_get();
  182. $birthdate = datetime_convert('UTC',$owner_tz,$dob . ' 00:00:00+00:00','Y-m-d');
  183. list($year,$month,$day) = explode("-",$birthdate);
  184. $year_diff = datetime_convert('UTC',$viewer_tz,'now','Y') - $year;
  185. $curr_month = datetime_convert('UTC',$viewer_tz,'now','m');
  186. $curr_day = datetime_convert('UTC',$viewer_tz,'now','d');
  187. if(($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day)))
  188. $year_diff--;
  189. return $year_diff;
  190. }
  191. // Get days in month
  192. // get_dim($year, $month);
  193. // returns number of days.
  194. // $month[1] = 'January';
  195. // to match human usage.
  196. if(! function_exists('get_dim')) {
  197. function get_dim($y,$m) {
  198. $dim = array( 0,
  199. 31, 28, 31, 30, 31, 30,
  200. 31, 31, 30, 31, 30, 31);
  201. if($m != 2)
  202. return $dim[$m];
  203. if(((($y % 4) == 0) && (($y % 100) != 0)) || (($y % 400) == 0))
  204. return 29;
  205. return $dim[2];
  206. }}
  207. // Returns the first day in month for a given month, year
  208. // get_first_dim($year,$month)
  209. // returns 0 = Sunday through 6 = Saturday
  210. // Months start at 1.
  211. if(! function_exists('get_first_dim')) {
  212. function get_first_dim($y,$m) {
  213. $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
  214. return datetime_convert('UTC','UTC',$d,'w');
  215. }}
  216. // output a calendar for the given month, year.
  217. // if $links are provided (array), e.g. $links[12] => 'http://mylink' ,
  218. // date 12 will be linked appropriately. Today's date is also noted by
  219. // altering td class.
  220. // Months count from 1.
  221. // TODO: provide (prev,next) links, define class variations for different size calendars
  222. if(! function_exists('cal')) {
  223. function cal($y = 0,$m = 0, $links = false, $class='') {
  224. // month table - start at 1 to match human usage.
  225. $mtab = array(' ',
  226. 'January','February','March',
  227. 'April','May','June',
  228. 'July','August','September',
  229. 'October','November','December'
  230. );
  231. $thisyear = datetime_convert('UTC',date_default_timezone_get(),'now','Y');
  232. $thismonth = datetime_convert('UTC',date_default_timezone_get(),'now','m');
  233. if(! $y)
  234. $y = $thisyear;
  235. if(! $m)
  236. $m = intval($thismonth);
  237. $dn = array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday');
  238. $f = get_first_dim($y,$m);
  239. $l = get_dim($y,$m);
  240. $d = 1;
  241. $dow = 0;
  242. $started = false;
  243. if(($y == $thisyear) && ($m == $thismonth))
  244. $tddate = intval(datetime_convert('UTC',date_default_timezone_get(),'now','j'));
  245. $str_month = day_translate($mtab[$m]);
  246. $o = '<table class="calendar' . $class . '">';
  247. $o .= "<caption>$str_month $y</caption><tr>";
  248. for($a = 0; $a < 7; $a ++)
  249. $o .= '<th>' . mb_substr(day_translate($dn[$a]),0,3,'UTF-8') . '</th>';
  250. $o .= '</tr><tr>';
  251. while($d <= $l) {
  252. if(($dow == $f) && (! $started))
  253. $started = true;
  254. $today = (((isset($tddate)) && ($tddate == $d)) ? "class=\"today\" " : '');
  255. $o .= "<td $today>";
  256. $day = str_replace(' ','&nbsp;',sprintf('%2.2d', $d));
  257. if($started) {
  258. if(is_array($links) && isset($links[$d]))
  259. $o .= "<a href=\"{$links[$d]}\">$day</a>";
  260. else
  261. $o .= $day;
  262. $d ++;
  263. }
  264. else
  265. $o .= '&nbsp;';
  266. $o .= '</td>';
  267. $dow ++;
  268. if(($dow == 7) && ($d <= $l)) {
  269. $dow = 0;
  270. $o .= '</tr><tr>';
  271. }
  272. }
  273. if($dow)
  274. for($a = $dow; $a < 7; $a ++)
  275. $o .= '<td>&nbsp;</td>';
  276. $o .= '</tr></table>'."\r\n";
  277. return $o;
  278. }}