eventtimelist.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * Helper class for calculating and displaying event times
  4. *
  5. * PHP version 5
  6. *
  7. * @category Data
  8. * @package StatusNet
  9. * @author Zach Copley <zach@status.net>
  10. * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
  11. * @link http://status.net/
  12. *
  13. * StatusNet - the distributed open-source microblogging tool
  14. * Copyright (C) 2011, StatusNet, Inc.
  15. *
  16. * This program is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License as published by
  18. * the Free Software Foundation, either version 3 of the License, or
  19. * (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  28. */
  29. /**
  30. * Class to get fancy times for the dropdowns on the new event form
  31. */
  32. class EventTimeList {
  33. /**
  34. * Round up to the nearest half hour
  35. *
  36. * @param string $time the time to round (date/time string)
  37. * @return DateTime the rounded time
  38. */
  39. public static function nearestHalfHour($time)
  40. {
  41. $startd = new DateTime($time);
  42. $minutes = $startd->format('i');
  43. $hour = $startd->format('H');
  44. if ($minutes >= 30) {
  45. $minutes = '00';
  46. $hour++;
  47. } else {
  48. $minutes = '30';
  49. }
  50. $startd->setTime($hour, $minutes, 0);
  51. return $startd;
  52. }
  53. /**
  54. * Output a list of times in half-hour intervals
  55. *
  56. * @param string $start Time to start with (date string, usually a ts)
  57. * @param boolean $duration Whether to include the duration of the event
  58. * (from the start)
  59. * @return array $times (localized 24 hour time string => fancy time string)
  60. */
  61. public static function getTimes($start = 'now', $duration = false)
  62. {
  63. $newTime = new DateTime($start);
  64. $times = array();
  65. $len = 0;
  66. $newTime->setTimezone(new DateTimeZone(common_timezone()));
  67. for ($i = 0; $i < 47; $i++) {
  68. $localTime = $newTime->format("g:ia");
  69. // pretty up the end-time option list a bit
  70. if ($duration) {
  71. $hours = $len / 60;
  72. switch ($hours) {
  73. case 0:
  74. // TRANS: 0 minutes abbreviated. Used in a list.
  75. $total = ' ' . _m('(0 min)');
  76. break;
  77. case .5:
  78. // TRANS: 30 minutes abbreviated. Used in a list.
  79. $total = ' ' . _m('(30 min)');
  80. break;
  81. case 1:
  82. // TRANS: 1 hour. Used in a list.
  83. $total = ' ' . _m('(1 hour)');
  84. break;
  85. default:
  86. // TRANS: Number of hours (%.1f and %d). Used in a list.
  87. $format = is_float($hours)
  88. ? _m('(%.1f hours)')
  89. : _m('(%d hours)');
  90. $total = ' ' . sprintf($format, $hours);
  91. break;
  92. }
  93. $localTime .= $total;
  94. $len += 30;
  95. }
  96. $times[$newTime->format('g:ia')] = $localTime;
  97. $newTime->modify('+30min'); // 30 min intervals
  98. }
  99. return $times;
  100. }
  101. }