Source for file OCSP_DATE.phpclass

Documentation is available at OCSP_DATE.phpclass

  1. <?php
  2. /**
  3.   * Common Date Functions and Class
  4.   *
  5.   * @project    Open CSP-Management
  6.   * @package    common
  7.   * @category   datetime
  8.   *
  9.   * @author     Peter Krebs (pk)<pitlinz@users.sourceforge.net>
  10.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  11.   *
  12.   * @version pk-06-05-23
  13.   * @version pk-07-03-12
  14.   * @version pk-07-06-17
  15.   *
  16.   ***/
  17.  
  18.  
  19. /**
  20.   * Date Class
  21.   *
  22.   * Note: it seams that the php function mktime has problems with dates > 2037
  23.   *
  24.   * @project    Open CSP-Management
  25.   * @package    common
  26.   * @category   datetime
  27.   *
  28.   * @author     Peter Krebs (pk)<pitlinz@users.sourceforge.net>
  29.   * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
  30.   *
  31.   * @version pk-07-03-12
  32.   * @version pk-07-06-17
  33.   ***/
  34. class OCSP_DATE {
  35.  
  36.     /**
  37.       * @var int $myYear 
  38.       * @access protected
  39.       */
  40.     protected $myYear    =0;
  41.  
  42.     /**
  43.       * @var int $myMonth 
  44.       * @access protected
  45.       */
  46.     protected $myMonth   =0;
  47.  
  48.     /**
  49.       * @var int $myDay 
  50.       * @access protected
  51.       */
  52.     protected $myDay     =0;
  53.  
  54.     /**
  55.       * @var int $myHour 
  56.       * @access protected
  57.       */
  58.     protected $myHour    =0;
  59.  
  60.     /**
  61.       * @var int $myMinute 
  62.       * @access protected
  63.       */
  64.     protected $myMinute  =0;
  65.  
  66.     /**
  67.       * @var int $mySecond 
  68.       * @access protected
  69.       */
  70.     protected $mySecond  =0;
  71.  
  72.     /**
  73.       * constructor
  74.       *
  75.       * @param boolean $setNow (if true the current timestamp is set to the object
  76.       *
  77.       * @returns OCSP_DATE
  78.       *
  79.       * @access public
  80.       */
  81.     function OCSP_DATE($setNow=True{
  82.         if ($setNow$this->setLinuxTime(time());
  83.     }
  84.  
  85.     function setNULL({
  86.         $this->myYear     =0;
  87.         $this->myMonth    =0;
  88.         $this->myDay      =0;
  89.         $this->myHour     =0;
  90.         $this->myMinute   =0;
  91.         $this->mySecond   =0;
  92.     }
  93.  
  94.     function setNow({
  95.         $this->setLinuxTime(time());
  96.         // echo "<pre>setNow()\n";print_r($this);echo "</pre>";
  97.     }
  98.  
  99.     /**
  100.       * returns if the Date is null
  101.       *
  102.       * NOTE: if year is 0 and day or month lower 1 the date is considered as null
  103.       *
  104.       * @return boolean 
  105.       *
  106.       * @version pk-05-07-17
  107.       * @version pk-06-08-08 $this->myYear == 0 &&
  108.       * @version pk-06-11-27 return true if  ((intval($this->myDay) < 1) || (intval($this->myMonth) < 1))
  109.       *
  110.       ***/
  111.     function isNULL({
  112.         if ((intval($this->myYear== && ((intval($this->myDay1|| (intval($this->myMonth1))) {
  113.             return True;
  114.         else if  ((intval($this->myDay1&& (intval($this->myMonth1)) // <pk-06-11-27>
  115.             return True;
  116.         else {
  117.             if (($this->getLinuxTime(False True)) {
  118.                 return True;
  119.             }
  120.             // <pk-06-08-08> it seams mktime has problems with dates > 2037
  121.             // return ($this->getLinuxTime() ? False : True);
  122.             return False;
  123.         }
  124.     }
  125.  
  126.  
  127.     
  128.     /**
  129.       * sets the date to a linux timestamp
  130.       *
  131.       * @var double $aTime if 0 -> the date is set to NULL
  132.       *
  133.       * @returns boolean True if not zero else false
  134.       *
  135.       */
  136.     function setLinuxTime($aTime=0{
  137.         if (empty($aTime)) {
  138.             $this->setNULL();
  139.             return False;
  140.         else {
  141.             $this->myYear       =intval(date("Y",$aTime));
  142.             $this->myMonth      =intval(date("m",$aTime));
  143.             $this->myDay        =intval(date("d",$aTime));
  144.             $this->myHour       =intval(date("H",$aTime));
  145.             $this->myMinute     =intval(date("i",$aTime));
  146.             $this->mySecond     =intval(date("s",$aTime));
  147.             // echo "<pre>";print_r($this);echo "</pre>";
  148.             return True;
  149.         }
  150.     }
  151.  
  152.     function getLinuxTime({
  153.         return mktime($this->myHour,$this->myMinute,$this->mySecond,$this->myMonth,$this->myDay,$this->myYear);
  154.     }
  155.  
  156.     function setLinuxTimeStamp($aTime=0{
  157.         return $this->setLinuxTime($aTime);
  158.     }
  159.  
  160.     function getLinuxTimeStamp({
  161.         return $this->getLinuxTime();
  162.     }
  163.  
  164.     /**
  165.       * sets the date from a db string
  166.       *
  167.       * @param string $dbValue 
  168.       * @param boolean $debug 
  169.       *
  170.       * @return boolean 
  171.       */
  172.     function setDbString($dbValue,$debug=False)
  173.     {
  174.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_DATE::setDBString($dbValue)");
  175.  
  176.         $this->setNULL();
  177.         if (empty($dbValue)) {
  178.             return True;
  179.         }
  180.  
  181.         if (ereg("^([0-9]{4}).([0-9]{1,2}).([0-9]{1,2}).([0-9]{1,2}):([0-9]{1,2}):([0-9]{1,2})(.*)",$dbValue,$arr_res))
  182.         {
  183.             $this->myYear=$arr_res[1];
  184.             $this->myMonth=$arr_res[2];
  185.             $this->myDay=$arr_res[3];
  186.             $this->myHour=$arr_res[4];
  187.             $this->myMinute=$arr_res[5];
  188.             $this->mySecond=$arr_res[6];
  189.             return True;
  190.         }
  191.  
  192.         if (ereg("^([0-9]{4}).([0-9]{1,2}).([0-9]{1,2})(.*)",$dbValue,$arr_res))
  193.         {
  194.             $this->myYear=$arr_res[1];
  195.             $this->myMonth=$arr_res[2];
  196.             $this->myDay=$arr_res[3];
  197.             $this->myHour=0;
  198.             $this->myMinute=0;
  199.             $this->mySecond=0;
  200.             return True;
  201.         }
  202.  
  203.         return False;
  204.     }
  205.  
  206.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  207.     // year methods
  208.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  209.  
  210.     /**
  211.       * returns the year as integer (4 digets)
  212.       * @returns int
  213.       ***/
  214.     function getYear({
  215.         return $this->myYear;
  216.     }
  217.  
  218.     /**
  219.       * sets the year as integer (4 digets)
  220.       * @param int $aYear 
  221.       ***/
  222.     function setYear($aYear{
  223.         $this->myYear=intval($aYear);
  224.     }
  225.  
  226.     /**
  227.       * returns if $this->myYear is a leap year (feb 29 days)
  228.       *
  229.       * @returns boolean
  230.       *
  231.       * @since pk-06-07-17
  232.       *
  233.       ***/
  234.     function isLeapYear()  {                
  235.         if (($this->myYear % 4!= 0return False;
  236.         if (($this->myYear % 400== 0return True;
  237.         if (($this->myYear % 100== 0return False;
  238.         return True;
  239.     }    
  240.     
  241.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  242.     // month methods
  243.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  244.  
  245.     /**
  246.       * returns the month as integer (1..12)
  247.       *
  248.       * @param boolean $with2Digets add leading zero?
  249.       *
  250.       * @returns int
  251.       *
  252.       * @version pk-06-08-08
  253.       *
  254.       ***/
  255.     function getMonth({
  256.         return intval($this->myMonth);
  257.     }
  258.  
  259.     /**
  260.       * sets the month
  261.       * @param int $aMonth 
  262.       ***/
  263.     function setMonth($aMonth{
  264.         $this->myMonth=intval($aMonth);
  265.     }
  266.  
  267.     /**
  268.       * returns the monthname from the global array $OCSP_VAL['monthNames']
  269.       * 
  270.       * if $aMonth > 0 the method can be called as static function
  271.       *
  272.       * @param int $aMonth 
  273.       * @param string $lang 
  274.       * 
  275.       * @global array $OCSP_VAL 
  276.       * 
  277.       * @returns string
  278.       */
  279.     function getMonthName($aMonth=0,$lang="DE"{
  280.         global $OCSP_VAL;
  281.         
  282.         if (!intval($aMonth)) $aMonth=$this->myMonth;
  283.         require_once dirname(__FILE__)._OCSP_DIRSEP_."languages"._OCSP_DIRSEP_.strtoupper($lang)."_calendar.phpinc";
  284.         return $OCSP_VAL['monthNames'][$aMonth];
  285.     }
  286.  
  287.     /**
  288.       * returns a short monthname (4 letters or 3 letter and dot)
  289.       *
  290.       * @return string 
  291.       * @since pk-04-03-10
  292.       *
  293.       ***/
  294.     function getMonthNameShort({
  295.         if (strlen($GLOBALS['MONTHNAMES'][$this->myMonth]5)
  296.             return $GLOBALS['MONTHNAMES'][$this->myMonth];
  297.         else
  298.             return substr($GLOBALS['MONTHNAMES'][$this->myMonth],0,3).".";
  299.     }
  300.  
  301.     /**
  302.       * returns the days of the current month
  303.       *
  304.       * @return int 
  305.       *
  306.       * @since pk-07-10-20
  307.       *
  308.       */
  309.     function getMonthNofDays()
  310.     {
  311.         switch($this->myMonth)
  312.         {
  313.             case 2:
  314.                 if ($this->isLeapYear())
  315.                 {
  316.                     return 29;
  317.                 else {
  318.                     return 28;
  319.                 }
  320.             case 1:
  321.             case 3:
  322.             case 5:
  323.             case 7:
  324.             case 8:
  325.             case 10:
  326.             case 12:
  327.                 return 31;
  328.             default:
  329.                 return 30;
  330.         }
  331.     }
  332.  
  333.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  334.     // week methods
  335.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  336.  
  337.     /**
  338.       * returns the ISO-8601 week number
  339.       *
  340.       * @returns int
  341.       *
  342.       * @since pk-06-06-05
  343.       *
  344.       ***/
  345.     function getWeekNr({
  346.         return intval(date("W",$this->getLinuxTime()));
  347.     }
  348.  
  349.     /**
  350.       * returns the ISO-8601 week number of
  351.       * the week of the 1st day of a month
  352.       *
  353.       * @returns int
  354.       *
  355.       * @since pk-06-06-05
  356.       *
  357.       ***/
  358.     function getMonthFirstWeekNr({
  359.         return date("W",mktime(0,0,0,$this->myMonth,1,$this->myYear));
  360.     }
  361.  
  362.     /**
  363.       * calculate the first monday of year due to ISO8601 and returns
  364.       * the linux timestamp of it
  365.       *
  366.       * @returns double
  367.       *
  368.       * @since pk-06-03-08
  369.       *
  370.       ***/
  371.     function getFirstMondayOfYear({
  372.         $i_wday date('w',mktime(0,0,0,1,1,$this->myYear));
  373.         if ($i_wday <= 4{
  374.             // Sunday .. Wednesday calc back
  375.             return mktime(0,0,0,1,1-($i_wday-1),$this->myYear);
  376.         else {
  377.             // Thursday .. Saturday
  378.             return mktime(0,0,0,1,1+(7-$i_wday+1),$this->myYear);
  379.         }
  380.     }
  381.  
  382.     /**
  383.       * sets to the Monday in week (ISO8601)
  384.       *
  385.       * @param int $week 
  386.       * @param int $year 
  387.       *
  388.       * @since pk-06-03-08
  389.       *
  390.       ***/
  391.     function setMondayOfWeek($week,$year=0{
  392.         if (!intval($year)) $year=$this->getYear();
  393.         if (!intval($year)) $year=intval(date("Y",time()));
  394.         $this->setYear(intval($year));
  395.         $d_firstMonday=$this->getFirstMondayOfYear();
  396.         $i_days (intval($week)-1)*7;
  397.         $this->setLinuxTime(mktime(0,0,0,date('m',$d_firstMonday),date('d',$d_firstMonday)+$i_days,$this->getYear()));
  398.     }
  399.  
  400.     /**
  401.       * sets to the Monday 00:00:00 of the current week
  402.       *
  403.       * @since pk-06-03-09
  404.       *
  405.       ***/
  406.     function setMonday({
  407.         switch($this->getWeekDay()) {
  408.             case 0:
  409.                 $i_days=6;
  410.                 break;
  411.             case 1:
  412.                 $i_days=0;
  413.                 break;
  414.             default:
  415.                 $i_days=($this->getWeekDay()-1);
  416.         }
  417.         $this->setLinuxTime(mktime(0,0,0,$this->myMonth,$this->myDay-$i_days,$this->getYear()));
  418.     }
  419.  
  420.     /**
  421.       * Numeric representation of the day of the weekday
  422.       * 0 (for Sunday) through 6 (for Saturday)
  423.       *
  424.       * @returns int
  425.       *
  426.       * @version pk-05-12-23
  427.       *
  428.       ***/
  429.      function getWeekDay({
  430.         // if ($this->isHoliday()) return 0;
  431.         return intval(date("w",$this->getLinuxTime()));
  432.     }
  433.  
  434.     /**
  435.       * returns if the day is a workday or not
  436.       * returns True for Monday - Friday
  437.       * returns False for Saturday, Sunday and holidays
  438.       *
  439.       * @returns boolean
  440.       *
  441.       * @version pk-05-12-23
  442.       ***/
  443.     function isWorkDay({
  444.         if ($this->isHoliday()) return False// pk-05-12-23
  445.         $wd=$this->getWeekDay();
  446.         return (($wd && $wd 6True False);
  447.     }
  448.     
  449.     
  450.  
  451.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  452.     // day methods
  453.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  454.  
  455.     /**
  456.       * returns the day (of month [1..31])
  457.       * @returns int
  458.       ***/
  459.     function getDay({
  460.         return $this->myDay;
  461.     }
  462.  
  463.     /**
  464.       * sets the day
  465.       * @param int $aMonth 
  466.       * @since pk-06-04-20
  467.       ***/
  468.     function setDay($aDay{
  469.         $this->myDay=intval($aDay);
  470.     }
  471.  
  472.     /**
  473.       * returns the day of the week (0 Sunday,1 Monday,....6 Saturday)
  474.       * 
  475.       * taken from: http://www.terra.es/personal2/grimmer/
  476.       * 
  477.       * @return int 
  478.       ***/
  479.     function getDayOfWeek({
  480.         $i_y1=intval(substr(strval($this->myYear),0,2));
  481.         $i_y2=intval(substr(strval($this->myYear),-2));
  482.         
  483.         $i_ret=$i_y2+floor($i_y2/4);
  484.         switch($i_y1)
  485.         {
  486.             case "17":
  487.                 $i_ret $i_ret+5;
  488.                 break;
  489.             case "18":
  490.                 $i_ret $i_ret+3;                    
  491.                 break;
  492.             case "19":
  493.                 $i_ret $i_ret+1;                    
  494.                 break;
  495.             case "21":
  496.                 $i_ret $i_ret-2;                    
  497.                 break;
  498.             case "22":
  499.                 $i_ret $i_ret-4;                    
  500.                 break;               
  501.         }
  502.         
  503.                 
  504.         $arr_mCode=array(1=>6,2=>2,3=>2,4=>5,5=>0,6=>3,7=>5,8=>1,9=>4,10=>6,11=>2,12=>4);
  505.         
  506.         $i_ret $i_ret+$arr_mCode[$this->myMonth]+$this->myDay;
  507.                       
  508.         if ($this->isLeapYear(&& $this->myMonth < 3)
  509.         {
  510.             $i_ret $i_ret -1;
  511.         }
  512.         
  513.         while($i_ret 0$i_ret=$i_ret+7;
  514.         $i_ret=$i_ret 7;
  515.         return $i_ret;
  516.         
  517.         //return date('w',$this->getLinuxTime());
  518.     }
  519.     
  520.     /**
  521.      * @param int $dayNr 
  522.      * @param string $lang 
  523.      * 
  524.      * @global array $OCSP_VAL 
  525.      * 
  526.      * @return string 
  527.      */
  528.     function getDayNameShort($dayNr=-1,$lang="DE")
  529.     {
  530.         global $OCSP_VAL;
  531.         
  532.         if ($dayNr<0$dayNr=$this->getDayOfWeek();
  533.         require_once dirname(__FILE__)._OCSP_DIRSEP_."languages"._OCSP_DIRSEP_.strtoupper($lang)."_calendar.phpinc";
  534.         return $OCSP_VAL['dayNameShort'][$dayNr];        
  535.     }
  536.     
  537.     /**
  538.      * returns if the current date is a holiday
  539.      * 
  540.      * @param string $country 
  541.      * @global $OCSP_VAL 
  542.      * 
  543.      * @return array (the ical entry)
  544.      */
  545.     function isHoliday($country="AT")
  546.     {
  547.         global $OCSP_VAL;
  548.         
  549.         if (!is_array($OCSP_VAL['holidays_'.$country]))
  550.         {
  551.             $OCSP_VAL['holidays_'.$country]=OCSP_DATE::iCalDecoder(dirname(__FILE__)._OCSP_DIRSEP_."languages"._OCSP_DIRSEP_.strtoupper($country)."_Holidays.ics");
  552.             //print_r($OCSP_VAL['holidays_'.$country]);
  553.         }
  554.  
  555.         $str_date=sprintf ("%04d%02d%02d",$this->myYear,$this->myMonth,$this->myDay);
  556.         $str_mday=sprintf ("%02d%02d",$this->myMonth,$this->myDay);
  557.         
  558.         foreach($OCSP_VAL['holidays_'.$countryas $a_ical)
  559.         {
  560.             if (substr(trim($a_ical['DTSTART']['VALUE']),0,8== $str_date)
  561.             {
  562.                 return $a_ical;
  563.             else if ($a_ical['RRULE']['FREQ'== 'YEARLY'{
  564.                 //echo "\n\n".substr(trim($a_ical['DTSTART']['VALUE']),4,4)." == $str_mday \n\n";                                
  565.                 if (substr(trim($a_ical['DTSTART']['VALUE']),4,4== $str_mday)
  566.                 {
  567.                     return $a_ical;
  568.                 }                
  569.             
  570.         }
  571.         
  572.         return False;
  573.         
  574.     }
  575.         
  576.     
  577.  
  578.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  579.     // time methods
  580.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  581.  
  582.     /**
  583.       * @return int 
  584.       *
  585.       * @since pk-06-08-08
  586.       *
  587.       ***/
  588.     function getHour({
  589.         return intval($this->myHour);
  590.  
  591.     }
  592.  
  593.     /**
  594.       * @return int 
  595.       *
  596.       * @since pk-06-08-08
  597.       *
  598.       ***/
  599.     function getMinutes({
  600.         return intval($this->myMinute);
  601.  
  602.     }
  603.  
  604.     /**
  605.       * @return int 
  606.       *
  607.       * @since pk-06-08-08
  608.       *
  609.       ***/
  610.     function getSeconds({
  611.         return intval($this->mySecond);
  612.  
  613.     }
  614.  
  615.  
  616.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  617.     // mysql_timestamp methods
  618.     // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  619.  
  620.     /**
  621.       * sets the current date out of a mysql timestamp value
  622.       *
  623.       * @param string $mysql_timestamp (YYYYMMDDHHMMSS)
  624.       *
  625.       */
  626.     function setMySqlTimeStamp($mysql_timestamp{
  627.         // YYYYMMDDHHMMSS
  628.  
  629.         if (ereg("^([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})([0-9]{2})",$mysql_timestamp,$res)) {
  630.             $this->myYear=$res[1];
  631.             $this->myMonth=$res[2];
  632.             $this->myDay=$res[3];
  633.             $this->myHour=$res[4];
  634.             $this->myMinute=$res[5];
  635.             $this->mySecond=$res[6];
  636.         }
  637.     }
  638.  
  639.     function getMySqlTimeStamp ({
  640.         $retStr"".$this->myYear;
  641.         $retStr.= pcf_addzero($this->myMonth,2);
  642.         $retStr.= pcf_addzero($this->myDay,2);
  643.         $retStr.= pcf_addzero($this->myHour,2);
  644.         $retStr.= pcf_addzero($this->myMinute,2);
  645.         $retStr.= pcf_addzero($this->mySecond,2);
  646.  
  647.         return $retStr;
  648.     }
  649.  
  650.     /**
  651.       * returns the date as string with format $aFormat
  652.       * for $aFormat see http://at.php.net/manual/en/function.date.php
  653.       *
  654.       * @param string $aFormat 
  655.       * @param boolean $debug 
  656.       *
  657.       * @return string 
  658.       *
  659.       * @version pk-06-08-08 check if the year > 2036
  660.       * @version pk-06-08-18 check for NULL
  661.       *
  662.       * @todo implement format according to: http://www.php.net/manual/en/function.date.php
  663.       *
  664.       ***/
  665.     function dateStr($aFormat="r",$debug=False{
  666.         if ($this->isNULL()) return ""// <pk-08-18 />
  667.         if (intval($this->myYear < 2037)) {
  668.             if (empty($aFormat)) $aFormat="r";
  669.             return date($aFormat,$this->getLinuxTime());
  670.         else {
  671.             // day ---------------------------------------
  672.             // year
  673.             $s_ret=str_replace("Y",sprintf("%04d",$this->myYear),$aFormat);
  674.             $s_ret=str_replace("y",sprintf("%02d",$this->myYear),$s_ret);
  675.             // month
  676.             $s_ret=str_replace("d",sprintf("%02d",$this->myMonth),$s_ret);
  677.             $s_ret=str_replace("j",sprintf("%d",$this->myMonth),$s_ret);
  678.             // day
  679.             $s_ret=str_replace("m",sprintf("%02d",$this->myDay),$s_ret);
  680.             $s_ret=str_replace("n",sprintf("%d",$this->myDay),$s_ret);
  681.  
  682.             // time ---------------------------------------
  683.             // hours
  684.             $s_ret=str_replace("H",sprintf("%02d",$this->myHour),$s_ret);
  685.             $s_ret=str_replace("h",sprintf("%d",$this->myHour),$s_ret);
  686.             // minutes
  687.             $s_ret=str_replace("i",sprintf("%02d",$this->myMinute),$s_ret);
  688.             // seconds
  689.             $s_ret=str_replace("s",sprintf("%02d",$this->mySecond),$s_ret);
  690.  
  691.             return $s_ret;
  692.         }
  693.     }
  694.  
  695.  
  696.     function getArray({
  697.         $ret=array();
  698.         if ($this->isNULL()) return $ret;
  699.  
  700.         $ret['YEAR']    =$this->myYear;
  701.         $ret['MONTH']   =$this->myMonth;
  702.         $ret['DAY']     =$this->myDay;
  703.         $ret['HOUR']    =$this->myHour;
  704.         $ret['MIN']     =$this->myMinute;
  705.         $ret['SEC']     =$this->mySecond;
  706.  
  707.         return $ret;
  708.     }
  709.  
  710.     /**
  711.       * sets the date from an array with at least YEAR set
  712.       * if $dataArray is not an array or $dateArray['YEAR'] is not set
  713.       * the object is set to null and False is returned
  714.       *
  715.       * @param array $dataArray 
  716.       * @param boolean $debug 
  717.       *
  718.       * @returns boolean
  719.       *
  720.       * @version pk-06-08-08
  721.       *
  722.       ***/
  723.     function setFromArray($dateArray,$debug=False{
  724.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_DATE::setFromArray()");
  725.  
  726.         if (is_array($dateArray)) {
  727.             reset($dateArray);
  728.             // $res=array_change_key_case($dateArray, CASE_UPPER); PHP Version >= 4.2
  729.             while(list($key,$val)=each($dateArray)) $res[strtoupper($key)]=$val;
  730.  
  731.             if ((intval($res['YEAR'])==0|| (intval($res['MONTH'])==0|| (intval($res['DAY'])==0)) // <pk-06-09-06 > bugfix for empty dates
  732.                 $this->setNULL();
  733.                 return False;
  734.             }
  735.             if ($debugechoDebug(__FILE__,"<pre style=\"padding-left:30px;font-size:85%\">".print_r($res,True)."</pre>");
  736.             if (isset($res['YEAR'])) {
  737.                 $this->myYear =intval($res['YEAR']);
  738.                 if ($this->myYear < 100$this->myYear = $this->myYear + 2000;
  739.             else {
  740.                 $this->setNULL();
  741.                 return False;
  742.             }
  743.             $this->myMonth   =intval($res['MONTH']);
  744.             $this->myDay     =intval($res['DAY']);
  745.             $this->myHour    =intval($res['HOUR']);
  746.             $this->myMinute  =intval($res['MIN']);
  747.             $this->mySecond  =intval($res['SEC']);
  748.             return True;
  749.         else {
  750.             $this->setNULL();
  751.             return False;
  752.         }
  753.     }
  754.  
  755.    /**
  756.       * sets the date from a date string with yyyy-MM-dd [HH:mm:ss] format
  757.       *
  758.       * @param string $aString 
  759.       * @param boolean $debug since pk-05-03-30
  760.       *
  761.       ***/
  762.     function setDateStr($aString,$debug=False{     // yyyy-MM-dd [HH:mm:ss]
  763.         if ($debugecho "<p><b>OCSP_DATE::setDateStr($aString)</b> (".get_class($this).")</p><blockquote>";
  764.         if (empty($aString)) {
  765.             if ($debugecho "<p>empty setNULL()</p>";
  766.             $this->setNULL();
  767.         else if ($dT=explode(" ",$aString)) {
  768.             if ($date=explode("-",$dT[0])) {
  769.                 if ($debugecho "<p>date: ".$dT[0]."</p>";
  770.                 $this->myYear=intval($date[0]);
  771.                 if ($this->myYear < 100$this->myYear += 2000;
  772.                 if (!isset($date[1])) // <pk-07-02-08> E_ALL
  773.                     if ($debugecho "<p>setNULL</p>";
  774.                     $this->setNULL();
  775.                 else {
  776.                     $this->myMonth=intval($date[1]);
  777.                     $this->myDay=intval(isset($date[2]$date[2);
  778.                 }
  779.             }
  780.             if ((!empty($dT[1])) && ($time=explode(":",$dT[1]))) {
  781.                 if ($debugecho "<p>time: ".$dT[1]."</p>";
  782.                 $this->myHour=intval($time[0]);
  783.                 $this->myMinute =intval($time[1]);
  784.                 $this->mySecond =intval($time[2]);
  785.             }
  786.         else {
  787.             if ($debugecho "<p>setNULL</p>";
  788.             $this->setNULL();
  789.         }
  790.         if ($debugecho "<p>Date set to: ".$this->dateStr()."</p></blockquote>";
  791.     }
  792.  
  793.     function setTimeStr($aString{     // HH:mm:ss
  794.         if (empty($aString)) $this->setTimeStr("00:00:00");
  795.         if ($timeArr=explode(":",$aString)) {
  796.             $this->myHour=intval($timeArr[0]);
  797.             $this->myMinute =intval($timeArr[1]);
  798.             $this->mySecond =intval($timeArr[2]);
  799.         else {
  800.             $this->setTimeStr("00:00:00");
  801.         }
  802.     }
  803.  
  804.     /**
  805.       * checks if the current date is in past
  806.       *
  807.       * @return boolean 
  808.       * 
  809.       * @version pk-07-11-19 $this->myYear > 2037 save
  810.       * 
  811.       ***/
  812.     function isInPast({
  813.         $aTime=time();
  814.         
  815.         if ($this->myYear < intval(date("Y",$aTime)))       return True;
  816.         else if ($this->myYear > intval(date("Y",$aTime)))  return False;
  817.         
  818.         if ($this->myMonth < intval(date("m",$aTime)))      return True;
  819.         else if ($this->myMonth > intval(date("m",$aTime))) return False;
  820.         
  821.         if ($this->myDay < intval(date("d",$aTime)))      return True;
  822.         else if ($this->myDay > intval(date("d",$aTime))) return False;
  823.         
  824.         if ($this->myHour < intval(date("H",$aTime)))      return True;
  825.         else if ($this->myHour > intval(date("H",$aTime))) return False;
  826.         
  827.         if ($this->myMinute < intval(date("i",$aTime)))      return True;
  828.         else if ($this->myMinute > intval(date("i",$aTime))) return False;
  829.  
  830.         if ($this->mySecond < intval(date("s",$aTime)))      return True;
  831.         return False;
  832.     }
  833.  
  834.     /**
  835.       * checks if the current date is in futur
  836.       *
  837.       * @return boolean 
  838.       *
  839.       * @version pk-07-11-19 $this->myYear > 2037 save
  840.       ***/
  841.     function isInFutur({
  842.         $aTime=time();
  843.         
  844.         if ($this->myYear > intval(date("Y",$aTime)))       return True;
  845.         else if ($this->myYear < intval(date("Y",$aTime)))  return False;
  846.         
  847.         if ($this->myMonth > intval(date("m",$aTime)))      return True;
  848.         else if ($this->myMonth < intval(date("m",$aTime))) return False;
  849.         
  850.         if ($this->myDay > intval(date("d",$aTime)))      return True;
  851.         else if ($this->myDay < intval(date("d",$aTime))) return False;
  852.         
  853.         if ($this->myHour > intval(date("H",$aTime)))      return True;
  854.         else if ($this->myHour < intval(date("H",$aTime))) return False;
  855.         
  856.         if ($this->myMinute > intval(date("i",$aTime)))      return True;
  857.         else if ($this->myMinute < intval(date("i",$aTime))) return False;
  858.  
  859.         if ($this->mySecond > intval(date("s",$aTime)))      return True;
  860.         return False;
  861.     }
  862.  
  863.  
  864.     /**
  865.       * calculates the difference between two dates
  866.       * returns the rounded granularity between $this and a Date
  867.       *
  868.       * granularity values are:
  869.       *
  870.       * - s seconds
  871.       * - i minutes
  872.       * - h hours
  873.       * - d days
  874.       * - m month
  875.       * - y year
  876.       *
  877.       * @param pcf_DATE $aDate 
  878.       * @param char $granularity 
  879.       * @param char $round 
  880.       * @param boolean $debug 
  881.       *
  882.       * @returns int
  883.       *
  884.       * @version pk-06-07-17 granularity added
  885.       *
  886.       * @todo check if date is > 2037
  887.       ***/
  888.     function diff($aDate,$granularity='s',$round=0,$debug=False{
  889.         if ($debugechoDebugMethod(__FILE__,get_class($this),"OCSP_DATE::diff()","\$granularity=$granularity\n\$round=$round\n\$aDate:<pre>".print_r($aDate,True)."</pre>\$this:<pre>".print_r($this,True)."</pre>");
  890.  
  891.         if (!is_object($aDate)) return False;
  892.         $d_secons=$aDate->getLinuxTime($this->getLinuxTime();
  893.         switch($granularity{
  894.             case "i"return round(($d_secons/60),$round);
  895.             case "h"return round((($d_secons/60)/60),$round);
  896.             case "d"return round(((($d_secons/60)/60)/24),$round);
  897.             case "m":
  898.                         $f_month=($aDate->getYear($this->myYear)*12.0;
  899.                         $f_month=$f_month+($aDate->getMonth($this->myMonth);
  900.                         $f_month=$f_month+(($aDate->getDay($this->myDay)/30);
  901.                         return round($f_month,$round);
  902.             case "y"return round(($this->diff($aDate,'m')/12),$round);
  903.             defaultreturn $d_secons// seconds
  904.         }
  905.     }
  906.  
  907.     /**
  908.       * add $sec seconds to the date
  909.       *
  910.       * @param int $sec 
  911.       *
  912.       * @todo check if date is > 2037
  913.       *
  914.       ***/
  915.     function add($sec{
  916.         $this->setLinuxTime($this->getLinuxTime()+intval($sec));
  917.     }
  918.  
  919.     /**
  920.       * add $days days to the date
  921.       *
  922.       * @param int $sec 
  923.       *
  924.       * @param since pk-05-04-07
  925.       *
  926.       * @todo check if date is > 2037
  927.       *
  928.       ***/
  929.     function addDays($days{
  930.         $this->setLinuxTime($this->getLinuxTime()+(intval($days)*24*60*60));
  931.     }
  932.  
  933.  
  934.     /**
  935.       * subtract $sec seconds from the date
  936.       *
  937.       * @param int $sec 
  938.       *
  939.       * @since pk-05-04-07
  940.       *
  941.       * @todo check if date is > 2037
  942.       *
  943.       ***/
  944.     function subtract($sec{
  945.         $this->setLinuxTime($this->getLinuxTime()-intval($sec));
  946.     }
  947.  
  948.     /**
  949.       * subtract $days from the date
  950.       *
  951.       * @param int $sec 
  952.       *
  953.       * @since pk-05-04-07
  954.       *
  955.       * @todo check if date is > 2037
  956.       *
  957.       ***/
  958.     function subtractDays($days{
  959.         $this->setLinuxTime($this->getLinuxTime()-intval($days)*24*60*60);
  960.     }
  961.  
  962.     /**
  963.       * @todo check if date is > 2037
  964.       *
  965.       */
  966.     function equal($aDate{
  967.         if (is_object($aDate)) {
  968.             return (($this->getLinuxTime()==$aDate->getLinuxTimeStamp()) True False);
  969.         else {
  970.             return False;
  971.         }
  972.     }
  973.  
  974.     /**
  975.      * returns if to date objectes have the same date;
  976.      * 
  977.      * @param OCSP_DATE $aDate 
  978.      * 
  979.      * @return boolean 
  980.      */
  981.     function equalDate($aDate{
  982.         if (!(is_object($aDate)))           return False;
  983.         if ($this->myYear != $aDate->year)    return False;
  984.         if ($this->myMonth != $aDate->month)  return False;
  985.         if ($this->myDay  != $aDate->day)    return False;
  986.         return True;
  987.     }
  988.     
  989.     
  990.     /*** ical methods */
  991.     
  992.     function iCalDecoder($file{
  993.         $icalarray=array();
  994.         if (file_exists($file))
  995.         {
  996.             $ical file_get_contents($file);
  997.             preg_match_all('/(BEGIN:VEVENT.*?END:VEVENT)/si'$ical$resultPREG_PATTERN_ORDER);
  998.             for ($i 0$i count($result[0])$i++
  999.             {
  1000.                 $tmpbyline explode("\r\n"$result[0][$i]);
  1001.                 
  1002.                 foreach ($tmpbyline as $item{                                       
  1003.                     $tmpholderarray explode(":",$item);
  1004.                     
  1005.                     switch($tmpholderarray[0])
  1006.                     {
  1007.                         case "UID":
  1008.                             $majorarray['UID'$tmpholderarray[1];
  1009.                             break;
  1010.                         case "RRULE":
  1011.                             $a_tmp=explode(";",$tmpholderarray[1]);
  1012.                             for ($j=0;$j<sizeof($a_tmp);$j++)
  1013.                             {
  1014.                                 list($s_key,$s_val)=explode("=",$a_tmp[$j]);
  1015.                                 $majorarray['RRULE'][$s_key]=$s_val;
  1016.                             }                        
  1017.                             
  1018.                             break;
  1019.                         default:
  1020.                             $a_tmp=explode(";",$tmpholderarray[0]);
  1021.         
  1022.                             for ($j=1;$j<sizeof($a_tmp);$j++)
  1023.                             {
  1024.                                 list($s_key,$s_val)=explode("=",$a_tmp[$j]);
  1025.                                 $majorarray[$a_tmp[0]]['ATTR'][$s_key]=$s_val;
  1026.                             }                        
  1027.                             if (count($tmpholderarray>1{
  1028.                                 $majorarray[$a_tmp[0]]['VALUE'$tmpholderarray[1];
  1029.                             }
  1030.                     }                    
  1031.                 }
  1032.                 /*
  1033.                     lets just finish what we started..
  1034.                 */
  1035.                 if (preg_match('/DESCRIPTION:(.*)END:VEVENT/si'$result[0][$i]$regs)) {
  1036.                     $majorarray['DESCRIPTION'str_replace("  "" "str_replace("\r\n"""$regs[1]));
  1037.                 }
  1038.                 $icalarray[$majorarray;
  1039.                 unset($majorarray);                        
  1040.             }
  1041.         }
  1042.         return $icalarray;
  1043.     
  1044.  
  1045.  
  1046.  
  1047. }
  1048. ?>

Documentation generated on Thu, 08 Jan 2009 17:45:26 +0100 by phpDocumentor 1.4.0a2