2011-08-02 17:17:56 +09:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Helper class for calculating and displaying event times
|
|
|
|
*
|
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* @category Data
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Zach Copley <zach@status.net>
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
|
|
|
|
* @link http://status.net/
|
|
|
|
*
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2011, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class to get fancy times for the dropdowns on the new event form
|
|
|
|
*/
|
|
|
|
class EventTimeList {
|
|
|
|
/**
|
|
|
|
* Round up to the nearest half hour
|
|
|
|
*
|
|
|
|
* @param string $time the time to round (date/time string)
|
|
|
|
* @return DateTime the rounded time
|
|
|
|
*/
|
|
|
|
public static function nearestHalfHour($time)
|
|
|
|
{
|
2011-09-08 16:42:13 +09:00
|
|
|
$startd = new DateTime($time);
|
2011-08-02 17:17:56 +09:00
|
|
|
|
2011-09-08 16:42:13 +09:00
|
|
|
$minutes = $startd->format('i');
|
|
|
|
$hour = $startd->format('H');
|
2011-08-02 17:17:56 +09:00
|
|
|
|
|
|
|
if ($minutes >= 30) {
|
|
|
|
$minutes = '00';
|
|
|
|
$hour++;
|
|
|
|
} else {
|
|
|
|
$minutes = '30';
|
|
|
|
}
|
|
|
|
|
2011-09-08 16:42:13 +09:00
|
|
|
$startd->setTime($hour, $minutes, 0);
|
|
|
|
|
|
|
|
return $startd;
|
2011-08-02 17:17:56 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Output a list of times in half-hour intervals
|
|
|
|
*
|
2011-09-08 16:42:13 +09:00
|
|
|
* @param string $start Time to start with (date string, usually a ts)
|
2011-08-02 17:17:56 +09:00
|
|
|
* @param boolean $duration Whether to include the duration of the event
|
|
|
|
* (from the start)
|
2011-09-08 16:42:13 +09:00
|
|
|
* @return array $times (localized 24 hour time string => fancy time string)
|
2011-08-02 17:17:56 +09:00
|
|
|
*/
|
|
|
|
public static function getTimes($start = 'now', $duration = false)
|
|
|
|
{
|
2011-09-08 16:42:13 +09:00
|
|
|
$newTime = new DateTime($start);
|
|
|
|
$times = array();
|
|
|
|
$len = 0;
|
2011-08-02 17:17:56 +09:00
|
|
|
|
|
|
|
$newTime->setTimezone(new DateTimeZone(common_timezone()));
|
|
|
|
|
2011-09-08 16:42:13 +09:00
|
|
|
for ($i = 0; $i < 47; $i++) {
|
2011-08-02 17:17:56 +09:00
|
|
|
|
2011-09-08 16:42:13 +09:00
|
|
|
$localTime = $newTime->format("g:ia");
|
2011-08-02 17:17:56 +09:00
|
|
|
|
|
|
|
// pretty up the end-time option list a bit
|
|
|
|
if ($duration) {
|
2011-09-08 16:42:13 +09:00
|
|
|
$hours = $len / 60;
|
2011-08-02 17:17:56 +09:00
|
|
|
switch ($hours) {
|
|
|
|
case 0:
|
2011-08-17 01:04:56 +09:00
|
|
|
// TRANS: 0 minutes abbreviated. Used in a list.
|
|
|
|
$total = ' ' . _m('(0 min)');
|
2011-08-02 17:17:56 +09:00
|
|
|
break;
|
|
|
|
case .5:
|
2011-08-17 01:04:56 +09:00
|
|
|
// TRANS: 30 minutes abbreviated. Used in a list.
|
|
|
|
$total = ' ' . _m('(30 min)');
|
2011-08-02 17:17:56 +09:00
|
|
|
break;
|
|
|
|
case 1:
|
2011-08-17 01:04:56 +09:00
|
|
|
// TRANS: 1 hour. Used in a list.
|
|
|
|
$total = ' ' . _m('(1 hour)');
|
2011-08-02 17:17:56 +09:00
|
|
|
break;
|
|
|
|
default:
|
2011-09-08 16:42:13 +09:00
|
|
|
// TRANS: Number of hours (%.1f and %d). Used in a list.
|
|
|
|
$format = is_float($hours)
|
|
|
|
? _m('(%.1f hours)')
|
|
|
|
: _m('(%d hours)');
|
|
|
|
$total = ' ' . sprintf($format, $hours);
|
2011-08-02 17:17:56 +09:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
$localTime .= $total;
|
2011-09-08 16:42:13 +09:00
|
|
|
$len += 30;
|
2011-08-02 17:17:56 +09:00
|
|
|
}
|
|
|
|
|
2011-09-08 16:42:13 +09:00
|
|
|
$times[$newTime->format('g:ia')] = $localTime;
|
2011-08-02 17:17:56 +09:00
|
|
|
$newTime->modify('+30min'); // 30 min intervals
|
|
|
|
}
|
|
|
|
|
|
|
|
return $times;
|
|
|
|
}
|
|
|
|
}
|