[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/project/usage/includes/ -> date_api.inc (source)

   1  <?php
   2  // $Id: date_api.inc,v 1.2 2009/04/11 22:42:34 dww Exp $
   3  
   4  /**
   5   * @file
   6   * API functions for manipulating usage dates and timestamps.
   7   */
   8  
   9  /**
  10   * getdate() with timezone adjustment.
  11   *
  12   * PHP's getdate() is affected by the server's timezone. We need to cancel it
  13   * out so everything is GMT.
  14   *
  15   * @param $timestamp
  16   *   An optional, integer UNIX timestamp.
  17   * @return
  18   *   An array with results identical to PHP's getdate().
  19   */
  20  function project_usage_gmgetdate($timestamp = NULL) {
  21    $timestamp = isset($timestamp) ? $timestamp : time();
  22    $gmt_offset = (int) date('Z', $timestamp);
  23    return getdate($timestamp - $gmt_offset);
  24  }
  25  
  26  /**
  27   * Compute a timestamp for the beginning of a day N days ago.
  28   *
  29   * @param $time
  30   *   Mixed, either a GMT timestamp or an array returned by
  31   *   project_usage_gmgetdate().
  32   * @param $days_ago
  33   *   An integer specifying a number of days previous. A value of 0 indicates
  34   *   the current day.
  35   *
  36   * @return
  37   *   GMT UNIX timestamp.
  38   */
  39  function project_usage_daily_timestamp($time = NULL, $days_ago = 0) {
  40    $time_parts = is_array($time) ? $time : project_usage_gmgetdate($time);
  41    $day = $time_parts['mday'] - $days_ago;
  42    return gmmktime(0, 0, 0, $time_parts['mon'], $day, $time_parts['year']);
  43  }
  44  
  45  /**
  46   * Compute a timestamp for the beginning of a week N weeks ago.
  47   *
  48   * @param $time
  49   *   Mixed. Integer timestamp or an array returned by project_usage_gmgetdate().
  50   * @param $weeks_ago
  51   *   An integer specifying a number of weeks previous. A value of 0 indicates
  52   *   the current week.
  53   *
  54   * @return
  55   *   GMT UNIX timestamp.
  56   */
  57  function project_usage_weekly_timestamp($time = NULL, $weeks_ago = 0) {
  58    $time_parts = is_array($time) ? $time : project_usage_gmgetdate($time);
  59    $day = $time_parts['mday'] - $time_parts['wday'] + (7 * $weeks_ago);
  60    return gmmktime(0, 0, 0, $time_parts['mon'], $day, $time_parts['year']);
  61  }
  62  
  63  /**
  64   * Build an array of timestamps for the beginning (midnight Sunday) for each
  65   * week since a given timestamp.
  66   *
  67   * @param $timestamp
  68   *   UNIX timestamp. The first returned timestamp will be the beginning of the
  69   *   week with this time in it.
  70   * @return
  71   *   An array of GMT timestamps sorted in ascending order. The first value is
  72   *   is the week containing $timestamp. Each subsequent value is the timestamp
  73   *   for the next week. The final value is the beginning of the current week.
  74   */
  75  function project_usage_get_weeks_since($timestamp) {
  76    $times = array();
  77  
  78    // First, compute the start of the current week so we know when to stop...
  79    $this_week = project_usage_weekly_timestamp();
  80  
  81    // ...then compute all the weeks up to that.
  82    $parts = project_usage_gmgetdate($timestamp);
  83    $i = 0;
  84    do {
  85      $times[$i] = project_usage_weekly_timestamp($parts, $i);
  86    } while ($times[$i++] < $this_week);
  87  
  88    return $times;
  89  }
  90  
  91  /**
  92   * Return an array of the most recent weeks for which we have data.
  93   *
  94   * @return
  95   *   An array of UNIX timestamps sorted newest to oldest. Will not include
  96   *   the current week.
  97   */
  98  function project_usage_get_active_weeks($reset = FALSE) {
  99    $weeks = variable_get('project_usage_active_weeks', array());
 100    if ($reset || empty($weeks)) {
 101      $count = variable_get('project_usage_show_weeks', PROJECT_USAGE_SHOW_WEEKS);
 102      $query = db_query_range("SELECT DISTINCT(timestamp) FROM {project_usage_week_project} ORDER BY timestamp DESC", array(), 0, $count);
 103      $weeks = array();
 104      while ($week = db_fetch_object($query)) {
 105        $weeks[] = $week->timestamp;
 106      }
 107      variable_set('project_usage_active_weeks', $weeks);
 108    }
 109    return $weeks;
 110  }
 111  
 112  function project_usage_get_current_active_week() {
 113    static $current_week = 0;
 114    if (empty($current_week)) {
 115      $current_week = array_shift(project_usage_get_active_weeks());
 116    }
 117    return $current_week;
 118  }
 119  
 120  /**
 121   * Sets the expiry timestamp for cached project usage pages.
 122   *
 123   * Default is 24 hours.
 124   *
 125   * @return The UNIX timestamp to expire the page at.
 126   */
 127  function project_usage_cache_time() {
 128    return time() + variable_get('project_usage_cache_length', 86400);
 129  }
 130  


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