]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Event/eventtimelist.php
Work improving the interface of the Event micro-app
[quix0rs-gnu-social.git] / plugins / Event / eventtimelist.php
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 /**
31  *  Class to get fancy times for the dropdowns on the new event form
32  */
33 class EventTimeList {
34
35     /**
36      * Round up to the nearest half hour
37      *
38      * @param string $time the time to round (date/time string)
39      * @return DateTime    the rounded time
40      */
41     public static function nearestHalfHour($time)
42     {
43         $start = strtotime($time);
44
45         $minutes = date('i', $start);
46         $hour = date('H', $start);
47
48         if ($minutes >= 30) {
49             $minutes = '00';
50             $hour++;
51         } else {
52             $minutes = '30';
53         }
54
55         $newTimeStr = date('m/d/y', $start) . " {$hour}:{$minutes}:00";
56         return new DateTime($newTimeStr);
57     }
58
59     /**
60      * Output a list of times in half-hour intervals
61      *
62      * @param string  $start       Time to start with (date/time string)
63      * @param boolean $duration    Whether to include the duration of the event
64      *                             (from the start)
65      * @return array  $times (UTC time string => localized time string)
66      */
67     public static function getTimes($start = 'now', $duration = false)
68     {
69         $newTime = self::nearestHalfHour($start);
70
71         $newTime->setTimezone(new DateTimeZone(common_timezone()));
72         $times = array();
73         $len   = 0;
74
75         for ($i = 0; $i < 48; $i++) {
76
77             // make sure we store the time as UTC
78             $newTime->setTimezone(new DateTimeZone('UTC'));
79             $utcTime = $newTime->format('H:i:s');
80
81             // localize time for user
82             $newTime->setTimezone(new DateTimeZone(common_timezone()));
83             $localTime = $newTime->format('g:ia');
84
85             // pretty up the end-time option list a bit
86             if ($duration) {
87                 $len += 30;
88                 $hours    = $len / 60;
89                 // for i18n
90                 $hourStr  = _m('hour');
91                 $hoursStr = _m('hrs');
92                 $minStr   = _m('mins');
93                 switch ($hours) {
94                 case 0:
95                     $total = " (0 {$minStr})";
96                     break;
97                 case .5:
98                     $total = " (30 {$minStr})";
99                     break;
100                 case 1:
101                     $total = " (1 {$hourStr})";
102                     break;
103                 default:
104                     $total = " ({$hours} " . $hoursStr . ')';
105                     break;
106                 }
107                 $localTime .= $total;
108             }
109
110             $times[$utcTime] = $localTime;
111             $newTime->modify('+30min'); // 30 min intervals
112         }
113
114         return $times;
115     }
116
117 }
118
119