| 1 |
<?php
|
| 2 |
|
| 3 |
// $Id: dayhour.inc,v 1.20 2009/08/29 08:01:41 drewish Exp $
|
| 4 |
|
| 5 |
// There are 1440 minutes in a day (60minute * 24hours = 1day).
|
| 6 |
define('MINUTES_IN_DAY', 1440);
|
| 7 |
// There are 10080 minutes in a week (60minute * 24hours * 1day = 1week).
|
| 8 |
define('MINUTES_IN_WEEK', 10080);
|
| 9 |
|
| 10 |
/**
|
| 11 |
* Turn a string into a valid hour.
|
| 12 |
*
|
| 13 |
* @param $hour
|
| 14 |
* String containing an hour. e.g. 0-24, 0-12 am/pm, noon, midnight
|
| 15 |
* @return
|
| 16 |
* An integer between 0 and 23.
|
| 17 |
*/
|
| 18 |
function station_valid_hour($hour) {
|
| 19 |
if (!is_numeric($hour)) {
|
| 20 |
$hour = strtolower(trim($hour));
|
| 21 |
|
| 22 |
// take care of a couple of handy strings
|
| 23 |
if ($hour == t('midnight')) {
|
| 24 |
return 0;
|
| 25 |
}
|
| 26 |
elseif ($hour == t('noon')) {
|
| 27 |
return 12;
|
| 28 |
}
|
| 29 |
|
| 30 |
// try to parse out am/pm times then let it fall through to the
|
| 31 |
// 24 hour stuff below.
|
| 32 |
$parts = array();
|
| 33 |
if (preg_match('/(\d+)\s*([ap]m)/', $hour, $parts)) {
|
| 34 |
$hour = (integer) $parts[1];
|
| 35 |
// 12am and 12pm are special cases
|
| 36 |
if ($hour == 12) {
|
| 37 |
$hour += 12;
|
| 38 |
}
|
| 39 |
if ($parts[2] == 'pm') {
|
| 40 |
$hour += 12;
|
| 41 |
}
|
| 42 |
}
|
| 43 |
}
|
| 44 |
|
| 45 |
if ($hour < 0) {
|
| 46 |
return ((integer) $hour % 24) + 24;
|
| 47 |
}
|
| 48 |
return ((integer) $hour % 24);
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Get a numeric day of the week from a string or integer.
|
| 53 |
*
|
| 54 |
* @param $day
|
| 55 |
* An integer from 0-6, or a case in-sensitive, English day name.
|
| 56 |
* @return
|
| 57 |
* An nteger between 0 and 6 (Sunday, Monday, ... , Saturday).
|
| 58 |
*/
|
| 59 |
function station_valid_day($day) {
|
| 60 |
if (is_numeric($day)) {
|
| 61 |
if ($day < 0) {
|
| 62 |
return ((integer) $day % 7) + 7;
|
| 63 |
}
|
| 64 |
elseif ($day > 6) {
|
| 65 |
return ((integer) $day % 7);
|
| 66 |
}
|
| 67 |
return (integer) $day;
|
| 68 |
}
|
| 69 |
else {
|
| 70 |
$dayname = station_day_name();
|
| 71 |
$ret = array_search(ucfirst($day), $dayname, FALSE);
|
| 72 |
return ($ret === FALSE) ? 0 : $ret;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
|
| 76 |
/**
|
| 77 |
* Return an array of the names of the days of the week.
|
| 78 |
*
|
| 79 |
* @param $day
|
| 80 |
* An optional integer for the day of the week, zero being Sunday.
|
| 81 |
* @return
|
| 82 |
* If $day is specified a string, othereise an array of the names of all
|
| 83 |
* the days in the week. When an array is returned it will be ordered
|
| 84 |
* honoring the site's first day of the week setting.
|
| 85 |
*/
|
| 86 |
function station_day_name($day = NULL) {
|
| 87 |
static $days;
|
| 88 |
|
| 89 |
if (!isset($days)) {
|
| 90 |
$names = array(
|
| 91 |
0 => t('Sunday'),
|
| 92 |
t('Monday'),
|
| 93 |
t('Tuesday'),
|
| 94 |
t('Wednesday'),
|
| 95 |
t('Thursday'),
|
| 96 |
t('Friday'),
|
| 97 |
t('Saturday')
|
| 98 |
);
|
| 99 |
$day_offset = variable_get('date_first_day', 0);
|
| 100 |
$days = array();
|
| 101 |
for ($i = $day_offset; $i < (7 + $day_offset); $i++) {
|
| 102 |
$days[$i % 7] = $names[$i % 7];
|
| 103 |
}
|
| 104 |
}
|
| 105 |
|
| 106 |
if ($day === NULL) {
|
| 107 |
return $days;
|
| 108 |
}
|
| 109 |
else {
|
| 110 |
return $days[$day % 7];
|
| 111 |
}
|
| 112 |
}
|
| 113 |
|
| 114 |
/**
|
| 115 |
* Given a day and hour, compute the number of minutes since midnight Sunday.
|
| 116 |
*
|
| 117 |
* @param $day
|
| 118 |
* Integer from 0 to 6.
|
| 119 |
* @param $hour
|
| 120 |
* Integer from 0 to 23.
|
| 121 |
* @return
|
| 122 |
* Integer specifying the number of minutes.
|
| 123 |
*/
|
| 124 |
function station_minute_from_day_hour($day, $hour) {
|
| 125 |
return (($day * 24) + $hour) * 60;
|
| 126 |
}
|
| 127 |
|
| 128 |
/**
|
| 129 |
* Convert a local timestamp into an integer specifying minutes since midnight
|
| 130 |
* on Sunday.
|
| 131 |
*
|
| 132 |
* @return
|
| 133 |
* Integer specifying the number of minutes.
|
| 134 |
*
|
| 135 |
* @see station_local_ts()
|
| 136 |
*/
|
| 137 |
function station_minute_from_local_ts($local_timestamp = NULL) {
|
| 138 |
if (!isset($local_timestamp)) {
|
| 139 |
$local_timestamp = station_local_ts();
|
| 140 |
}
|
| 141 |
list($day, $hour, $minute) = explode(' ', date('w G i', $local_timestamp));
|
| 142 |
return (($day * 24) + $hour) * 60 + $minute;
|
| 143 |
}
|
| 144 |
|
| 145 |
/**
|
| 146 |
* Day of the week from minutes.
|
| 147 |
*
|
| 148 |
* @param $minutes
|
| 149 |
* Integer with the number of minutes since midnight Sunday.
|
| 150 |
* @return
|
| 151 |
* Integer day of the week, zero being Sunday.
|
| 152 |
*/
|
| 153 |
function station_day_from_minute($minute) {
|
| 154 |
return (int) (($minute) / MINUTES_IN_DAY);
|
| 155 |
}
|
| 156 |
|
| 157 |
/**
|
| 158 |
* Compute time information for a minute in the week.
|
| 159 |
*
|
| 160 |
* @param $minutes
|
| 161 |
* Integer specifying minutes since midnight on Sunday.
|
| 162 |
* @return
|
| 163 |
* An array with the following keys:
|
| 164 |
* 'w' - Day of week (0-6).
|
| 165 |
* 'G' - 24 hour.
|
| 166 |
* 'g' - 12 hour.
|
| 167 |
* 'H' - 24 hour, 0 padded.
|
| 168 |
* 'h' - 12 hour, 0 padded.
|
| 169 |
* 'i' - minutes, 0 padded.
|
| 170 |
* 'time' - hour and minutes acording to 12/24 setting.
|
| 171 |
* 'minutes' - minutes since midnight Sunday.
|
| 172 |
* 'a' - am/pm.
|
| 173 |
*/
|
| 174 |
function station_time_from_minute($minutes) {
|
| 175 |
$min = $minutes % 60;
|
| 176 |
$day = (int) (($minutes) / MINUTES_IN_DAY);
|
| 177 |
$hour24 = (int) (($minutes % MINUTES_IN_DAY) / 60);
|
| 178 |
if (!($hour12 = $hour24 % 12)) {
|
| 179 |
$hour12 = 12;
|
| 180 |
}
|
| 181 |
$i = str_pad($min, 2, '0', STR_PAD_LEFT);
|
| 182 |
$h = str_pad($hour12, 2, '0', STR_PAD_LEFT);
|
| 183 |
if (variable_get('station_clock', 12) == 12) {
|
| 184 |
$time = $hour12 . (($min == 0) ? '' : ":$i");
|
| 185 |
$a = ($hour24 > 11) ? 'pm' : 'am';
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
$time = "$hour24:$i";
|
| 189 |
$a = '';
|
| 190 |
}
|
| 191 |
return array(
|
| 192 |
'w' => $day,
|
| 193 |
'G' => $hour24,
|
| 194 |
'g' => $hour12,
|
| 195 |
'H' => str_pad($hour24, 2, '0', STR_PAD_LEFT),
|
| 196 |
'h' => $h,
|
| 197 |
'i' => $i,
|
| 198 |
'time' => $time,
|
| 199 |
'minutes' => $minutes,
|
| 200 |
'a' => $a,
|
| 201 |
);
|
| 202 |
}
|
| 203 |
|
| 204 |
/**
|
| 205 |
* Format minutes into a day hour string, e.g. "Sunday 11:15pm".
|
| 206 |
*
|
| 207 |
* @param $time
|
| 208 |
* Integer specifying minutes.
|
| 209 |
* @return
|
| 210 |
* Formatted string.
|
| 211 |
*/
|
| 212 |
function theme_station_dayhour($time) {
|
| 213 |
$time = station_time_from_minute($time);
|
| 214 |
$format_params = array(
|
| 215 |
'@day' => station_day_name($time['w']), '@hour' => $time['g'],
|
| 216 |
'@time' => $time['time'], '@ampm' => $time['a'],
|
| 217 |
);
|
| 218 |
|
| 219 |
return t('@day @time@ampm', $format_params);
|
| 220 |
}
|
| 221 |
|
| 222 |
/**
|
| 223 |
* Format a range of minutes into a day hour string, e.g.
|
| 224 |
* "Sunday 11pm - Monday 1am".
|
| 225 |
*
|
| 226 |
* @param $start
|
| 227 |
* Integer specifying minutes.
|
| 228 |
* @param $finish
|
| 229 |
* Integer specifying minutes.
|
| 230 |
* @return
|
| 231 |
* Formatted string.
|
| 232 |
*/
|
| 233 |
function theme_station_dayhour_range($start, $finish) {
|
| 234 |
$start = station_time_from_minute($start);
|
| 235 |
$finish = station_time_from_minute($finish);
|
| 236 |
$format_params = array(
|
| 237 |
'@sday' => station_day_name($start['w']), '@shour' => $start['g'],
|
| 238 |
'@stime' => $start['time'], '@sampm' => $start['a'],
|
| 239 |
'@fday' => station_day_name($finish['w']), '@fhour' => $finish['g'],
|
| 240 |
'@ftime' => $finish['time'], '@fampm' => $finish['a'],
|
| 241 |
);
|
| 242 |
|
| 243 |
// same day
|
| 244 |
if ($start['w'] == $finish['w']) {
|
| 245 |
// same am pm
|
| 246 |
if ($start['a'] == $finish['a']) {
|
| 247 |
return t('@sday @stime-@ftime@sampm', $format_params);
|
| 248 |
}
|
| 249 |
else {
|
| 250 |
return t('@sday @stime@sampm-@ftime@fampm', $format_params);
|
| 251 |
}
|
| 252 |
}
|
| 253 |
else {
|
| 254 |
return t('@sday @stime@sampm-@fday @ftime@fampm', $format_params);
|
| 255 |
}
|
| 256 |
}
|
| 257 |
|
| 258 |
/**
|
| 259 |
* Format a range of minutes into a hour string, e.g. "1am-3pm".
|
| 260 |
*
|
| 261 |
* @param $start
|
| 262 |
* Integer specifying minutes.
|
| 263 |
* @param $finish
|
| 264 |
* Integer specifying minutes.
|
| 265 |
* @return
|
| 266 |
* Formatted string.
|
| 267 |
*/
|
| 268 |
function theme_station_hour_range($start, $finish) {
|
| 269 |
$start = station_time_from_minute($start);
|
| 270 |
$finish = station_time_from_minute($finish);
|
| 271 |
$format_params = array(
|
| 272 |
'@stime' => $start['time'], '@sampm' => $start['a'],
|
| 273 |
'@ftime' => $finish['time'], '@fampm' => $finish['a'],
|
| 274 |
);
|
| 275 |
|
| 276 |
if ($start['a'] == $finish['a']) {
|
| 277 |
return t('@stime-@ftime@sampm', $format_params);
|
| 278 |
}
|
| 279 |
else {
|
| 280 |
return t('@stime@sampm-@ftime@fampm', $format_params);
|
| 281 |
}
|
| 282 |
}
|
| 283 |
|
| 284 |
/**
|
| 285 |
* Print the amount of time between start and finish.
|
| 286 |
*
|
| 287 |
* @param $start
|
| 288 |
* Integer specifying minutes.
|
| 289 |
* @param $finish
|
| 290 |
* Integer specifying minutes.
|
| 291 |
* @return
|
| 292 |
* Formatted string.
|
| 293 |
*/
|
| 294 |
function theme_station_hour_duration($start, $finish) {
|
| 295 |
return format_interval(($finish - $start) * 60);
|
| 296 |
}
|
| 297 |
|
| 298 |
/**
|
| 299 |
* Format minutes into a hour string, e.g. "1am".
|
| 300 |
*
|
| 301 |
* @param $time
|
| 302 |
* Integer specifying minutes.
|
| 303 |
* @return
|
| 304 |
* Formatted string.
|
| 305 |
*/
|
| 306 |
function theme_station_hour($time) {
|
| 307 |
$time = station_time_from_minute($time);
|
| 308 |
$format_params = array(
|
| 309 |
'@time' => $time['time'], '@ampm' => $time['a'],
|
| 310 |
);
|
| 311 |
|
| 312 |
return t('@time@ampm', $format_params);
|
| 313 |
}
|