[ Index ]
 

Code source de LifeType 1.2.4

Accédez au Source d'autres logiciels libres

Classes | Fonctions | Variables | Constantes | Tables

title

Body

[fermer]

/class/data/Date/ -> Human.class.php (source)

   1  <?php
   2  //
   3  // +----------------------------------------------------------------------+
   4  // | PHP Version 4                                                        |
   5  // +----------------------------------------------------------------------+
   6  // | Copyright (c) 1997-2003 The PHP Group                                |
   7  // +----------------------------------------------------------------------+
   8  // | This source file is subject to version 2.0 of the PHP license,       |
   9  // | that is bundled with this package in the file LICENSE, and is        |
  10  // | available at through the world-wide-web at                           |
  11  // | http://www.php.net/license/2_02.txt.                                 |
  12  // | If you did not receive a copy of the PHP license and are unable to   |
  13  // | obtain it through the world-wide-web, please send a note to          |
  14  // | license@php.net so we can mail you a copy immediately.               |
  15  // +----------------------------------------------------------------------+
  16  // | Author: Allan Kent <allan@lodestone.co.za>                           |
  17  // +----------------------------------------------------------------------+
  18  //
  19  // $Id: Human.php,v 1.3 2003/01/04 11:54:54 mj Exp $
  20  //
  21  
  22  /**
  23   * \ingroup Data
  24   * 
  25   * Class to convert date strings between Gregorian and Human calendar formats.
  26   * The Human Calendar format has been proposed by Scott Flansburg and can be
  27   * explained as follows:
  28   *  The year is made up of 13 months
  29   *  Each month has 28 days
  30   *  Counting of months starts from 0 (zero) so the months will run from 0 to 12
  31   *  New Years day (00) is a monthless day
  32   *  Note: Leap Years are not yet accounted for in the Human Calendar system
  33   *
  34   * @since PHP 4.0.4
  35   * @author Allan Kent <allan@lodestone.co.za>
  36   */
  37  class Date_Human
  38  {
  39  
  40      /**
  41       * Returns an associative array containing the converted date information
  42       * in 'Human Calendar' format.
  43       *
  44       * @param int day in DD format, default current local day
  45       * @param int month in MM format, default current local month
  46       * @param int year in CCYY format, default to current local year
  47       *
  48       * @access public
  49       *
  50       * @return associative array(
  51       *               hdom,       // Human Day Of Month, starting at 1
  52       *               hdow,       // Human Day Of Week, starting at 1
  53       *               hwom,       // Human Week of Month, starting at 1
  54       *               hwoy,       // Human Week of Year, starting at 1
  55       *               hmoy,       // Human Month of Year, starting at 0
  56       *               )
  57       *
  58       * If the day is New Years Day, the function will return
  59       * "hdom" =>  0
  60       * "hdow" =>  0
  61       * "hwom" =>  0
  62       * "hwoy" =>  0
  63       * "hmoy" => -1
  64       *  Since 0 is a valid month number under the Human Calendar, I have left
  65       *  the month as -1 for New Years Day.
  66       */
  67      function gregorianToHuman($day=0, $month=0, $year=0)
  68      {
  69          /**
  70           * Check to see if any of the arguments are empty
  71           * If they are then populate the $dateinfo array
  72           * Then check to see which arguments are empty and fill
  73           * those with the current date info
  74           */
  75          if ((empty($day) || (empty($month)) || empty($year))) {
  76              $dateinfo = getdate(time());
  77          }
  78          if (empty($day)) {
  79              $day = $dateinfo["mday"];
  80          }
  81          if (empty($month)) {
  82              $month = $dateinfo["mon"];
  83          }
  84          if (empty($year)) {
  85              $year = $dateinfo["year"];
  86          }
  87          /**
  88           * We need to know how many days into the year we are
  89           */
  90          $dateinfo = getdate(mktime(0, 0, 0, $month, $day, $year));
  91          $dayofyear = $dateinfo["yday"];
  92          /**
  93           * Human Calendar starts at 0 for months and the first day of the year
  94           * is designated 00, so we need to start our day of the year at 0 for
  95           * these calculations.
  96           * Also, the day of the month is calculated with a modulus of 28.
  97           * Because a day is 28 days, the last day of the month would have a
  98           * remainder of 0 and not 28 as it should be.  Decrementing $dayofyear
  99           * gets around this.
 100           */
 101          $dayofyear--;
 102          /**
 103           * 28 days in a month...
 104           */
 105          $humanMonthOfYear = floor($dayofyear / 28);
 106          /**
 107           * If we are in the first month then the day of the month is $dayofyear
 108           * else we need to find the modulus of 28.
 109           */
 110          if ($humanMonthOfYear == 0) {
 111              $humanDayOfMonth = $dayofyear;
 112          } else {
 113              $humanDayOfMonth = ($dayofyear) % 28;
 114          }
 115          /**
 116           * Day of the week is modulus 7
 117           */
 118          $humanDayOfWeek = $dayofyear % 7;
 119          /**
 120           * We can now increment $dayofyear back to it's correct value for
 121           * the remainder of the calculations
 122           */
 123          $dayofyear++;
 124          /**
 125           * $humanDayOfMonth needs to be incremented now - recall that we fudged
 126           * it a bit by decrementing $dayofyear earlier
 127           * Same goes for $humanDayOfWeek
 128           */
 129          $humanDayOfMonth++;
 130          $humanDayOfWeek++;
 131          /**
 132           * Week of the month is day of the month divided by 7, rounded up
 133           * Same for week of the year, but use $dayofyear instead $humanDayOfMonth
 134           */
 135          $humanWeekOfMonth = ceil($humanDayOfMonth / 7);
 136          $humanWeekOfYear = ceil($dayofyear / 7);
 137          /**
 138           * Return an associative array of the values
 139           */
 140          return array(
 141                       "hdom" => $humanDayOfMonth,
 142                       "hdow" => $humanDayOfWeek,
 143                       "hwom" => $humanWeekOfMonth,
 144                       "hwoy" => $humanWeekOfYear,
 145                       "hmoy" => $humanMonthOfYear );
 146      }
 147  
 148      /**
 149       * Returns unix timestamp for a given Human Calendar date
 150       *
 151       * @param int day in DD format
 152       * @param int month in MM format
 153       * @param int year in CCYY format, default to current local year
 154       *
 155       * @access public
 156       *
 157       * @return int unix timestamp of date
 158       */
 159      function HumanToGregorian($day, $month, $year=0)
 160      {
 161          /**
 162           * Check to see if the year has been passed through.
 163           * If not get current year
 164           */
 165          if (empty($year)) {
 166              $dateinfo = getdate(time());
 167              $year = $dateinfo["year"];
 168          }
 169          /**
 170           * We need to get the day of the year that we are currently at so that
 171           * we can work out the Gregorian Month and day
 172           */
 173          $DayOfYear = $month * 28;
 174          $DayOfYear += $day;
 175          /**
 176           * Human Calendar starts at 0, so we need to increment $DayOfYear
 177           * to take into account the day 00
 178           */
 179          $DayOfYear++;
 180          /**
 181           * the mktime() function will correctly calculate the date for out of
 182           * range values, so putting $DayOfYear instead of the day of the month
 183           * will work fine.
 184           */
 185          $GregorianTimeStamp = mktime(0, 0, 0, 1, $DayOfYear, $year);
 186          return $GregorianTimeStamp;
 187      }
 188  
 189  }
 190  ?>


Généré le : Mon Nov 26 21:04:15 2007 par Balluche grâce à PHPXref 0.7
  Clicky Web Analytics