home *** CD-ROM | disk | FTP | other *** search
/ Chip 2004 April / CMCD0404.ISO / Software / Freeware / Programare / dotproject / classes / date.class.php next >
Encoding:
PHP Script  |  2003-12-09  |  3.7 KB  |  145 lines

  1. <?php /* CLASSES $Id: date.class.php,v 1.9 2003/12/09 06:34:47 jcgonz Exp $ */
  2. /**
  3. * @package dotproject
  4. * @subpackage utilites
  5. */
  6.  
  7. require_once( $AppUI->getLibraryClass( 'PEAR/Date' ) );
  8.  
  9. define( 'FMT_DATEISO', '%Y%m%dT%H%M%S' );
  10. define( 'FMT_DATELDAP', '%Y%m%d%H%M%SZ' );
  11. define( 'FMT_DATETIME_MYSQL', '%Y-%m-%d %H:%M:%S' );
  12. define( 'FMT_DATERFC822', '%a, %d %b %Y %H:%M:%S' );
  13. define( 'FMT_TIMESTAMP', '%Y%m%d%H%M%S' );
  14. define( 'FMT_TIMESTAMP_DATE', '%Y%m%d' );
  15. define( 'FMT_TIMESTAMP_TIME', '%H%M%S' );
  16. define( 'FMT_UNIX', '3' );
  17. define( 'WDAY_SUNDAY',    0 );
  18. define( 'WDAY_MONDAY',    1 );
  19. define( 'WDAY_TUESDAY',   2 );
  20. define( 'WDAY_WENESDAY',  3 );
  21. define( 'WDAY_THURSDAY',  4 );
  22. define( 'WDAY_FRIDAY',    5 );
  23. define( 'WDAY_SATURDAY',  6 );
  24. define( 'SEC_MINUTE',    60 );
  25. define( 'SEC_HOUR',    3600 );
  26. define( 'SEC_DAY',    86400 );
  27.  
  28. /**
  29. * dotProject implementation of the Pear Date class
  30. *
  31. * This provides customised extensions to the Date class to leave the
  32. * Date package as 'pure' as possible
  33. */
  34. class CDate extends Date {
  35.  
  36. /**
  37. * Overloaded compare method
  38. *
  39. * The convertTZ calls are time intensive calls.  When a compare call is
  40. * made in a recussive loop the lag can be significant.
  41. */
  42.     function compare($d1, $d2, $convertTZ=false)
  43.     {
  44.         if ($convertTZ) {
  45.             $d1->convertTZ(new Date_TimeZone('UTC'));
  46.             $d2->convertTZ(new Date_TimeZone('UTC'));
  47.         }
  48.         $days1 = Date_Calc::dateToDays($d1->day, $d1->month, $d1->year);
  49.         $days2 = Date_Calc::dateToDays($d2->day, $d2->month, $d2->year);
  50.         if($days1 < $days2) return -1;
  51.         if($days1 > $days2) return 1;
  52.         if($d1->hour < $d2->hour) return -1;
  53.         if($d1->hour > $d2->hour) return 1;
  54.         if($d1->minute < $d2->minute) return -1;
  55.         if($d1->minute > $d2->minute) return 1;
  56.         if($d1->second < $d2->second) return -1;
  57.         if($d1->second > $d2->second) return 1;
  58.         return 0;
  59.     }
  60.  
  61.  
  62. /**
  63. * Adds (+/-) a number of days to the current date.
  64. * @param int Positive or negative number of days
  65. * @author J. Christopher Pereira <kripper@users.sf.net>
  66. */
  67.     function addDays( $n ) {
  68.         $this->setDate( $this->getTime() + 60 * 60 * 24 * $n, DATE_FORMAT_UNIXTIME);
  69.     }
  70.     
  71. /**
  72. * Adds (+/-) a number of months to the current date.
  73. * @param int Positive or negative number of months
  74. * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  75. */
  76.     function addMonths( $n ) {
  77.         $an = abs( $n );
  78.         $years = floor( $an / 12 );
  79.         $months = $an % 12;
  80.  
  81.         if ($n < 0) {
  82.             $this->year -= $years;
  83.             $this->month -= $months;
  84.             if ($this->month < 1) {
  85.                 $this->year--;
  86.                 $this->month = 12 - $this->month;
  87.             }
  88.         } else {
  89.             $this->year += $years;
  90.             $this->month += $months;
  91.             if ($this->month > 12) {
  92.                 $this->year++;
  93.                 $this->month -= 12;
  94.             }
  95.         }
  96.     }    
  97.  
  98. /**
  99. * New method to get the difference in days the stored date
  100. * @param Date The date to compare to
  101. * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  102. */
  103.     function dateDiff( $when ) {
  104.         return Date_calc::dateDiff(
  105.             $this->getDay(), $this->getMonth(), $this->getYear(),
  106.             $when->getDay(), $when->getMonth(), $when->getYear()
  107.         );
  108.     }
  109.  
  110. /**
  111. * New method that sets hour, minute and second in a single call
  112. * @param int hour
  113. * @param int minute
  114. * @param int second
  115. * @author Andrew Eddie <eddieajau@users.sourceforge.net>
  116. */
  117.     function setTime( $h=0, $m=0, $s=0 ) {
  118.         $this->setHour( $h );
  119.         $this->setMinute( $m );
  120.         $this->setSecond( $s );
  121.     }
  122.     
  123.     function isWorkingDay(){
  124.         global $AppUI;
  125.         
  126.         $working_days = $AppUI->getConfig("cal_working_days");
  127.         if(is_null($working_days)){
  128.             $working_days = array('1','2','3','4','5');
  129.         } else {
  130.             $working_days = explode(",", $working_days);
  131.         }
  132.         
  133.         return in_array($this->getDayOfWeek(), $working_days);
  134.     }
  135.     
  136.     function getAMPM() {
  137.         if ( $this->getHour() > 11 ) {
  138.             return "pm";
  139.         } else {
  140.             return "am";
  141.         }
  142.     }
  143. }
  144. ?>
  145.