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