3 * Helper class for calculating and displaying event times
9 * @author Zach Copley <zach@status.net>
10 * @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
11 * @link http://status.net/
13 * StatusNet - the distributed open-source microblogging tool
14 * Copyright (C) 2011, StatusNet, Inc.
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.
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.
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/>.
31 * Class to get fancy times for the dropdowns on the new event form
35 * Round up to the nearest half hour
37 * @param string $time the time to round (date/time string)
38 * @return DateTime the rounded time
40 public static function nearestHalfHour($time)
42 $start = strtotime($time);
44 $minutes = date('i', $start);
45 $hour = date('H', $start);
54 $newTimeStr = date('m/d/y', $start) . " {$hour}:{$minutes}:00";
55 return new DateTime($newTimeStr);
59 * Output a list of times in half-hour intervals
61 * @param string $start Time to start with (date/time string)
62 * @param boolean $duration Whether to include the duration of the event
64 * @return array $times (UTC time string => localized time string)
66 public static function getTimes($start = 'now', $duration = false)
68 $newTime = self::nearestHalfHour($start);
70 $newTime->setTimezone(new DateTimeZone(common_timezone()));
74 for ($i = 0; $i < 48; $i++) {
75 // make sure we store the time as UTC
76 $newTime->setTimezone(new DateTimeZone('UTC'));
77 $utcTime = $newTime->format('H:i:s');
79 // localize time for user
80 $newTime->setTimezone(new DateTimeZone(common_timezone()));
81 $localTime = $newTime->format('g:ia');
83 // pretty up the end-time option list a bit
89 // TRANS: 0 minutes abbreviated. Used in a list.
90 $total = ' ' . _m('(0 min)');
93 // TRANS: 30 minutes abbreviated. Used in a list.
94 $total = ' ' . _m('(30 min)');
97 // TRANS: 1 hour. Used in a list.
98 $total = ' ' . _m('(1 hour)');
101 // TRANS: Number of hours (%d). Used in a list.
102 $total = ' ' . sprintf(_m('(%d hour)','(%d hours)',$hours), $hours);
105 $localTime .= $total;
108 $times[$utcTime] = $localTime;
109 $newTime->modify('+30min'); // 30 min intervals