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