PageRenderTime 33ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/phpical2/functions/date_functions.php

https://gitlab.com/endomorphosis/fusenews
PHP | 302 lines | 217 code | 23 blank | 62 comment | 39 complexity | 6134f50140847c7c9f532322b2c89ab6 MD5 | raw file
  1. <?php
  2. require_once(BASE."functions/is_daylight.php");
  3. // date_functions.php
  4. // functions for returning or comparing dates
  5. // takes iCalendar 2 day format and makes it into 3 characters
  6. // if $txt is true, it returns the 3 letters, otherwise it returns the
  7. // integer of that day; 0=Sun, 1=Mon, etc.
  8. function two2threeCharDays($day, $txt=true) {
  9. switch($day) {
  10. case 'SU': return ($txt ? 'sun' : '0');
  11. case 'MO': return ($txt ? 'mon' : '1');
  12. case 'TU': return ($txt ? 'tue' : '2');
  13. case 'WE': return ($txt ? 'wed' : '3');
  14. case 'TH': return ($txt ? 'thu' : '4');
  15. case 'FR': return ($txt ? 'fri' : '5');
  16. case 'SA': return ($txt ? 'sat' : '6');
  17. }
  18. }
  19. // dateOfWeek() takes a date in Ymd and a day of week in 3 letters or more
  20. // and returns the date of that day. (ie: "sun" or "sunday" would be acceptable values of $day but not "su")
  21. function dateOfWeek($Ymd, $day) {
  22. global $phpiCal_config;
  23. $week_start_day = 'Sunday';
  24. if (isset($phpiCal_config->week_start_day)) $week_start_day = $phpiCal_config->week_start_day;
  25. $timestamp = strtotime($Ymd);
  26. $num = date('w', strtotime($week_start_day));
  27. $start_day_time = strtotime((date('w',$timestamp)==$num ? "$week_start_day" : "last $week_start_day"), $timestamp);
  28. $ret_unixtime = strtotime($day,$start_day_time);
  29. // Fix for 992744
  30. // $ret_unixtime = strtotime('+12 hours', $ret_unixtime);
  31. $ret_unixtime += (12 * 60 * 60);
  32. $ret = date('Ymd',$ret_unixtime);
  33. return $ret;
  34. }
  35. // function to compare to dates in Ymd and return the number of weeks
  36. // that differ between them. requires dateOfWeek()
  37. function weekCompare($now, $then) {
  38. global $week_start_day;
  39. $sun_now = dateOfWeek($now, "Sunday");
  40. $sun_then = dateOfWeek($then, "Sunday");
  41. $seconds_now = strtotime($sun_now);
  42. $seconds_then = strtotime($sun_then);
  43. $diff_weeks = round(($seconds_now - $seconds_then)/(60*60*24*7));
  44. return $diff_weeks;
  45. }
  46. // function to compare to dates in Ymd and return the number of days
  47. // that differ between them.
  48. function dayCompare($now, $then) {
  49. $seconds_now = strtotime($now);
  50. $seconds_then = strtotime($then);
  51. $diff_seconds = $seconds_now - $seconds_then;
  52. $diff_minutes = $diff_seconds/60;
  53. $diff_hours = $diff_minutes/60;
  54. $diff_days = round($diff_hours/24);
  55. return $diff_days;
  56. }
  57. // function to compare to dates in Ymd and return the number of months
  58. // that differ between them.
  59. function monthCompare($now, $then) {
  60. ereg ("([0-9]{4})([0-9]{2})([0-9]{2})", $now, $date_now);
  61. ereg ("([0-9]{4})([0-9]{2})([0-9]{2})", $then, $date_then);
  62. $diff_years = $date_now[1] - $date_then[1];
  63. $diff_months = $date_now[2] - $date_then[2];
  64. if ($date_now[2] < $date_then[2]) {
  65. $diff_years -= 1;
  66. $diff_months = ($diff_months + 12) % 12;
  67. }
  68. $diff_months = ($diff_years * 12) + $diff_months;
  69. return $diff_months;
  70. }
  71. function yearCompare($now, $then) {
  72. ereg ("([0-9]{4})([0-9]{2})([0-9]{2})", $now, $date_now);
  73. ereg ("([0-9]{4})([0-9]{2})([0-9]{2})", $then, $date_then);
  74. $diff_years = $date_now[1] - $date_then[1];
  75. return $diff_years;
  76. }
  77. function noneCompare($now, $then) {
  78. return 0;
  79. }
  80. // localizeDate() - similar to strftime but uses our preset arrays of localized
  81. // months and week days and only supports %A, %a, %B, %b, %e, and %Y
  82. // more can be added as needed but trying to keep it small while we can
  83. function localizeDate($format, $timestamp) {
  84. global $daysofweek_lang, $daysofweekshort_lang, $daysofweekreallyshort_lang, $monthsofyear_lang, $monthsofyear_lang, $monthsofyearshort_lang;
  85. $year = date("Y", $timestamp);
  86. $month = date("n", $timestamp)-1;
  87. $day = date("j", $timestamp);
  88. $dayofweek = date("w", $timestamp);
  89. $weeknumber = date("W", $timestamp);
  90. $replacements = array(
  91. '%Y' => $year,
  92. '%e' => $day,
  93. '%B' => $monthsofyear_lang[$month],
  94. '%b' => $monthsofyearshort_lang[$month],
  95. '%A' => $daysofweek_lang[$dayofweek],
  96. '%a' => $daysofweekshort_lang[$dayofweek],
  97. '%W' => $weeknumber,
  98. '%d' => sprintf("%02d", $day)
  99. );
  100. $date = str_replace(array_keys($replacements), array_values($replacements), $format);
  101. return $date;
  102. }
  103. // calcOffset takes an offset (ie, -0500) and returns it in the number of seconds
  104. function calcOffset($offset_str) {
  105. $sign = substr($offset_str, 0, 1);
  106. $hours = substr($offset_str, 1, 2);
  107. $mins = substr($offset_str, 3, 2);
  108. $secs = ((int)$hours * 3600) + ((int)$mins * 60);
  109. if ($sign == '-') $secs = 0 - $secs;
  110. return $secs;
  111. }
  112. // calcTime calculates the unixtime of a new offset by comparing it to the current offset
  113. // $have is the current offset (ie, '-0500')
  114. // $want is the wanted offset (ie, '-0700')
  115. // $time is the unixtime relative to $have
  116. function calcTime($have, $want, $time) {
  117. if ($have == 'none' || $want == 'none') return $time;
  118. $have_secs = calcOffset($have);
  119. $want_secs = calcOffset($want);
  120. $diff = $want_secs - $have_secs;
  121. $time += $diff;
  122. return $time;
  123. }
  124. function chooseOffset($time, $timezone = '') {
  125. global $tz_array, $summary;
  126. switch ($timezone) {
  127. case '':
  128. $offset = 'none';
  129. break;
  130. case 'Same as Server':
  131. $offset = date('O', $time);
  132. break;
  133. default:
  134. if (is_array($tz_array) && array_key_exists($timezone, $tz_array)) {
  135. $dlst = is_daylight($time, $timezone);
  136. $offset = $tz_array[$timezone][$dlst];
  137. } else {
  138. $offset = '+0000';
  139. }
  140. }
  141. return $offset;
  142. }
  143. /* Returns a string to make event text with a link to popup boxes
  144. $arr is a master array item
  145. $lines is the number of lines to restrict the event_text to, using word_wrap
  146. $length is the length of one line
  147. $link_class is a css class
  148. $pre_text and $post_text are to add tags around the link text (e.g. <b> or<i>)
  149. $title is the tooltip for the link
  150. */
  151. function openevent($event_date, $time, $uid, $arr, $lines = 0, $length = 0, $link_class = '', $pre_text = '', $post_text = '') {
  152. global $cpath, $timeFormat, $dateFormat_week;
  153. $return = '';
  154. $event_text = stripslashes(urldecode($arr["event_text"]));
  155. # build tooltip
  156. if ($time == -1) {
  157. $start = localizeDate($dateFormat_week, $arr['start_unixtime']);
  158. $end = localizeDate($dateFormat_week, ($arr['end_unixtime'] - 60));
  159. $title = $event_text;
  160. if ($start != $end) $title = "$start - $end $event_text";
  161. } else {
  162. $start = date($timeFormat, $arr['start_unixtime']);
  163. $end = date($timeFormat, $arr['end_unixtime']);
  164. $title = "$start: $event_text";
  165. if ($start != $end) $title = "$start - $end $event_text";
  166. }
  167. $title .= "\n".urldecode($arr['description'])."\n".urldecode($arr['location']);
  168. $title = trim($title);
  169. # for iCal pseudo tag <http> comptability
  170. if (ereg("<([[:alpha:]]+://)([^<>[:space:]]+)>",$event_text,$matches)) {
  171. $full_event_text = $matches[1] . $matches[2];
  172. $event_text = $matches[2];
  173. } else {
  174. $full_event_text = $event_text;
  175. $event_text = strip_tags($event_text, '<b><i><u><img>');
  176. }
  177. if (!empty($event_text)) {
  178. if ($lines > 0) {
  179. $event_text = word_wrap($event_text, $length, $lines);
  180. }
  181. if ((!(ereg("([[:alpha:]]+://[^<>[:space:]]+)", $full_event_text, $res))) || ($arr['description'])) {
  182. $escaped_date = addslashes($event_date);
  183. $escaped_time = addslashes($time);
  184. $escaped_uid = addslashes($uid);
  185. $event_data = addslashes(serialize ($arr));
  186. // fix for URL-length bug in IE: populate and submit a hidden form on click
  187. static $popup_data_index = 0;
  188. $return = "
  189. <script language='Javascript' type='text/javascript'><!--
  190. var eventData = new EventData('$escaped_date', '$escaped_time', '$escaped_uid','$cpath','$event_data');
  191. document.popup_data[$popup_data_index] = eventData;
  192. // --></script>";
  193. $return .= '<a class="'.$link_class.'" title="'.$title.'" href="#" onclick="openEventWindow('.$popup_data_index.'); return false;">';
  194. $popup_data_index++;
  195. } else {
  196. $return .= '<a class="'.$link_class.'" title="'.$title.'" href="'.$res[1].'">';
  197. }
  198. $return .= $pre_text.$event_text.$post_text.'</a>'."\n";
  199. }
  200. return $return;
  201. }
  202. /* Returns an array of the date and time extracted from the data
  203. passed in. This array contains (unixtime, date, time, allday, tzid).
  204. $data = A string representing a date-time per RFC2445.
  205. $property = The property being examined, e.g. DTSTART, DTEND.
  206. $field = The full field being examined, e.g. DTSTART;TZID=US/Pacific
  207. See:http://phpicalendar.org/documentation/index.php/Property_Value_Data_Types#4.3.5___Date-Time
  208. */
  209. function extractDateTime($data, $property, $field) {
  210. global $tz_array, $phpiCal_config, $calendar_tz;
  211. $allday =''; #suppress error on returning undef.
  212. // Check for zulu time.
  213. $zulu_time = false;
  214. if (substr($data,-1) == 'Z') $zulu_time = true;
  215. // Pull out the timezone, or use GMT if zulu time was indicated.
  216. if (preg_match('/^'.$property.';TZID=/i', $field)) {
  217. $tz_tmp = explode('=', $field);
  218. $tz_dt = match_tz($tz_tmp[1]); #echo "$tz_dt<br>";
  219. } elseif ($zulu_time) {
  220. $tz_dt = 'GMT';
  221. }
  222. // Extract date-only values.
  223. if ((preg_match('/^'.$property.';VALUE=DATE:/i', $field)) || (ereg ('^([0-9]{4})([0-9]{2})([0-9]{2})$', $data))) {
  224. // Pull out the date value. Minimum year is 1970.
  225. ereg ('([0-9]{4})([0-9]{2})([0-9]{2})', $data, $dt_check);
  226. if ($dt_check[1] < 1970) {
  227. $data = '1971'.$dt_check[2].$dt_check[3];
  228. }
  229. # convert to date-time
  230. $data = $dt_check[1].$dt_check[2].$dt_check[3]."T000000";
  231. $time = '';
  232. $allday = $data;
  233. }
  234. // Extract date-time values.
  235. // Pull out the date and time values. Minimum year is 1970.
  236. preg_match ('/([0-9]{4})([0-9]{2})([0-9]{2})T{0,1}([0-9]{0,2})([0-9]{0,2})/', $data, $regs);
  237. if (!isset ($regs[1])) return;
  238. if ($regs[1] < 1970) {
  239. $regs[1] = '1971';
  240. }
  241. $date = $regs[1] . $regs[2] . $regs[3];
  242. $time = $regs[4] . $regs[5];
  243. $unixtime = mktime($regs[4], $regs[5], 0, $regs[2], $regs[3], $regs[1]);
  244. # chooseOffset checks for Daylight Saving Time
  245. $server_offset_tmp = chooseOffset($unixtime, $phpiCal_config->timezone);
  246. if (isset($tz_dt)) {
  247. $offset_tmp = chooseOffset($unixtime, $tz_dt);
  248. } elseif (isset($calendar_tz)) {
  249. $offset_tmp = chooseOffset($unixtime, $calendar_tz);
  250. $tz_dt = $calendar_tz;
  251. } else {
  252. $offset_tmp = $server_offset_tmp;
  253. $tz_dt = $phpiCal_config->timezone;
  254. }
  255. // Set the values.
  256. $unixtime = calcTime($offset_tmp, $server_offset_tmp, $unixtime);
  257. #echo "offset_tmp $offset_tmp, server_offset_tmp $server_offset_tmp, $unixtime =".date("Ymd His",$unixtime)." $time<br>";
  258. $date = date('Ymd', $unixtime);
  259. if ($allday == '') $time = date('Hi', $unixtime);
  260. // Return the results.
  261. return array($unixtime, $date, $time, $allday, $tz_dt);
  262. }
  263. /* TZIDs in calendars often contain leading information that should be stripped
  264. Example: TZID=/mozilla.org/20050126_1/Europe/Berlin
  265. if this has been set by the parse_tzs scanning the file, then it should be OK, but sometimes a calendar may have a tzid without having defined the vtimezone, expecting a match (This will often happen when users send isolated events in bug reports; the calendars should have vtimezones).
  266. Need to return the part that matches a key in $tz_array
  267. */
  268. function match_tz($data){
  269. global $tz_array;
  270. if (isset($tz_array[$data])) return $data;
  271. foreach ($tz_array as $key=>$val){
  272. if (strpos(" $data",$key) > 0) return $key;
  273. }
  274. return $data;
  275. }?>