Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

517 wiersze
14 KiB

  1. <?php
  2. /**
  3. * @file src/Util/Temporal.php
  4. */
  5. namespace Friendica\Util;
  6. use DateTime;
  7. use DateTimeZone;
  8. use Friendica\Core\L10n;
  9. use Friendica\Core\Renderer;
  10. use Friendica\Database\DBA;
  11. /**
  12. * @brief Temporal class
  13. */
  14. class Temporal
  15. {
  16. /**
  17. * @brief Two-level sort for timezones.
  18. *
  19. * @param string $a
  20. * @param string $b
  21. * @return int
  22. */
  23. private static function timezoneCompareCallback($a, $b)
  24. {
  25. if (strstr($a, '/') && strstr($b, '/')) {
  26. if (L10n::t($a) == L10n::t($b)) {
  27. return 0;
  28. }
  29. return (L10n::t($a) < L10n::t($b)) ? -1 : 1;
  30. }
  31. if (strstr($a, '/')) {
  32. return -1;
  33. } elseif (strstr($b, '/')) {
  34. return 1;
  35. } elseif (L10n::t($a) == L10n::t($b)) {
  36. return 0;
  37. }
  38. return (L10n::t($a) < L10n::t($b)) ? -1 : 1;
  39. }
  40. /**
  41. * @brief Emit a timezone selector grouped (primarily) by continent
  42. *
  43. * @param string $current Timezone
  44. * @return string Parsed HTML output
  45. */
  46. public static function getTimezoneSelect($current = 'America/Los_Angeles')
  47. {
  48. $timezone_identifiers = DateTimeZone::listIdentifiers();
  49. $o = '<select id="timezone_select" name="timezone">';
  50. usort($timezone_identifiers, [__CLASS__, 'timezoneCompareCallback']);
  51. $continent = '';
  52. foreach ($timezone_identifiers as $value) {
  53. $ex = explode("/", $value);
  54. if (count($ex) > 1) {
  55. if ($ex[0] != $continent) {
  56. if ($continent != '') {
  57. $o .= '</optgroup>';
  58. }
  59. $continent = $ex[0];
  60. $o .= '<optgroup label="' . L10n::t($continent) . '">';
  61. }
  62. if (count($ex) > 2) {
  63. $city = substr($value, strpos($value, '/') + 1);
  64. } else {
  65. $city = $ex[1];
  66. }
  67. } else {
  68. $city = $ex[0];
  69. if ($continent != L10n::t('Miscellaneous')) {
  70. $o .= '</optgroup>';
  71. $continent = L10n::t('Miscellaneous');
  72. $o .= '<optgroup label="' . L10n::t($continent) . '">';
  73. }
  74. }
  75. $city = str_replace('_', ' ', L10n::t($city));
  76. $selected = (($value == $current) ? " selected=\"selected\" " : "");
  77. $o .= "<option value=\"$value\" $selected >$city</option>";
  78. }
  79. $o .= '</optgroup></select>';
  80. return $o;
  81. }
  82. /**
  83. * @brief Generating a Timezone selector
  84. *
  85. * Return a select using 'field_select_raw' template, with timezones
  86. * grouped (primarily) by continent
  87. * arguments follow convention as other field_* template array:
  88. * 'name', 'label', $value, 'help'
  89. *
  90. * @param string $name Name of the selector
  91. * @param string $label Label for the selector
  92. * @param string $current Timezone
  93. * @param string $help Help text
  94. *
  95. * @return string Parsed HTML
  96. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  97. */
  98. public static function getTimezoneField($name = 'timezone', $label = '', $current = 'America/Los_Angeles', $help = '')
  99. {
  100. $options = self::getTimezoneSelect($current);
  101. $options = str_replace('<select id="timezone_select" name="timezone">', '', $options);
  102. $options = str_replace('</select>', '', $options);
  103. $tpl = Renderer::getMarkupTemplate('field_select_raw.tpl');
  104. return Renderer::replaceMacros($tpl, [
  105. '$field' => [$name, $label, $current, $help, $options],
  106. ]);
  107. }
  108. /**
  109. * @brief Wrapper for date selector, tailored for use in birthday fields.
  110. *
  111. * @param string $dob Date of Birth
  112. * @param string $timezone
  113. * @return string Formatted HTML
  114. * @throws \Exception
  115. */
  116. public static function getDateofBirthField(string $dob, string $timezone = 'UTC')
  117. {
  118. list($year, $month, $day) = sscanf($dob, '%4d-%2d-%2d');
  119. if ($dob < '0000-01-01') {
  120. $value = '';
  121. } else {
  122. $value = DateTimeFormat::utc(($year > 1000) ? $dob : '1000-' . $month . '-' . $day, 'Y-m-d');
  123. }
  124. $age = (intval($value) ? self::getAgeByTimezone($value, $timezone, $timezone) : "");
  125. $tpl = Renderer::getMarkupTemplate("field_input.tpl");
  126. $o = Renderer::replaceMacros($tpl,
  127. [
  128. '$field' => [
  129. 'dob',
  130. L10n::t('Birthday:'),
  131. $value,
  132. intval($age) > 0 ? L10n::t('Age: ') . $age : "",
  133. '',
  134. 'placeholder="' . L10n::t('YYYY-MM-DD or MM-DD') . '"'
  135. ]
  136. ]);
  137. return $o;
  138. }
  139. /**
  140. * @brief Returns a date selector
  141. *
  142. * @param DateTime $min Minimum date
  143. * @param DateTime $max Maximum date
  144. * @param DateTime $default Default date
  145. * @param string $id ID and name of datetimepicker (defaults to "datetimepicker")
  146. *
  147. * @return string Parsed HTML output.
  148. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  149. */
  150. public static function getDateField($min, $max, $default, $id = 'datepicker')
  151. {
  152. return self::getDateTimeField($min, $max, $default, '', $id, true, false, '', '');
  153. }
  154. /**
  155. * @brief Returns a time selector
  156. *
  157. * @param string $h Already selected hour
  158. * @param string $m Already selected minute
  159. * @param string $id ID and name of datetimepicker (defaults to "timepicker")
  160. *
  161. * @return string Parsed HTML output.
  162. * @throws \Exception
  163. */
  164. public static function getTimeField($h, $m, $id = 'timepicker')
  165. {
  166. return self::getDateTimeField(new DateTime(), new DateTime(), new DateTime("$h:$m"), '', $id, false, true);
  167. }
  168. /**
  169. * @brief Returns a datetime selector.
  170. *
  171. * @param DateTime $minDate Minimum date
  172. * @param DateTime $maxDate Maximum date
  173. * @param DateTime $defaultDate Default date
  174. * @param $label
  175. * @param string $id Id and name of datetimepicker (defaults to "datetimepicker")
  176. * @param bool $pickdate true to show date picker (default)
  177. * @param bool $picktime true to show time picker (default)
  178. * @param string $minfrom set minimum date from picker with id $minfrom (none by default)
  179. * @param string $maxfrom set maximum date from picker with id $maxfrom (none by default)
  180. * @param bool $required default false
  181. *
  182. * @return string Parsed HTML output.
  183. *
  184. * @throws \Friendica\Network\HTTPException\InternalServerErrorException
  185. * @todo Once browser support is better this could probably be replaced with
  186. * native HTML5 date picker.
  187. */
  188. public static function getDateTimeField(
  189. DateTime $minDate,
  190. DateTime $maxDate,
  191. DateTime $defaultDate,
  192. $label,
  193. $id = 'datetimepicker',
  194. $pickdate = true,
  195. $picktime = true,
  196. $minfrom = '',
  197. $maxfrom = '',
  198. $required = false)
  199. {
  200. // First day of the week (0 = Sunday)
  201. $firstDay = DI::pConfig()->get(local_user(), 'system', 'first_day_of_week', 0);
  202. $lang = substr(L10n::getCurrentLang(), 0, 2);
  203. // Check if the detected language is supported by the picker
  204. if (!in_array($lang,
  205. ["ar", "ro", "id", "bg", "fa", "ru", "uk", "en", "el", "de", "nl", "tr", "fr", "es", "th", "pl", "pt", "ch", "se", "kr",
  206. "it", "da", "no", "ja", "vi", "sl", "cs", "hu"])) {
  207. $lang = 'en';
  208. }
  209. $o = '';
  210. $dateformat = '';
  211. if ($pickdate) {
  212. $dateformat .= 'Y-m-d';
  213. }
  214. if ($pickdate && $picktime) {
  215. $dateformat .= ' ';
  216. }
  217. if ($picktime) {
  218. $dateformat .= 'H:i';
  219. }
  220. $input_text = $defaultDate ? date($dateformat, $defaultDate->getTimestamp()) : '';
  221. $readable_format = str_replace(['Y', 'm', 'd', 'H', 'i'], ['yyyy', 'mm', 'dd', 'HH', 'MM'], $dateformat);
  222. $tpl = Renderer::getMarkupTemplate('field_datetime.tpl');
  223. $o .= Renderer::replaceMacros($tpl, [
  224. '$field' => [
  225. $id,
  226. $label,
  227. $input_text,
  228. '',
  229. $required ? '*' : '',
  230. 'placeholder="' . $readable_format . '"'
  231. ],
  232. '$datetimepicker' => [
  233. 'minDate' => $minDate,
  234. 'maxDate' => $maxDate,
  235. 'defaultDate' => $defaultDate,
  236. 'dateformat' => $dateformat,
  237. 'firstDay' => $firstDay,
  238. 'lang' => $lang,
  239. 'minfrom' => $minfrom,
  240. 'maxfrom' => $maxfrom,
  241. ]
  242. ]);
  243. return $o;
  244. }
  245. /**
  246. * @brief Returns a relative date string.
  247. *
  248. * Implements "3 seconds ago" etc.
  249. * Based on $posted_date, (UTC).
  250. * Results relative to current timezone.
  251. * Limited to range of timestamps.
  252. *
  253. * @param string $posted_date MySQL-formatted date string (YYYY-MM-DD HH:MM:SS)
  254. * @param string $format (optional) Parsed with sprintf()
  255. * <tt>%1$d %2$s ago</tt>, e.g. 22 hours ago, 1 minute ago
  256. *
  257. * @return string with relative date
  258. */
  259. public static function getRelativeDate($posted_date, $format = null)
  260. {
  261. $localtime = $posted_date . ' UTC';
  262. $abs = strtotime($localtime);
  263. if (is_null($posted_date) || $posted_date <= DBA::NULL_DATETIME || $abs === false) {
  264. return L10n::t('never');
  265. }
  266. $isfuture = false;
  267. $etime = time() - $abs;
  268. if ($etime < 1 && $etime >= 0) {
  269. return L10n::t('less than a second ago');
  270. }
  271. if ($etime < 0){
  272. $etime = -$etime;
  273. $isfuture = true;
  274. }
  275. $a = [12 * 30 * 24 * 60 * 60 => [L10n::t('year'), L10n::t('years')],
  276. 30 * 24 * 60 * 60 => [L10n::t('month'), L10n::t('months')],
  277. 7 * 24 * 60 * 60 => [L10n::t('week'), L10n::t('weeks')],
  278. 24 * 60 * 60 => [L10n::t('day'), L10n::t('days')],
  279. 60 * 60 => [L10n::t('hour'), L10n::t('hours')],
  280. 60 => [L10n::t('minute'), L10n::t('minutes')],
  281. 1 => [L10n::t('second'), L10n::t('seconds')]
  282. ];
  283. foreach ($a as $secs => $str) {
  284. $d = $etime / $secs;
  285. if ($d >= 1) {
  286. $r = round($d);
  287. // translators - e.g. 22 hours ago, 1 minute ago
  288. if (!$format) {
  289. if($isfuture){
  290. $format = L10n::t('in %1$d %2$s');
  291. }
  292. else {
  293. $format = L10n::t('%1$d %2$s ago');
  294. }
  295. }
  296. return sprintf($format, $r, (($r == 1) ? $str[0] : $str[1]));
  297. }
  298. }
  299. }
  300. /**
  301. * @brief Returns timezone correct age in years.
  302. *
  303. * Returns the age in years, given a date of birth, the timezone of the person
  304. * whose date of birth is provided, and the timezone of the person viewing the
  305. * result.
  306. *
  307. * Why? Bear with me. Let's say I live in Mittagong, Australia, and my birthday
  308. * is on New Year's. You live in San Bruno, California.
  309. * When exactly are you going to see my age increase?
  310. *
  311. * A: 5:00 AM Dec 31 San Bruno time. That's precisely when I start celebrating
  312. * and become a year older. If you wish me happy birthday on January 1
  313. * (San Bruno time), you'll be a day late.
  314. *
  315. * @param string $dob Date of Birth
  316. * @param string $owner_tz (optional) Timezone of the person of interest
  317. * @param string $viewer_tz (optional) Timezone of the person viewing
  318. *
  319. * @return int Age in years
  320. * @throws \Exception
  321. */
  322. public static function getAgeByTimezone($dob, $owner_tz = '', $viewer_tz = '')
  323. {
  324. if (!intval($dob)) {
  325. return 0;
  326. }
  327. if (!$owner_tz) {
  328. $owner_tz = date_default_timezone_get();
  329. }
  330. if (!$viewer_tz) {
  331. $viewer_tz = date_default_timezone_get();
  332. }
  333. $birthdate = DateTimeFormat::convert($dob . ' 00:00:00+00:00', $owner_tz, 'UTC', 'Y-m-d');
  334. list($year, $month, $day) = explode("-", $birthdate);
  335. $year_diff = DateTimeFormat::timezoneNow($viewer_tz, 'Y') - $year;
  336. $curr_month = DateTimeFormat::timezoneNow($viewer_tz, 'm');
  337. $curr_day = DateTimeFormat::timezoneNow($viewer_tz, 'd');
  338. if (($curr_month < $month) || (($curr_month == $month) && ($curr_day < $day))) {
  339. $year_diff--;
  340. }
  341. return $year_diff;
  342. }
  343. /**
  344. * @brief Get days of a month in a given year.
  345. *
  346. * Returns number of days in the month of the given year.
  347. * $m = 1 is 'January' to match human usage.
  348. *
  349. * @param int $y Year
  350. * @param int $m Month (1=January, 12=December)
  351. *
  352. * @return int Number of days in the given month
  353. */
  354. public static function getDaysInMonth($y, $m)
  355. {
  356. return date('t', mktime(0, 0, 0, $m, 1, $y));
  357. }
  358. /**
  359. * @brief Returns the first day in month for a given month, year.
  360. *
  361. * Months start at 1.
  362. *
  363. * @param int $y Year
  364. * @param int $m Month (1=January, 12=December)
  365. *
  366. * @return string day 0 = Sunday through 6 = Saturday
  367. * @throws \Exception
  368. */
  369. private static function getFirstDayInMonth($y, $m)
  370. {
  371. $d = sprintf('%04d-%02d-01 00:00', intval($y), intval($m));
  372. return DateTimeFormat::utc($d, 'w');
  373. }
  374. /**
  375. * @brief Output a calendar for the given month, year.
  376. *
  377. * If $links are provided (array), e.g. $links[12] => 'http://mylink' ,
  378. * date 12 will be linked appropriately. Today's date is also noted by
  379. * altering td class.
  380. * Months count from 1.
  381. *
  382. * @param int $y Year
  383. * @param int $m Month
  384. * @param array $links (default null)
  385. * @param string $class
  386. *
  387. * @return string
  388. *
  389. * @throws \Exception
  390. * @todo Provide (prev, next) links, define class variations for different size calendars
  391. */
  392. public static function getCalendarTable($y = 0, $m = 0, $links = null, $class = '')
  393. {
  394. // month table - start at 1 to match human usage.
  395. $mtab = [' ',
  396. 'January', 'February', 'March',
  397. 'April', 'May', 'June',
  398. 'July', 'August', 'September',
  399. 'October', 'November', 'December'
  400. ];
  401. $thisyear = DateTimeFormat::localNow('Y');
  402. $thismonth = DateTimeFormat::localNow('m');
  403. if (!$y) {
  404. $y = $thisyear;
  405. }
  406. if (!$m) {
  407. $m = intval($thismonth);
  408. }
  409. $dn = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
  410. $f = self::getFirstDayInMonth($y, $m);
  411. $l = self::getDaysInMonth($y, $m);
  412. $d = 1;
  413. $dow = 0;
  414. $started = false;
  415. if (($y == $thisyear) && ($m == $thismonth)) {
  416. $tddate = intval(DateTimeFormat::localNow('j'));
  417. }
  418. $str_month = L10n::getDay($mtab[$m]);
  419. $o = '<table class="calendar' . $class . '">';
  420. $o .= "<caption>$str_month $y</caption><tr>";
  421. for ($a = 0; $a < 7; $a ++) {
  422. $o .= '<th>' . mb_substr(L10n::getDay($dn[$a]), 0, 3, 'UTF-8') . '</th>';
  423. }
  424. $o .= '</tr><tr>';
  425. while ($d <= $l) {
  426. if (($dow == $f) && (!$started)) {
  427. $started = true;
  428. }
  429. $today = (((isset($tddate)) && ($tddate == $d)) ? "class=\"today\" " : '');
  430. $o .= "<td $today>";
  431. $day = str_replace(' ', '&nbsp;', sprintf('%2.2d', $d));
  432. if ($started) {
  433. if (isset($links[$d])) {
  434. $o .= "<a href=\"{$links[$d]}\">$day</a>";
  435. } else {
  436. $o .= $day;
  437. }
  438. $d ++;
  439. } else {
  440. $o .= '&nbsp;';
  441. }
  442. $o .= '</td>';
  443. $dow ++;
  444. if (($dow == 7) && ($d <= $l)) {
  445. $dow = 0;
  446. $o .= '</tr><tr>';
  447. }
  448. }
  449. if ($dow) {
  450. for ($a = $dow; $a < 7; $a ++) {
  451. $o .= '<td>&nbsp;</td>';
  452. }
  453. }
  454. $o .= '</tr></table>' . "\r\n";
  455. return $o;
  456. }
  457. }