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
36 * Round up to the nearest half hour
38 * @param string $time the time to round (date/time string)
39 * @return DateTime the rounded time
41 public static function nearestHalfHour($time)
43 $start = strtotime($time);
45 $minutes = date('i', $start);
46 $hour = date('H', $start);
55 $newTimeStr = date('m/d/y', $start) . " {$hour}:{$minutes}:00";
56 return new DateTime($newTimeStr);
60 * Output a list of times in half-hour intervals
62 * @param string $start Time to start with (date/time string)
63 * @param boolean $duration Whether to include the duration of the event
65 * @return array $times (UTC time string => localized time string)
67 public static function getTimes($start = 'now', $duration = false)
69 $newTime = self::nearestHalfHour($start);
71 $newTime->setTimezone(new DateTimeZone(common_timezone()));
75 for ($i = 0; $i < 48; $i++) {
77 // make sure we store the time as UTC
78 $newTime->setTimezone(new DateTimeZone('UTC'));
79 $utcTime = $newTime->format('H:i:s');
81 // localize time for user
82 $newTime->setTimezone(new DateTimeZone(common_timezone()));
83 $localTime = $newTime->format('g:ia');
85 // pretty up the end-time option list a bit
90 $hourStr = _m('hour');
91 $hoursStr = _m('hrs');
95 $total = " (0 {$minStr})";
98 $total = " (30 {$minStr})";
101 $total = " (1 {$hourStr})";
104 $total = " ({$hours} " . $hoursStr . ')';
107 $localTime .= $total;
110 $times[$utcTime] = $localTime;
111 $newTime->modify('+30min'); // 30 min intervals