[ 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/ -> timestamp.class.php (source)

   1  <?php
   2  
   3       
   4       lt_include( PLOG_CLASS_PATH."class/config/config.class.php" );
   5       lt_include( PLOG_CLASS_PATH."class/locale/locales.class.php" );
   6       lt_include( PLOG_CLASS_PATH."class/data/Date.class.php" );
   7  
   8       /**
   9        * constants used to determine whether we are using dynamic time
  10        * difference calculation or static time difference calculation (that is,
  11        * dates are stored 
  12        */
  13        define( "TIME_DIFFERENCE_CALCULATION_DYNAMIC", 0 );     
  14       define( "TIME_DIFFERENCE_CALCULATION_STATIC", 1 );
  15  
  16  
  17      /**
  18       * \ingroup Data
  19       *
  20       * This class allows to deal with all sorts of different date formats, as many as the PEAR::Date class
  21       * supports (Timestamp started as an independent class but later on due to requirements it was developed
  22       * as an extension for PEAR::Date, maintaining the previous interface for compatibility reasons)
  23       *
  24       * PEAR::Date has its own documentation available here: http://pear.php.net/package/Date/docs/1.4.2/Date/Date.html
  25       * 
  26       * The formats supported by PEAR::Date are:
  27       *
  28       * - DATE_FORMAT_ISO (YYYY-MM-DD HH:MM:SS)
  29       * - DATE_FORMAT_ISO_BASIC (YYYYMMSSTHHMMSS(Z|(+/-)HHMM)?)
  30       * - DATE_FORMAT_ISO_EXTENDED (YYYY-MM-SSTHH:MM:SS(Z|(+/-)HH:MM)?)
  31       * - DATE_FORMAT_ISO_EXTENDED_MICROTIME (YYYY-MM-SSTHH:MM:SS(.S*)?(Z|(+/-)HH:MM)?)
  32       * - DATE_FORMAT_TIMESTAMP (YYYYMMDDHHMMSS)
  33       * - DATE_FORMAT_UNIXTIME (seconds since the unix epoch)
  34       *
  35       * Use the static method Timestamp::parseDate() in order to use other date formats which are not directly
  36       * supported by this class.
  37       *
  38       * @see Date
  39       * @see Timestamp::parseDate()
  40       */
  41      class Timestamp extends Date 
  42      {
  43  
  44          // internal variables
  45          var $_timestamp;
  46  
  47          /*
  48           * Creates a Timestamp object
  49           * If $timestamp is empty or not specified, creates a timestamp
  50           * taking the current time
  51           *
  52           * @param timestamp a valid SQL timestamp (DATE_FORMAT_TIMESTAMP / YYYYMMDDHHMMSS)
  53           */
  54  		function Timestamp( $timestamp = null )
  55          {
  56              //if( $timestamp == "" ) $timestamp = null;
  57              $this->Date( $timestamp );
  58          }
  59  
  60          /**
  61           * @deprecated Use setDate instead
  62           */
  63  		function setTime( $timestamp, $format = DATE_FORMAT_ISO )
  64          {
  65              $this->setDate( $timestamp, $format );
  66          }
  67  
  68          /**
  69           * @deprecated Use getDate instead
  70           */
  71  		function getTimestamp( $format = DATE_FORMAT_TIMESTAMP )
  72          {
  73              return $this->getDate( $format );
  74          }
  75  
  76  
  77          /**
  78           * Returns the century corresponding to the given date. If the current year is '2003', then the
  79           * century will be '2000', not 21.
  80           *
  81           * @return An integer value representing the current century.
  82           * @deprecated
  83           */
  84  		function getCentury()
  85          {
  86              //return $this->_century;
  87              throw( new Exception("not implemented?"));
  88              die();
  89          }
  90  
  91          /**
  92           * Returns only the minutes specified by the current date.
  93           *
  94           * @return The minutes specified by the current date.
  95           */
  96  		function getMinutes()
  97          {
  98              return $this->getMinute();
  99          }
 100  
 101          /**
 102           * Returns only the seconds specified by the current date.
 103           *
 104           * @return The seconds specified by the current date.
 105           */
 106  		function getSeconds()
 107          {
 108              return $this->getSecond();
 109          }
 110  
 111          /**
 112            * Returns the name of the month using the current locale.
 113           *
 114           * @return A string with the name of the month.
 115            */
 116  		function getMonthString()
 117          {
 118              throw( new Exception("Timestamp::getMonthString not implemented. Use Locale::formatDate instead!"));
 119              die();
 120          }
 121  
 122          /**
 123           * @return Returns the current month, as a value from "00" to "12"
 124           */
 125          function getMonth()
 126          {
 127              // call the parent getMonth() method
 128              $month = Date::getMonth();
 129              if( $month < 10 && $month[0] != "0" )
 130                  $month = "0".$month;
 131  
 132              return $month;
 133          }
 134  
 135          /**
 136           * Sets a new value for the minutes in this timestamp
 137           *
 138           * @param newMinutes the new value for the minutes
 139           */
 140          function setMinutes( $newMinutes )
 141          {
 142              $this->setMinute( $newMinutes );
 143          }
 144  
 145          /**
 146           * Instead of returning a string it will just return 0 for sunday, 1 for monday,
 147           * 2 for tuesday, 3 for wednesday and so on...
 148           * @deprecated
 149           * @private
 150           */
 151  		function getWeekdayId()
 152          {
 153              return $this->getDayOfWeek();
 154          }
 155  
 156          /**
 157           * @private
 158           */
 159  		function getNextMonthAndYear()
 160          {
 161              if( $this->_month == 12 ) {
 162                  $month = "01";
 163                  $year  = $this->_year+1;
 164              }
 165              else {
 166                  $month = $this->_month+1;
 167                  if( $month < 10 )
 168                      $month = "0".$month;
 169                  $year = $this->_year;
 170              }
 171  
 172              return $year.$month;
 173          }
 174  
 175          /**
 176           * @private
 177           */
 178  		function setNextMonthAndYear()
 179          {
 180              $result = $this->getNextMonthAndYear();
 181  
 182              $this->setYear( substr( $result, 0, 4 ));
 183              $this->setMonth( substr( $result, 4, 2 ));
 184  
 185              $this->_calculateFields();
 186          }
 187  
 188          /**
 189           * @private
 190           */
 191  		function getPrevMonthAndYear()
 192          {
 193              if( $this->_month == 01 ) {
 194                  $month = 12;
 195                  $year = $this->_year-1;
 196              }
 197              else {
 198                  $month = $this->_month - 1;
 199                  if( $month < 10 )
 200                      $month = "0".$month;
 201                  $year = $this->_year;
 202              }
 203  
 204              return $year.$month;
 205          }
 206  
 207          /**
 208           * @private
 209           */
 210  		function setPrevMonthAndYear()
 211          {
 212              $result = $this->getPrevMonthAndYear();
 213  
 214              $this->setYear( substr( $result, 0, 4 ));
 215              $this->setMonth( substr( $result, 4, 2 ));
 216  
 217              $this->_calculateFields();
 218          }
 219  
 220          /**
 221           * Returns the UNIX timestamp for the given date.
 222           *
 223           * @return An integer specifying the unix timestamp for the given date.
 224           */
 225          function getUnixDate()
 226          {
 227              return $this->getDate( DATE_FORMAT_UNIXTIME );
 228          }
 229  
 230          /**
 231           * Returns the date formatted in ISO 8601
 232           *
 233           * @return A string with the date in format ISO 8601
 234           */
 235          function getIsoDate()
 236          {
 237              return $this->getDate( DATE_FORMAT_ISO );
 238          }
 239          
 240          /**
 241           * returns the date formatted according to the W3 specifications
 242           * @see http://www.w3.org/TR/NOTE-datetime
 243           */
 244          function getW3Date()
 245          {
 246              return $this->getDate( DATE_FORMAT_ISO_EXTENDED );
 247          }
 248  
 249          /**
 250           * Static method that returns a timestamp after applying a time
 251           * difference to it.
 252           *
 253           * @static
 254           * @param timeStamp The original ISO timestamp
 255           * @param timeDiff The time difference that we'd like to apply to the
 256           * original timestamp
 257           */
 258          function getDateWithOffset( $timeStamp, $timeDiff )
 259          {
 260              if( $timeDiff != 0 ) {
 261                  $t = new Timestamp( $timeStamp );
 262                  //
 263                  // we can't use the addSeconds method with a negative offset
 264                  // so we have to check wether the offset is positive or negative
 265                  // and then use the correct one...
 266                  //
 267                  if( $timeDiff > 0 )
 268                      $t->addSeconds( $timeDiff * 3600 );
 269                  else
 270                      $t->subtractSeconds( $timeDiff * (-3600));
 271  
 272                  $date = $t->getTimestamp();
 273              }
 274              else {
 275                  $date = $timeStamp;
 276              }
 277  
 278              return $date;
 279          }
 280          
 281          /**
 282           * Equivalent to Timestamp::getDateWithOffset but instead of returning a date,
 283           * it returns a Timestamp object with the given time difference applied to the starting
 284           * timestamp
 285           *
 286           * @param timeStamp
 287           * @param timeDiff
 288           * @see getDateWithOffset
 289           * @return A Timestamp object
 290           */
 291          function getTimestampWithOffset( $timeStamp, $timeDiff )
 292          {
 293              return( new Timestamp( Timestamp::getDateWithOffset( $timeStamp, $timeDiff )));
 294          }
 295          
 296          /**
 297           * @static
 298           * returns a Timestamp object with the blog time difference already
 299           * applied, if needed
 300           *
 301           * @param blog either a blog id or a BlogInfo object
 302           * @param timestamp 
 303           * @return A Timestamp object with time difference applied, if needed
 304           * @see BlogInfo
 305           */
 306          function getBlogDate( $blog, $timestamp = null )
 307          {
 308             // check whether time differences are dynamically or statically
 309             // applied, because in case of the former, we don't have to do
 310             // anything here!
 311             $config =& Config::getConfig();
 312             if( $config->getValue( "time_difference_calculation" ) == TIME_DIFFERENCE_CALCULATION_DYNAMIC )
 313                     return( new Timestamp( $timestamp ));
 314  
 315             //
 316             // how's this for function overloading??
 317             // I know it's quite hackish, but it's a bit of a pain that
 318             // we need to define two different functions depending on whether
 319             // we're getting an object or an integer!
 320             //
 321             if( is_object( $blog )) {
 322                  $blogSettings = $blog->getSettings();
 323                     $timeDifference = $blogSettings->getValue( "time_offset" );
 324                } 
 325                else {
 326                        lt_include( PLOG_CLASS_PATH."class/dao/blogs.class.php" );
 327                     $blogs = new Blogs();
 328                     $blogInfo = $blogs->getBlogInfoById( $blog );
 329                     if( !$blogInfo )
 330                         $timeDifference = 0;
 331                     else {
 332                         $blogSettings = $blogInfo->getSettings();
 333                         $timeDifference = $blogSettings->getValue( "time_offset" );    
 334                     }
 335             }
 336             
 337             // generate the date with the correct time difference applied
 338             $t = new Timestamp();           
 339             $t->setDate( Timestamp::getDateWithOffset( $t->getDate(), $timeDifference ), DATE_FORMAT_TIMESTAMP );
 340             
 341             return $t;
 342          }
 343          
 344          /**
 345           * stupid function that returns an array with all the hours
 346           *
 347           * @return an array
 348           */
 349      	function getAllHours()
 350          {
 351              $hours = Array( "00", "01", "02", "03", "04", "05", "06", "07", "08",
 352                              "09", "10", "11", "12", "13", "14", "15", "16", "17",
 353                              "18", "19", "20", "21", "22", "23" );            
 354                              
 355              return $hours;
 356          }
 357          
 358          /**
 359           * stupid function that returns an array with... the minutes!
 360           *
 361           * @return an array
 362           */
 363      	function getAllMinutes()
 364          {
 365              $minutes = Array( "00", "01", "02", "03", "04", "05", "06", "07", "08", "09",
 366                                "10", "11", "12", "13", "14", "15", "16", "17", "18", "19",
 367                                "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
 368                                "30", "31", "32", "33", "34", "35", "36", "37", "38", "39",
 369                                "40", "41", "42", "43", "44", "45", "46", "47", "48", "49",
 370                                "50", "51", "52", "53", "54", "55", "56", "57", "58", "59" );            
 371                                
 372              return $minutes;
 373          }
 374          
 375          /**
 376           * returns the current time as a mysql timestamp
 377           *
 378           * @static
 379           */
 380      	function getNowTimestamp()
 381          {
 382              $t = new Timestamp();
 383              return( $t->getTimestamp());
 384          }
 385          
 386          /**
 387           * returns an array with a range of years
 388           *
 389           * @param an array
 390           */
 391      	function getYears( $minYear = 1900, $maxYear = 2099 )
 392          {
 393              return( range( $minYear, $maxYear ));    
 394          }
 395          
 396          /**
 397           * Given a string with a date in whatever format, returns a valid Timestamp object
 398           * The date will be parsed via PHP's strtotime()
 399           * (http://fi2.php.net/manual/en/function.strtotime.php)
 400           *
 401           * @param dateString a string representing a date.
 402           * @return a valid Timestamp object
 403           * @static
 404           */
 405          function parseDate( $dateString )
 406          {
 407              // parse the date via strtotime
 408              $timestamp = strtotime( $dateString );            
 409              
 410              // convert the unix timestamp to a Timestamp object
 411              $date = new Timestamp( $timestamp );
 412              
 413              // and return the whole thing
 414              return( $date );
 415          }         
 416      }
 417  ?>


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