[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/station/ -> dayhour.inc (source)

   1  <?php
   2  
   3  // $Id: dayhour.inc,v 1.23 2010/11/18 05:23:38 timplunkett 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 = drupal_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(drupal_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 string, e.g. "Sunday".
 206   *
 207   * @param $time
 208   *   Integer specifying minutes.
 209   * @return
 210   *   Formatted string.
 211   */
 212  function theme_station_day($time) {
 213    $day = station_day_from_minute($time);
 214    $format_params = array(
 215      '@day' => station_day_name($day),
 216    );
 217  
 218    return t('@day', $format_params);
 219  }
 220  
 221  /**
 222   * Format minutes into a day hour string, e.g. "Sunday 11:15pm".
 223   *
 224   * @param $time
 225   *   Integer specifying minutes.
 226   * @return
 227   *   Formatted string.
 228   */
 229  function theme_station_dayhour($time) {
 230    $time = station_time_from_minute($time);
 231    $format_params = array(
 232      '@day' => station_day_name($time['w']), '@hour' => $time['g'],
 233      '@time' => $time['time'], '@ampm' => $time['a'],
 234    );
 235  
 236    return t('@day @time@ampm', $format_params);
 237  }
 238  
 239  /**
 240   * Format a range of minutes into a day hour string, e.g.
 241   * "Sunday 11pm - Monday 1am".
 242   *
 243   * @param $start
 244   *   Integer specifying minutes.
 245   * @param $finish
 246   *   Integer specifying minutes.
 247   * @return
 248   *   Formatted string.
 249   */
 250  function theme_station_dayhour_range($start, $finish) {
 251    $start = station_time_from_minute($start);
 252    $finish = station_time_from_minute($finish);
 253    $format_params = array(
 254      '@sday' => station_day_name($start['w']), '@shour' => $start['g'],
 255      '@stime' => $start['time'], '@sampm' => $start['a'],
 256      '@fday' => station_day_name($finish['w']), '@fhour' => $finish['g'],
 257      '@ftime' => $finish['time'], '@fampm' => $finish['a'],
 258    );
 259  
 260    // same day
 261    if ($start['w'] == $finish['w']) {
 262      // same am pm
 263      if ($start['a'] == $finish['a']) {
 264        return t('@sday @stime-@ftime@sampm', $format_params);
 265      }
 266      else {
 267        return t('@sday @stime@sampm-@ftime@fampm', $format_params);
 268      }
 269    }
 270    else {
 271      return t('@sday @stime@sampm-@fday @ftime@fampm', $format_params);
 272    }
 273  }
 274  
 275  /**
 276   * Format a range of minutes into a hour string, e.g. "1am-3pm".
 277   *
 278   * @param $start
 279   *   Integer specifying minutes.
 280   * @param $finish
 281   *   Integer specifying minutes.
 282   * @return
 283   *   Formatted string.
 284   */
 285  function theme_station_hour_range($start, $finish) {
 286    $start = station_time_from_minute($start);
 287    $finish = station_time_from_minute($finish);
 288    $format_params = array(
 289      '@stime' => $start['time'], '@sampm' => $start['a'],
 290      '@ftime' => $finish['time'], '@fampm' => $finish['a'],
 291    );
 292  
 293    if ($start['a'] == $finish['a']) {
 294      return t('@stime-@ftime@sampm', $format_params);
 295    }
 296    else {
 297      return t('@stime@sampm-@ftime@fampm', $format_params);
 298    }
 299  }
 300  
 301  /**
 302   * Print the amount of time between start and finish.
 303   *
 304   * @param $start
 305   *   Integer specifying minutes.
 306   * @param $finish
 307   *   Integer specifying minutes.
 308   * @return
 309   *   Formatted string.
 310   */
 311  function theme_station_hour_duration($start, $finish) {
 312    return format_interval(($finish - $start) * 60);
 313  }
 314  
 315  /**
 316   * Format minutes into a hour string, e.g. "1am".
 317   *
 318   * @param $time
 319   *   Integer specifying minutes.
 320   * @return
 321   *   Formatted string.
 322   */
 323  function theme_station_hour($time) {
 324    $time = station_time_from_minute($time);
 325    $format_params = array(
 326      '@time' => $time['time'], '@ampm' => $time['a'],
 327    );
 328  
 329    return t('@time@ampm', $format_params);
 330  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7