/usr/local/miolo2/classes/utils/mkrono.class

Go to the documentation of this file.
00001 <?php
00002 /*
00003 +--+ Project Name: KronoClass
00004 +--+ Version: 0.7
00005 +--+ Project Author: Tommaso D'Argenio
00006 +--+ Author Email: rajasi@ziobudda.net, info@holosoft.it
00007 +--+ Build Date:  January 10 2003 16.18 (CET)
00008 +--+ Update: September 9 2003 16.35 (CET)
00009 
00010 +--+ DISCLAIMER
00011 Copyright (c) 2002-03 Tommaso D'Argenio <rajasi@ziobudda.net>
00012 This program is free software; you can redistribute it and/or
00013 modify it under the terms of the GNU Lesser General Public License
00014 as published by the Free Software Foundation; either version
00015 2.1 of the License, or (at your option) any later version.
00016 
00017 This program is distributed in the hope that it will be useful,
00018 but WITHOUT ANY WARRANTY; without even the implied
00019 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00020 PURPOSE.  See the GNU Lesser General Public License for more details.
00021 
00022 You should have received a copy of the GNU Lesser General Public
00023 License along with this program; if not, write to the
00024 Free Software Foundation, Inc.,
00025 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
00026 http://www.fsf.org
00027 
00028 +--+ NOTES FROM AUTHOR
00029 * Please, if you make any change in the code let me know by email!!!
00030    if use this class in your project, please let me know, in this way i can publish it.
00031     
00032    If you want to personalize the language settings, please take a look in all ini files.
00033 
00034 +--+ Requirements:
00035                 PHP 4.0+
00036 */
00037 
00048 class MKrono
00049 {
00053         var $dayNameExt;
00054         
00058         var $dayNameCon;
00059         
00063         var $monthNameExt;
00064         
00068         var $monthNameCon;
00069 
00073         var $dataFrom;
00074 
00078         var $dataTo;
00079         
00083         var $error;
00084         
00088         var $abbr;
00089         
00093         var $lan; 
00094         
00098         var $version='0.7';
00099         
00108         var $dateFormat;
00109         
00117         var $timeFormat;
00118 
00122         var $separator;
00123         
00127         var $path;
00128         
00137         function __construct($lan='it', $dateFormat='it', $timeFormat='it',$separator='/')
00138         {
00139         global $MIOLO;
00140         
00141         $this->path = $MIOLO->GetConf('home.etc') . '/mkrono.conf';    
00142         $xml = new SimpleXML($this->path);
00143                 $this->lan = (string)$xml->xml->language;
00144                 $this->dateFormat = (string)$xml->xml->dateFormat;
00145                 $this->separator = (string)$xml->xml->separator;
00146         $this->timeFormat = (string)$xml->xml->timeFormat;
00147         $this->error='';
00148 
00149                 $this->_checkIniFile();
00150 
00151         $array = $xml->ToArray($conf, $xml->xml->longDay);
00152         foreach($array as $k=>$v) $this->dayNameExt[$k] = $v['day'];
00153                 $array = $xml->ToArray($conf, $xml->xml->shortDay);
00154         foreach($array as $k=>$v) $this->dayNameCon[$k] = $v['day'];
00155                 $array = $xml->ToArray($conf, $xml->xml->longMonth);
00156         foreach($array as $k=>$v) $this->monthNameExt[$k] = $v['month'];
00157                 $array = $xml->ToArray($conf, $xml->xml->shortMonth);
00158         foreach($array as $k=>$v) $this->monthNameCon[$k] = $v['month'];
00159 
00160 /*        
00161         $this->dayNameExt=parse_ini_file($this->path.'_long_day.ini', TRUE);
00162                         
00163                 $this->dayNameCon=parse_ini_file($this->path.'_short_day.ini', TRUE);
00164 
00165                 $this->monthNameExt=parse_ini_file($this->path.'_long_month.ini', TRUE);
00166 
00167                 $this->monthNameCon=parse_ini_file($this->path.'_short_month.ini', TRUE);
00168 */
00169         }
00170         
00175     function _checkIniFile()
00176     {
00177 /*
00178         if(!file_exists($this->path.'_long_day.ini'))
00179             $this->error.='Long Days ini languages does\'nt exists!<br>';
00180         if(!file_exists($this->path.'_short_day.ini'))
00181             $this->error.='Short Days ini languages does\'nt exists!<br>';
00182         if(!file_exists($this->path.'_long_month.ini'))
00183             $this->error.='Long Months ini languages does\'nt exists!<br>';
00184         if(!file_exists($this->path.'_short_month.ini'))
00185             $this->error.='Short Months ini languages does\'nt exists!<br>';
00186 */        
00187         $this->exitOnError();    
00188     }
00189     
00194         function exitOnError()
00195         {
00196                 if($this->error!='')
00197         {
00198                         echo ' [Fatal Error] <b>'.$this->error.'</b> ';
00199                 exit;
00200         }
00201         }
00202         
00207         function _update()
00208         {
00209                 $s=stat(__FILE__);
00210                 return $this->kDate('%l %d %F %Y',$s[9]);
00211         }
00212         
00217         function _format()
00218         {
00219                 switch($this->dateFormat)
00220                 {
00221                         case 'ansi': 
00222                                 if(!$this->abbr) 
00223                                         return 'Y'.$this->separator.'m'.$this->separator.'d'; 
00224                                 else 
00225                                         return 'Y'.$this->separator.'n'.$this->separator.'j'; 
00226                         break;
00227                         case 'it': 
00228             case 'br':
00229                                 if(!$this->abbr)
00230                                         return 'd'.$this->separator.'m'.$this->separator.'Y';
00231                                 else
00232                                         return 'j'.$this->separator.'n'.$this->separator.'Y';
00233                         break; 
00234                         case 'en': 
00235                         case 'std': 
00236                                 if(!$this->abbr)
00237                                         return 'm'.$this->separator.'d'.$this->separator.'Y';
00238                                 else 
00239                                         return 'n'.$this->separator.'j'.$this->separator.'Y';
00240                         break;          
00241                         default: $this->error='Date Format not recognized, must be "ansi", "it", "std", "br" or "en" !! '; $this->exitOnError();                        
00242                 }
00243         }
00244         
00250         function _language()
00251         {
00252                 switch($this->lan)
00253                 {
00254                         case 'it': return 'Italian'; break;
00255                         case 'en': return 'English'; break;
00256                         case 'de': return 'Deutch'; break;
00257                         case 'fr': return 'French'; break;
00258                         case 'es': return 'Spanish'; break;
00259                         case 'id': return 'Indonesian'; break;
00260                         case 'no': return 'Norway'; break;
00261                         case 'jp': return 'Japanese'; break;
00262                         case 'fi': return 'Finnish'; break;
00263                         case 'nl': return 'Dutch'; break;
00264                         case 'br': return 'Brazilian Portuguese'; break;
00265                         default: return 'Language not recognized!!'; 
00266                 }
00267         }
00268         
00273         function _debug()
00274         {
00275                 $debug ='
00276                                         Format of Date: <i>'.$this->_format().'</i><br>
00277                                         Date Separator: <i>'.$this->separator.'</i><br>
00278                                         Language: <i>'.$this->_language().'</i>
00279                                         ';
00280                 return $debug;
00281         }
00286         function _disclaimer()
00287         {
00288                 echo '<span style="font-family:helvetica,verdana,serif;font-size:14px;color:#ff9900;">';
00289                 echo '<b>KronoClass</b> v. '.$this->version.' <br>';
00290                 echo '<i>Copyright (c) 2002-2003 by Tommaso D\'Argenio &lt;<a href="mailto:rajasi@ziobudda.net" title="Send me an email">rajasi@ziobudda.net</a>&gt;<br>';
00291                 echo 'Last modified on: '.$this->_update().'</i><br><hr size="1px" width="50%" color="black" align="left"></span><br>';
00292         }
00293         
00298         function _isDaylight($time)
00299         {
00300                 list($dom, $dow, $month, $hour, $min) = explode(":", date("d:w:m:H:i", $time)); 
00301                 
00302                 if   ($month > 4 && $month < 10) 
00303                 { 
00304                   $retval = 1;        # May thru September 
00305                 } 
00306                 elseif ($month == 4 && $dom > 7) 
00307                 { 
00308                   $retval = 1;        # After first week in April 
00309                 } 
00310                 elseif ($month == 4 && $dom <= 7 && $dow == 0 && $hour >= 2) 
00311                 { 
00312                   $retval = 1;        # After 2am on first Sunday ($dow=0) in April 
00313                 } 
00314                 elseif ($month == 4 && $dom <= 7 && $dow != 0 && ($dom-$dow > 0)) 
00315                 { 
00316                   $retval = 1;        # After Sunday of first week in April 
00317                 } 
00318                 elseif ($month == 10 && $dom < 25) 
00319                 { 
00320                   $retval = 1;        # Before last week of October 
00321                 } 
00322                 elseif ($month == 10 && $dom >= 25 && $dow == 0 && $hour < 2) 
00323                 { 
00324                   $retval = 1;        # Before 2am on last Sunday in October 
00325                 } 
00326                 elseif ($month == 10 && $dom >= 25 && $dow != 0 && ($dom-24-$dow < 1) ) 
00327                 { 
00328                   $retval = 1;        # Before Sunday of last week in October 
00329                 } 
00330                 else 
00331                 { 
00332                   $retval = 0; 
00333                 } 
00334                 
00335                 return($retval);        
00336         }
00337         
00338         
00345         function dayToN($day)
00346         {
00347                 if($day=='' || strlen($day)<3)
00348                 {
00349                         $this->error='Day name not valid!';
00350                         $this->exitOnError();                   
00351                 }
00352                 
00353                 $day=ucwords($day);             
00354                 if(strlen($day)==3)
00355                         $ar=$this->dayNameCon[$this->lan];
00356                 else
00357                         $ar=$this->dayNameExt[$this->lan];
00358                         
00359                 if(in_array($day,$ar))
00360                 {
00361                         $k=array_keys($ar,$day);
00362                         return($k[0]);
00363                 }
00364         }
00365         
00372         function NToDay($day)
00373         {
00374                 if($day>6 || $day<0)
00375                 {
00376                         $this->error='Day range not valid. Must be 0 to 6!';
00377                         $this->exitOnError();
00378                 }
00379                 
00380                 if($this->abbr===true)
00381                         return($this->dayNameCon[$this->lan][$day]);
00382                 elseif($this->abbr!=true)
00383                         return($this->dayNameExt[$this->lan][$day]);
00384         }
00385         
00392         function monthToN($month)
00393         {
00394                 if($month=='' || strlen($month)<3)
00395                 {
00396                         $this->error='Month name not valid!';
00397                         $this->exitOnError();                   
00398                 }
00399                 
00400                 $month=ucwords($month);         
00401                 if(strlen($month)==3)
00402                         $ar=$this->monthNameCon[$this->lan];
00403                 else
00404                         $ar=$this->monthNameExt[$this->lan];
00405                         
00406                 if(in_array($month,$ar))
00407                 {
00408                         $k=array_search($month,$ar);
00409                         return($k+1);
00410                 }
00411                 else
00412                         return -1;
00413         }
00414         
00421         function NToMonth($month)
00422         {
00423                 if($month>12 || $month<1)
00424                 {
00425                         $this->error='Month range not valid. Must be 1 to 12!';
00426                         $this->exitOnError();
00427                 }
00428 
00429                 if($this->abbr===true)
00430                         return($this->monthNameCon[$this->lan][$month-1]);
00431                 elseif($this->abbr!=true)
00432                         return($this->monthNameExt[$this->lan][$month-1]);
00433         }
00434 
00435 
00444         function isDay($data,$day)
00445         {
00446                 $data=str_replace('-','/',$data);
00447                 $data=str_replace('.','/',$data);
00448                 $exp=explode('/',$data);
00449                 
00450                 $data_unix=$this->kMkTime($exp);
00451                 $giorno_unix=date('w',$data_unix);
00452         
00453                 if(!is_numeric($day))
00454                 {
00455                         $day=$this->dayToN($day);               
00456                 }
00457         
00458                 if($giorno_unix==$day)
00459                         return 1;
00460                 else
00461                         return $this->NToDay($giorno_unix);     
00462         }
00463 
00471         function dateToTimestamp($date)
00472         {
00473                 if(strstr($date,' '))
00474         {
00475             $t = explode(' ',$date);
00476             $date = $t[0];
00477             $time = explode(':',$t[1]);
00478         }
00479         else
00480             $time = '';
00481         $date=str_replace('-','/',$date);
00482                 $date=str_replace('.','/',$date);
00483                 $exp=explode('/',$date);
00484         if($time!='')
00485         {
00486             $exp[3]=(isset($time[0])) ? $time[0] : 0;
00487             $exp[4]=(isset($time[1])) ? $time[1] : 0;
00488             $exp[5]=(isset($time[2])) ? $time[2] : 0;
00489         }
00490                 return $this->kMkTime($exp);
00491         }
00492         
00500         function daysDiff($data_ini,$data_fin)
00501         {
00502                 $data_ini=str_replace('-','/',$data_ini);
00503                 $data_ini=str_replace('.','/',$data_ini);
00504                 $data_fin=str_replace('-','/',$data_fin);
00505                 $data_fin=str_replace('.','/',$data_fin);
00506                 
00507                 $exp_ini=explode('/',$data_ini);
00508                 $exp_fin=explode('/',$data_fin);
00509                 
00510                 $ini=date('z',$this->kMkTime($exp_ini));
00511                 $fin=date('z',$this->kMkTime($exp_fin));
00512 
00513                 $days = floor(($this->kMkTime($exp_fin)-$this->kMkTime($exp_ini))/(60*60*24));          
00514                 return $days;
00515         }
00516 
00531         function timesDiff($time_from,$time_to,$result_in="m")
00532         {
00533                 if( (strstr($time_from,'.') || strstr($time_from,':')) && (strstr($time_to,'.') || strstr($time_to,':')) )
00534                 {
00535                         $time_from=str_replace(':','.',$time_from);
00536                         $time_to=str_replace(':','.',$time_to);
00537                         
00538                         $t1=explode('.',$time_from);
00539                         $t2=explode('.',$time_to);
00540                         
00541                         $h1=$t1[0];
00542                         $m1=$t1[1];
00543                         
00544                         $h2=$t2[0];
00545                         $m2=$t2[1];
00546                         
00547                         if($h1<=24 && $h2<=24 && $h1>=0 && $h2>=0 && $m1<=59 && $m2<=59 && $m1>=0 && $m2>=0)
00548                         {
00549                                 $diff=($h2*3600+$m2*60)-($h1*3600+$m1*60);
00550                                 if($result_in=="s")
00551                                         return $diff;
00552                                 elseif($result_in=="m")
00553                                 {
00554                                         return $diff/60;
00555                                 }
00556                                 elseif($result_in=="h")
00557                                 {
00558                                         $r=$diff/3600;
00559                                         $t=explode('.',$r);
00560                                         $h=$t[0];
00561                                         if($h>24)
00562                                                 $h-=24;                                 
00563                                         $m=round("0.$t[1]"*60);
00564                                         return $h.'h'.$m.'m';
00565                                 }                       
00566                         }
00567                         else
00568                         {
00569                                 $this->error='Time range not valid. Must be 0 to 24 for hours and 0 to 59 for minutes!';
00570                                 $this->exitOnError();
00571                         }                                                       
00572                 }
00573                 else
00574                 {
00575                         $this->error='Time format not valid. Must be in format HH:mm or HH.mm';
00576                         $this->exitOnError();           
00577                 }       
00578         }
00579         
00593         function timesAdd($time,$add,$what)
00594         {
00595                 if( (strstr($time,'.') || strstr($time,':')))
00596                 {
00597                         $time=str_replace(':','.',$time);                       
00598                         $t1=explode('.',$time);                 
00599                         $h1=$t1[0];
00600                         $m1=$t1[1];
00601                         if($h1<=24 && $h1>=0  && $m1<=59 && $m1>=0)
00602                         {
00603                                 if($what=="m")
00604                                 {
00605                                         $res=($h1*60)+$m1+$add;
00606                                         $r=$res/60;
00607                                         $t=explode('.',$r);
00608                                         $h=$t[0];
00609                                         if($h>24)
00610                                                 $h-=24;                                 
00611                                         $m=round("0.$t[1]"*60);
00612                                         return $h.':'.$m;
00613                                 }
00614                                 elseif($what=="h")
00615                                 {
00616                                         $res=($h1*60)+$m1+($add*60);
00617                                         $r=$res/60;
00618                                         $t=explode('.',$r);
00619                                         $h=$t[0];
00620                                         if($h>24)
00621                                                 $h-=24;                                 
00622                                         $m=round("0.$t[1]"*60);
00623                                         return $h.':'.$m;                               
00624                                 }
00625                                 elseif($what=="t")
00626                                 {
00627                                         if( (strstr($add,'.') || strstr($add,':')))
00628                                         {
00629                                                 $add=str_replace(':','.',$add);                 
00630                                                 $t1=explode('.',$add);                  
00631                                                 $h2=$t1[0];
00632                                                 $m2=$t1[1];
00633                                                 if($h2<=24 && $h2>=0  && $m2<=59 && $m2>=0)
00634                                                 {
00635                                                         $res=($h1*60)+($h2*60)+$m1+$m2;
00636                                                         $r=$res/60;
00637                                                         $t=explode('.',$r);
00638                                                         $h=$t[0];
00639                                                         if($h>24)
00640                                                                 $h-=24;
00641                                                         $m=round("0.$t[1]"*60);
00642                                                         return $h.':'.$m;                                                                                       
00643                                                 }                                               
00644                                         }                                       
00645                                         else
00646                                         {
00647                                                 $this->error='Time format not valid. Must be in format HH:mm or HH.mm';
00648                                                 $this->exitOnError();           
00649                                         }       
00650                                 }
00651                         }
00652                         else
00653                         {
00654                                 $this->error='Time range not valid. Must be 0 to 24 for hours and 0 to 59 for minutes!';
00655                                 $this->exitOnError();
00656                         }                                                       
00657                 }
00658                 else
00659                 {
00660                         $this->error='Time format not valid. Must be in format HH:mm or HH.mm';
00661                         $this->exitOnError();           
00662                 }       
00663         }
00664         
00670         function howTo($data)
00671         {
00672                 $data=str_replace('-','/',$data);
00673                 $data=str_replace('.','/',$data);
00674                 $exp=explode('/',$data);
00675                 $data_unix=$this->kMkTime($exp);
00676         $diff = $data_unix-time();
00677                 if($data_unix>time())
00678                         return (date("z",$diff));
00679                 else
00680                 {
00681                         $this->error='Cannot perform calculation on past time!';
00682                         $this->exitOnError();
00683                 }
00684         }
00685 
00694         function howDays($data_ini,$data_fin,$day)
00695         {
00696                 $data_ini=str_replace('-','/',$data_ini);
00697                 $data_ini=str_replace('.','/',$data_ini);       
00698         
00699                 $eta_data=$this->daysDiff($data_ini,$data_fin);
00700 
00701                 $giorno=(int)$this->atomDate($data_ini,'d');
00702                 $mese=(int)$this->atomDate($data_ini,'m');
00703                 $anno=(int)$this->atomDate($data_ini,'Y');
00704                 
00705                 $count=0;
00706 
00707                 for($i=0;$i<$eta_data;$i++)
00708                 {               
00709                         $data=date($this->_format(),mktime(0,0,0,$mese,$giorno+$i,$anno));
00710                         if($this->isDay($data,$day)===1)
00711                                 $count++;
00712                 }               
00713                 return $count;
00714         }
00715         
00721     function kMkTime($exp)
00722     {
00723         (isset($exp[3])) ? $h=$exp[3] : $h=0;
00724         (isset($exp[4])) ? $m=$exp[4] : $m=0;
00725         (isset($exp[5])) ? $s=$exp[5] : $s=0;        
00726         switch($this->dateFormat) 
00727                 {
00728             case 'ansi': return $this->makeTime($h,$m,$s,$exp[1],$exp[2],$exp[0]); break; // using YYYY-MM-DD                           
00729                         case 'it': return $this->makeTime($h,$m,$s,$exp[1],$exp[0],$exp[2]); break;// using DD-MM-YYYY
00730                         case 'br': return $this->makeTime($h,$m,$s,$exp[1],$exp[0],$exp[2]); break;// using DD-MM-YYYY
00731                         case 'std': return $this->makeTime($h,$m,$s,$exp[0],$exp[1],$exp[2]); break; // using MM-DD-YYYY                                                                                
00732                         case 'en': return $this->makeTime($h,$m,$s,$exp[0],$exp[1],$exp[2]); break; // using MM-DD-YYYY                                                                                                     
00733                         default: $this->error='Date Format not recognized, must be "ansi", "it", "std", "br" or "en" !! '; $this->exitOnError();
00734             }
00735     }
00736         
00752         function atomDate($date,$atom)
00753         {
00754                 if(strlen($date)<=10)
00755                 {
00756                         $date.=' 00:00:00';
00757                 }
00758 
00759                 $t=explode(' ',$date);
00760                 $exp1=explode('/',$t[0]);
00761                 $exp2=explode(':',$t[1]);
00762                 $exp=array_merge($exp1,$exp2);
00763                 // Extract only time
00764                 switch($atom)
00765                 {
00766                         case 'h': return $exp[3]; break;
00767                         case 'i': return $exp[4]; break;
00768                         case 's': return $exp[5]; break;
00769                 }
00770                 // Extract day,month and year
00771                 switch($this->dateFormat)
00772                 {
00773                         case 'ansi':
00774                         {
00775                                 switch($atom)
00776                                 {
00777                                         case 'd': return $exp[2]; break;
00778                                         case 'm': return $exp[1]; break;
00779                                         case 'y': return substr($exp[0],2,2); break;
00780                                         case 'Y': return $exp[0]; break;
00781                                         default: $this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'; $this->exitOnError();
00782                                 }
00783                                 break;
00784                         }                       
00785                         case 'it':
00786                         case 'br':
00787                         {
00788                                 switch($atom)
00789                                 {
00790                                         case 'd': return $exp[0]; break;
00791                                         case 'm': return $exp[1]; break;
00792                                         case 'y': return substr($exp[2],2,2); break;
00793                                         case 'Y': return $exp[2]; break;
00794                                         default: $this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'; $this->exitOnError();
00795                                 }
00796                                 break;
00797                         }                       
00798                         case 'en':
00799                         case 'std':
00800                         {
00801                                 switch($atom)
00802                                 {
00803                                         case 'd': return $exp[1]; break;
00804                                         case 'm': return $exp[0]; break;
00805                                         case 'y': return substr($exp[2],2,2); break;
00806                                         case 'Y': return $exp[2]; break;
00807                                         default: $this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'; $this->exitOnError();
00808                                 }
00809                                 break;
00810                         }                       
00811                         default: $this->error='Date Format not recognized, must be "ansi", "it", "std","br" or "en" !! '; $this->exitOnError();                 
00812                 }
00813         }
00814         
00815 
00833         function kDate($format="%l %d %F %Y",$timestamp=0)
00834         {       
00835                 if($timestamp==0)
00836                         $timestamp=time();
00837                         
00838 
00839                 if(!preg_match('/\%l|\%F|\%D|\%M/',$format))
00840                 {
00841                         return date(str_replace('%','',$format),$timestamp);
00842                 }
00843                 else
00844                 {
00845                         $out=$format;
00846                         if(strstr($format,'%l'))
00847                         {
00848                                 $this->abbr=false;
00849                                 $out=str_replace('%l',$this->NToDay(date('w',$timestamp)),$out);
00850                         }
00851                         if(strstr($format,'%F'))
00852                         {
00853                                 $this->abbr=false;
00854                                 $out=str_replace('%F',$this->NToMonth(date('m',$timestamp)),$out);
00855                         }
00856                         if(strstr($format,'%D'))
00857                         {
00858                                 $this->abbr=true;
00859                                 $out=str_replace('%D',$this->NToDay(date('w',$timestamp)),$out);
00860                         }
00861                         if(strstr($format,'%M'))
00862                         {
00863                                 $this->abbr=true;
00864                                 $out=str_replace('%M',$this->NToMonth(date('m',$timestamp)),$out);
00865                         }                       
00866                         if(strstr($format,'%Y'))
00867                                 $out=str_replace('%Y',date('Y',$timestamp),$out);
00868                         if(strstr($format,'%y'))
00869                                 $out=str_replace('%y',date('y',$timestamp),$out);
00870                         if(strstr($format,'%d'))
00871                                 $out=str_replace('%d',date('d',$timestamp),$out);
00872                         if(strstr($format,'%m'))
00873                                 $out=str_replace('%m',date('m',$timestamp),$out);
00874                         if(strstr($format,'%n'))
00875                                 $out=str_replace('%n',date('n',$timestamp),$out);
00876                         if(strstr($format,'%j'))
00877                                 $out=str_replace('%j',date('j',$timestamp),$out);
00878 
00879                         return $out;
00880                 }
00881         }
00882         
00883         /* Perform operation like sum or subtraction on date 
00884         *  @access public
00885         *  @return date The date transformed by calc
00886         *  @param  string $operator Operator may be
00887         *  +  '+' -> for sum
00888         *  +  'sum' -> for sum
00889         *  +  'add' -> for sum
00890         *  +  '-' -> for subtraction
00891         *  +  'sub'-> for subtraction
00892         *  +  'sot'-> for subtraction
00893         *  @param  date $date The date to calc on
00894         *  @param  string $operand is a number plus '%D' for days, '%M' for months, '%Y' for years
00895         *  + Example:
00896         *       - Add 1 month to a date:
00897         *       - $obj->operation('+','10/01/2003','1%M');
00898         * 
00899         *       - Subtract 20 days from a date:
00900         *       - $obj->operation('-','10/01/2003','20%D');
00901         */      
00902         function operation($operator,$date,$operand)
00903         {
00904                 // Thanks to Tim Hodson tim@trundlie.fsnet.co.uk - Begin Change
00905                 if(is_array($date))
00906         {       //to take a date as an array rather than hardcoded string
00907                         $date2=$this->kMkTime($date);
00908                         $ts=$date2; 
00909                 }
00910                 elseif(is_string($date))
00911         { // to handle an original string
00912                         $ts=$this->dateToTimestamp($date);
00913                 }
00914                 elseif(is_integer($date))
00915         { // to handle a timestamp
00916                         $ts = $date;
00917                 }
00918                 // end change of Tim
00919                 
00920                 if(!strstr($operand,'%'))
00921                 {
00922                         $this->error='Bad operand type!!';
00923                         $this->exitOnError();
00924                 }
00925 
00926                 $t=explode('%',$operand);
00927                 $how=$t[0];
00928                 
00929                 switch($t[1])
00930                 {
00931                         case 'D':
00932                         {
00933                                 if($operator=='+' || $operator=='sum' || $operator=='add')
00934                                 {
00935                                         return date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts)+$how,date('Y',$ts)));
00936                                 }
00937                                 elseif($operator=='-' || $operator=='sub' || $operator=='sot')
00938                                 {
00939                                         return date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts)-$how,date('Y',$ts)));                             
00940                                 }
00941                                 else
00942                                 {
00943                                         $this->error='Operator not recognized!!';
00944                                         $this->exitOnError();
00945                                 }
00946                                 break;
00947                         }
00948                         case 'M':
00949                         {
00950                                 if($operator=='+' || $operator=='sum' || $operator=='add')
00951                                 {
00952                                         return date($this->_format(),mktime(0,0,0,date('m',$ts)+$how,date('d',$ts),date('Y',$ts)));
00953                                 }
00954                                 elseif($operator=='-' || $operator=='sub' || $operator=='sot')
00955                                 {
00956                                         return date($this->_format(),mktime(0,0,0,date('m',$ts)-$how,date('d',$ts),date('Y',$ts)));                             
00957                                 }
00958                                 else
00959                                 {
00960                                         $this->error='Operator not recognized!!';
00961                                         $this->exitOnError();
00962                                 }
00963                                 break;
00964                         }
00965                         case 'Y':
00966                         {
00967                                 if($operator=='+' || $operator=='sum' || $operator=='add')
00968                                 {
00969                                         return date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts),date('Y',$ts)+$how));
00970                                 }
00971                                 elseif($operator=='-' || $operator=='sub' || $operator=='sot')
00972                                 {
00973                                         return date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts),date('Y',$ts)-$how));                             
00974                                 }
00975                                 else
00976                                 {
00977                                         $this->error='Operator not recognized!!';
00978                                         $this->exitOnError();
00979                                 }
00980                                 break;
00981                         }
00982                         default: 
00983                         {
00984                                 $this->error='Bad operand type!!';
00985                                 $this->exitOnError();
00986                         }
00987                 }               
00988         }       
00989         
00996         function netTimestamp($server='time-a.nist.gov', $port=37) 
00997         {             
00998             if ($fp = fsockopen($server, $port, $errno, $errstr, 25)) 
00999                 { 
01000                 fputs($fp, "\n"); 
01001                 $timevalue = fread($fp, 49); 
01002                 fclose($fp); 
01003             } 
01004                 else
01005                 {
01006                         $this->error=$server.' Time Server unavailable or u\'re not connected on the net!!';
01007                         $this->exitOnError();
01008                 }
01009 
01010         $ts = (abs(hexdec('7fffffff') - hexdec(bin2hex($timevalue)) - hexdec('7fffffff')) - 2208988800); 
01011             return $ts; 
01012         } 
01013 
01018         function swatchTime() 
01019         { 
01020           $offset = 60; 
01021           $beat_division = 24 * 60 / 1000; 
01022           $current_date = getdate(time()); 
01023           $hour = $current_date["hours"]; 
01024           $minute = $current_date["minutes"]; 
01025           $seconds = $current_date["seconds"]; 
01026           $total_minutes = $minute + $offset + $hour * 60; 
01027           $beats = round ($total_minutes / $beat_division); 
01028       if ($beats >= 1000) 
01029       { 
01030             $beats = $beats % 1000; 
01031           } 
01032            return ("@".$beats); 
01033         }       
01034         
01043         function mysql_time_easy($timestamp,$mode='mysqlfrom') 
01044         { 
01045           $formated='';
01046           
01047                 if($mode=='mysqlfrom')
01048                 {
01049                         $hour = substr($timestamp,8,2);  
01050                     $minute = substr($timestamp,10,2);  
01051                     $second = substr($timestamp,12,2);  
01052                     $month = substr($timestamp,4,2);  
01053                     $day = substr($timestamp,6,2);  
01054                     $year = substr($timestamp,0,4);  
01055                     $mktime = mktime($hour, $minute, $second, $month, $day, $year);  
01056                         $format=$this->_format();
01057                         $f=explode($this->separator,$format);
01058                         $format='%'.$f[0].$this->separator.'%'.$f[1].$this->separator.'%'.$f[2].' %g:%i %a';
01059                         $formated = $this->kDate($format,$mktime); 
01060                 }
01061                 else
01062                 {
01063                         $data=str_replace('-','/',$timestamp);
01064                         $data=str_replace('.','/',$data);
01065                         $m=$this->atomDate($data,'m');
01066                         $d=$this->atomDate($data,'d');
01067                         $Y=$this->atomDate($data,'Y');
01068                         $h=$this->atomDate($data,'h');
01069                         $i=$this->atomDate($data,'i');
01070                         $s=$this->atomDate($data,'s');
01071             $formated=$Y.$m.$d.$h.$i.$s;
01072                 }               
01073             return $formated; 
01074         } 
01075         
01085         function getNthDay($number,$weekday,$month,$year=0) 
01086         { 
01087                 if($number>5)
01088                 {
01089                         $this->error='There isn\'t more than 5 '.$weekday.' in a month, usually!!';
01090                         $this->exitOnError();
01091                 }
01092 
01093             $date_counter=1;  
01094             $week_counter=0;  
01095                 
01096                 if($year==0)
01097                         $year=date('Y');
01098                 
01099                 if(strlen($weekday)>3)
01100                         $format_dow='%l';
01101                 else
01102                         $format_dow='%D';
01103                 
01104                 if(!is_numeric($month))
01105                 {
01106                         $month=$this->monthToN($month);
01107                 }
01108                 
01109             do
01110                 {
01111                 $itsit=mktime(0,0,0,$month,$date_counter,$year); 
01112                 $dow=$this->kDate($format_dow,$itsit); 
01113                 if($dow==$weekday)
01114                         {   
01115                     $week_counter++; 
01116                 } 
01117                 
01118                         if( ($week_counter==$number) && ($weekday==$dow) ) 
01119                         {
01120                     $week_counter=$number; 
01121                                 if($date_counter > 1) // Thanks to Maurizio Marini <maumar@datalogica.com>
01122                                         $date_counter--;
01123                 } 
01124                         else 
01125                         { 
01126                     $date_counter++; 
01127                 } 
01128             } 
01129                 while($week_counter<$number);
01130 
01131                 $itsit=mktime(0,0,0,$month,$date_counter+1,$year); 
01132                 $format=$this->_format();
01133                 $f=explode($this->separator,$format);
01134                 $format='%'.$f[0].$this->separator.'%'.$f[1].$this->separator.'%'.$f[2];                         
01135                 
01136                 if($this->kDate('n',$itsit)!=$month)
01137                 {
01138                         $this->error='Bad request, try again!!';
01139                         $this->exitOnError();
01140                 }
01141                 else
01142                 {
01143                     return $this->kDate($format,$itsit); 
01144                 }
01145         }     
01146         
01153         function romanDate($date)
01154         {
01155                 $unit=array(0=>"",1=>'I',2=>'II',3=>'III',4=>'IV',5=>'V',6=>'VI',7=>'VII',8=>'VIII',9=>'IX');
01156                 $tens=array(0=>"",10=>'X',20=>'XX',30=>'XXX',40=>'XL',50=>'L',60=>'LX',70=>'LXX',80=>'LXXX',90=>'XC');
01157                 $hund=array(0=>"",100=>'C',200=>'CC',300=>'CCC',400=>'CD',500=>'D',600=>'DC',700=>'DCC',800=>'DCCC',900=>'CM');
01158                 $thou=array(0=>"",1000=>'M',2000=>'MM',3000=>'MMM',4000=>'MMMM',5000=>'MMMMM'); 
01159 
01160                 if($date=='')
01161                 {
01162                         $date=date('d/m/Y',time());             
01163                         $old_state=$this->dateFormat;
01164                         $this->dateFormat='it';
01165                         $year=$this->atomDate($date,'Y');
01166                         $month=$this->atomDate($date,'m');
01167                         $day=$this->atomDate($date,'d');
01168                         $this->dateFormat=$old_state;
01169                 }
01170                 else
01171                 {
01172                         $year=$this->atomDate($date,'Y');
01173                         $month=$this->atomDate($date,'m');
01174                         $day=$this->atomDate($date,'d');
01175                 }
01176                 
01177                  $y_thou = substr($year, -4, 1) * 1000; 
01178                  $y_hund = substr($year, -3, 1) * 100; 
01179                  $y_tens = substr($year, -2, 1) * 10; 
01180                  $y_unit = substr($year, -1, 1); 
01181                  
01182                  $m_tens=substr($month,-2,1)*10;
01183                  $m_unit=substr($month,-1,1);
01184                  
01185                  $d_tens=substr($day,-2,1)*10;
01186                  $d_unit=substr($day,-1,1);
01187                  
01188                  return $tens[$d_tens].$unit[$d_unit].
01189                                         $tens[$m_tens].$unit[$m_unit].
01190                                         $thou[$y_thou].$hund[$y_hund].$tens[$y_tens].$unit[$y_unit];
01191         }
01192         
01198      function yearMoonPhases($Y) 
01199          { 
01200        //Converted from Basic by Roger W. Sinnot, Sky & Telescope, March 1985. 
01201        //Converted from javascript by Are Pedersen 2002 
01202        //Javascript found at http://www.stellafane.com/moon_phase/moon_phase.htm 
01203         
01204        $R1 = 3.14159265 / 180; 
01205        $U = false; 
01206        $s = ""; // Formatted Output String 
01207        $K0 = intval(($Y-1900)*12.3685); 
01208        $T = ($Y-1899.5) / 100; 
01209        $T2 = $T*$T; $T3 = $T*$T*$T; 
01210        $J0 = 2415020 + 29*$K0; 
01211        $F0 = 0.0001178*$T2 - 0.000000155*$T3; 
01212        $F0 += (0.75933 + 0.53058868*$K0); 
01213        $F0 -= (0.000837*$T + 0.000335*$T2); 
01214        //X In the Line Below, F is not yet initialized, and J is not used before it set in the FOR loop. 
01215        //X J += intval(F); F -= INT(F); 
01216        //X Ken Slater, 2002-Feb-19 on advice of Pete Moore of Houston, TX 
01217        $M0 = $K0*0.08084821133; 
01218        $M0 = 360*($M0 - intval($M0)) + 359.2242; 
01219        $M0 -= 0.0000333*$T2; 
01220        $M0 -= 0.00000347*$T3; 
01221        $M1 = $K0*0.07171366128; 
01222        $M1 = 360*($M1 - intval($M1)) + 306.0253; 
01223        $M1 += 0.0107306*$T2; 
01224        $M1 += 0.00001236*$T3; 
01225        $B1 = $K0*0.08519585128; 
01226        $B1 = 360*($B1 - intval($B1)) + 21.2964; 
01227        $B1 -= 0.0016528*$T2; 
01228        $B1 -= 0.00000239*$T3; 
01229        for ( $K9=0; $K9 <= 28; $K9=$K9+0.5 ) 
01230            { 
01231          $J = $J0 + 14*$K9; $F = $F0 + 0.765294*$K9; 
01232          $K = $K9/2; 
01233          $M5 = ($M0 + $K*29.10535608)*$R1; 
01234          $M6 = ($M1 + $K*385.81691806)*$R1; 
01235          $B6 = ($B1 + $K*390.67050646)*$R1; 
01236          $F -= 0.4068*sin($M6); 
01237          $F += (0.1734 - 0.000393*$T)*sin($M5); 
01238          $F += 0.0161*sin(2*$M6); 
01239          $F += 0.0104*sin(2*$B6); 
01240          $F -= 0.0074*sin($M5 - $M6); 
01241          $F -= 0.0051*sin($M5 + $M6); 
01242          $F += 0.0021*sin(2*$M5); 
01243          $F += 0.0010*sin(2*$B6-$M6); 
01244          $F += 0.5 / 1440; //Adds 1/2 minute for proper rounding to minutes per Sky & Tel article 
01245          $J += intval($F); $F -= intval($F); 
01246          //Convert from JD to Calendar Date 
01247          $julian=$J+round($F); 
01248          $s = jdtogregorian ($julian); 
01249          //half K 
01250          if (($K9-floor($K9))>0)
01251                  { 
01252              if (!$U)
01253                          { 
01254                  //New half 
01255                  $phases[$s]="New Half"; 
01256              }
01257                          else
01258                          { 
01259                  //Full half 
01260                  $phases[$s]="Full Half"; 
01261              } 
01262          }
01263                  else
01264                  { 
01265              //full K 
01266              if ( !$U )
01267                          { 
01268                  $phases[$s]="New Moon"; 
01269              }
01270                          else
01271                          { 
01272                  $phases[$s]="Full Moon"; 
01273              } 
01274              $U = !$U; 
01275          } 
01276        } // Next 
01277        return $phases; 
01278      } //End MoonPhase 
01279         
01280      /*
01281                 Public: Return the Moon phase for given date, if no date is present refer to present day
01282          */
01283          function todayMoonPhase($date='')
01284          { 
01285                 if(!isset($date) || $date=='')
01286                 {
01287                         $time=time();
01288                 }
01289                 else
01290                         $time=$this->dateToTimestamp($date);
01291 
01292                  $moon_phases=$this->yearMoonPhases(date("Y",$time));            
01293          $day=$this->kDate("%n/%j/%Y",$time); 
01294                  $k=array_keys($moon_phases);
01295                  $ab=$this->abbr;
01296                  $this->abbr=true;
01297                  while(!$key=array_search($day,$k))
01298                  {               
01299                         $day=$this->operation('-',$day,'1%D');
01300                  }               
01301                  $this->abbr=$ab;
01302          return $moon_phases[$day]; 
01303      } 
01304          
01320          function getSunTime($latitude=0,$longitude=0,$timezone=1,$location='IT|Roma',$daylight=1,$date='')
01321          {
01322                 $result='';
01323                 if(!isset($date) || $date=='')
01324                 {
01325                         $time=time();
01326                 }
01327                 else
01328                         $time=$this->dateToTimestamp($date);
01329 
01330                 if($location!='' && ($latitude==0 || $longitude==0))
01331                 {
01332                         require_once '_it_geo_coord.dat.php';                   
01333                         $location=strtoupper($location);
01334                         if(in_array($location,array_keys($_geo_coord)))
01335                         {
01336                                 $exp=explode('|',$_geo_coord[$location]);
01337                                 $latitude=$exp[0];
01338                                 $longitude=$exp[1];
01339                         }                       
01340                 }
01341                 
01342                 $yday = date('z',$time); 
01343                 $mon = date('n',$time); 
01344                 $mday = date('j',$time); 
01345                 $year = date('Y',$time); 
01346                 
01347 
01348                 $DST=$this->_isDaylight($time); 
01349                 if($DST) 
01350                 {
01351                         $timezone = ($timezone + 1);
01352                 } 
01353                 
01354                 if($timezone == "13") 
01355                 {
01356                         $timezone = "-11";
01357                 } 
01358         
01359            $A = 1.5708; 
01360            $B = 3.14159; 
01361            $C = 4.71239; 
01362            $D = 6.28319;      
01363            $E = 0.0174533 * $latitude; 
01364            $F = 0.0174533 * $longitude; 
01365            $G = 0.261799  * $timezone; 
01366         
01367            # For astronomical twilight, use R = -.309017 
01368            # For     nautical twilight, use R = -.207912 
01369            # For        civil twilight, use R = -.104528 
01370            # For     sunrise or sunset, use R = -.0145439 
01371            $R = -.0145439;      
01372         
01373                 for ($i = 0; $i < 2; $i++) 
01374                 { 
01375         
01376                         if (!$i) 
01377                         {
01378                                 $J =  $A;  
01379                                 $type = "rise";
01380                         }    # calculate sunrise 
01381                     else  
01382                         {
01383                                 $J =  $C;  
01384                                 $type = "set"; 
01385                         }    # calculate sunset 
01386         
01387                    $K = $yday + (($J - $F) / $D); 
01388                    $L = ($K * .017202) - .0574039;              # Solar Mean Anomoly 
01389                    $M = $L + .0334405 * sin($L);                # Solar True Longitude 
01390                    $M += 4.93289 + (3.49066E-04) * sin(2 * $L); 
01391                   # Quadrant Determination 
01392                      if ($D == 0) 
01393                          {
01394                                 $this->error="Trying to normalize with zero offset..."; 
01395                                 $this->exitOnError();
01396                          } 
01397                          
01398                      while($M < 0)   
01399                          {
01400                                 $M = ($M + $D);
01401                          } 
01402                      
01403                          while($M >= $D) 
01404                          {
01405                                 $M = ($M - $D);
01406                          } 
01407                 
01408                            if (($M / $A) - intval($M / $A) == 0) 
01409                            {
01410                                         $M += 4.84814E-06;
01411                            } 
01412                            
01413                            $P = sin($M) / cos($M);                   # Solar Right Ascension 
01414                            $P = atan2(.91746 * $P, 1); 
01415                         
01416                            # Quadrant Adjustment 
01417                            if ($M > $C) 
01418                            {
01419                                         $P += $D;
01420                            } 
01421                            else 
01422                            {
01423                                         if ($M > $A) 
01424                                         {
01425                                                 $P += $B;
01426                                         } 
01427                            } 
01428                         
01429                            $Q = .39782 * sin($M);            # Solar Declination 
01430                            $Q = $Q / sqrt(-$Q * $Q + 1);     # This is how the original author wrote it! 
01431                            $Q = atan2($Q, 1); 
01432                         
01433                            $S = $R - (sin($Q) * sin($E)); 
01434                            $S = $S / (cos($Q) * cos($E)); 
01435                         
01436                            if (abs($S) > 1) 
01437                            {
01438                                  $result.='none';
01439                            }     # Null phenomenon 
01440                         
01441                            $S = $S / sqrt(-$S * $S + 1); 
01442                            $S = $A - atan2($S, 1); 
01443                         
01444                            if ($type == 'rise') 
01445                            {
01446                                         $S = $D - $S ;
01447                            } 
01448                         
01449                            $T = $S + $P - 0.0172028 * $K - 1.73364; # Local apparent time 
01450                            $U = $T - $F;                            # Universal timer 
01451                            $V = $U + $G;                            # Wall clock time 
01452                           # Quadrant Determination 
01453                              if ($D == 0) 
01454                                  {
01455                                         $this->error="Trying to normalize with zero offset..."; 
01456                                         $this->exitOnError();
01457                                  } 
01458                              
01459                                  while($V < 0)
01460                                  {
01461                                         $V = ($V + $D);
01462                                  } 
01463                              
01464                                  while($V >= $D) 
01465                                  {
01466                                         $V = ($V - $D);
01467                                  } 
01468 
01469                            $V = $V * 3.81972; 
01470                         
01471                            $hour = intval($V); 
01472                            $min  = intval((($V - $hour) * 60) + 0.5); 
01473                         
01474                            $result.="sun$type is at: "; 
01475                            
01476                if($this->timeFormat=='12' || $this->timeFormat==12 || $this->timeFormat='en')
01477                    $result.=date( "g:i A", mktime($hour,$min,0,$mon,$mday,$year) ); 
01478                else
01479                    $result.=date( "H:i", mktime($hour,$min,0,$mon,$mday,$year) );                 
01480                    
01481                            $result.='&nbsp;-&nbsp;';
01482                   }
01483                 return $result;
01484          }      
01485          
01486      function pg_time_easy($timestamp)
01487          {
01488             $Date_Time = explode(" ", $timestamp);
01489             $date_pieces = explode("-", $Date_Time[0]);
01490             $time_convert = explode("-", $Date_Time[1]);
01491             $time_pieces = explode(":", $time_convert[0]);
01492         
01493             $timestamp = mktime($time_pieces[0], $time_pieces[1], $time_pieces[2], $date_pieces[1], $date_pieces[2], $date_pieces[0]);
01494                 return $this->kDate('',$timestamp);
01495         }
01496         
01509         function timeToText($seconds, $format="h")
01510     {
01511         $hours=0;
01512         $minutes=0;
01513         $days=0;
01514 
01515         if($seconds<=60)
01516         {
01517             $hours=0;
01518             $minutes=0;
01519         }        
01520         elseif($seconds>=60 && $seconds<3600)
01521         {
01522             $hours=0;
01523             $minutes=$seconds/60;
01524             $t=explode('.',$minutes);
01525             $minutes=$t[0];
01526             $seconds=round("0.$t[1]"*60);
01527         }
01528         elseif($seconds>=3600 && $seconds<86400)
01529         {
01530                 $r=$seconds/3600;
01531                 $t=explode('.',$r);
01532                 $hours=$t[0];
01533                 if($hours>24)
01534                         $hours-=24;                                     
01535 
01536                 $minutes="0.$t[1]"*60;
01537             $t=explode('.',$minutes);
01538             $minutes=$t[0];
01539             $seconds=round("0.$t[1]"*60);
01540         }                           
01541         elseif($seconds>=86400)        
01542         {
01543             $r=$seconds/86400;
01544             $t=explode('.',$r);
01545             $days=$t[0];
01546             $hours="0.$t[1]"*24;
01547             if(!strstr($hours,"."))
01548             {
01549                 $minutes=0;
01550                  $seconds=0;
01551             }
01552             else
01553             {
01554                 $t=explode(".",$hours);
01555                 $hours=$t[0];
01556                 $mi="0.$t[1]"*60;
01557                 if(!strstr($mi,"."))
01558                 {
01559                     $minutes=$mi;
01560                     $seconds=0;
01561                 }
01562                 else
01563                 {
01564                     $t=explode(".",$mi);
01565                     $minutes=$t[0];
01566                     $seconds=round("0.$t[1]"*60);
01567                 }
01568             }        
01569         }
01570         
01571         if($hours>1)
01572             $hours.=($format=='h' || $format=='d') ? 'h ' : 'hours ';
01573         else
01574             $hours.=($format=='h' || $format=='d') ? 'h ' : 'hour ';
01575             
01576         if($minutes>1)
01577             $minutes.=($format=='h' || $format=='d') ? 'm ' : 'minutes ';
01578         else
01579             $minutes.=($format=='h' || $format=='d') ? 'm ' : 'minute ';        
01580             
01581         if($seconds>1)
01582             $seconds.=($format=='h' || $format=='d') ? 's ' : 'seconds ';
01583         else
01584             $seconds.=($format=='h' || $format=='d') ? 's ' : 'second ';
01585             
01586         if($days>1)
01587             $days.=($format=='h' || $format=='d') ? 'd ' : 'days ';
01588         else
01589             $days.=($format=='h' || $format=='d') ? 'd ' : 'day ';       
01590             
01591        $timestr=$hours.$minutes.$seconds;
01592        $datestr=$days;
01593         
01594         if($format=="d" || $format=="D")
01595             return $datestr.$timestr;
01596         else        
01597             return $timestr;
01598     }
01599         
01609         function secsToTime($secs)
01610     {
01611                 if($secs == 0)
01612         {
01613                         return "-empty-";
01614                 }
01615         else
01616         {
01617                         $hours = round(floor(($secs/3600)),0); // just the whole hours
01618                         $minutes = $secs % 3600;  //remainder
01619                         $minutes = $minutes/60;
01620                         
01621                         if($minutes == 0)
01622             { // tidy up output
01623                         $minutes = '00';
01624                 }
01625                                 
01626                 return $hours.":".$minutes;
01627                 }
01628         }     
01629     
01638     function isValid($date)
01639     {
01640        if(strstr($date, ':'))
01641            {
01642                         if(strstr($date," "))
01643                         {
01644                                 $t = explode(' ',$date);
01645                                 $time = $t[1];
01646                                 $date = explode($this->separator,$t[0]);
01647                         }
01648                         else
01649             {
01650                                 $time = $date;
01651                 $date = "";
01652             }
01653            }
01654            else
01655                    $date = explode($this->separator,$date);
01656         
01657            $time = (isset($time)) ? explode(':',$time) : "";
01658            
01659                 if(isset($time) && $time!="")
01660                 {
01661                         $hour = $time[0];
01662                         $mins = (isset($time[1])) ? $time[1] : 0;
01663                         $seconds = (isset($time[2])) ? $time[2] : 0;
01664                 }
01665                 else
01666                 {
01667                         $time = 1;
01668                         $hour = 0;
01669                         $mins = 0;
01670                         $seconds = 0;
01671                 }
01672        
01673        if($date!="")
01674        {
01675            switch($this->dateFormat) 
01676                 {
01677                 case 'ansi': $year=$date[1]; $month=$date[2]; $day=$date[0]; break; // using YYYY-MM-DD                         
01678                         case 'it': $day=$date[1]; $month=$date[0]; $year=$date[2]; break;// using dd-mm-yyyy
01679                         case 'br': $day=$date[1]; $month=$date[0]; $year=$date[2]; break;// using dd-mm-yyyy
01680                         case 'std': $month=$date[0]; $day=$date[1]; $year=$date[2]; break; // using MM-DD-YYYY                                                                          
01681                         case 'en': $month=$date[0]; $day=$date[1]; $year=$date[2]; break; // using MM-DD-YYYY                                                                                                       
01682                         default: $this->error='Date Format not recognized, must be "ansi", "it", "std" or "en" !! '; $this->exitOnError();
01683             }
01684         }
01685         
01686                 switch($this->timeFormat)
01687                 {
01688                         case 'it':
01689                         case 24:
01690                         case '24':
01691                                 if($hour>=0 && $hour<=24 && $mins>=0 && $mins<=59 && $seconds>=0 && $seconds<=59)
01692                                         $time=1;
01693                                 else
01694                                         $time=0;
01695                                 break;
01696                         case 'en':
01697                         case '12':
01698                         case 12:
01699                                 if($hour>=0 && $hour<=12 && $mins>=0 && $mins<=59 && $seconds>=0 && $seconds<=59)
01700                                         $time=1;
01701                                 else
01702                                         $time=0;
01703                                 break;
01704                         default: $this->error='Time Format not recognized, must be "it" or "24", "en" or "12" !! '; $this->exitOnError();
01705                 }          
01706                 if($date!="")
01707             $date = checkdate($month,$day,$year);               
01708         else
01709             $date=true;
01710             
01711                 if($date && $time)
01712                         return true;
01713                 elseif(!$date)
01714                         return -1;
01715                 elseif(!$time)
01716                         return -2;
01717                 elseif(!$date && !$time)
01718                         return false;
01719     }  
01720       
01721 
01739     function getInfo($date,$what,$format="short")
01740     {
01741         $result = false;
01742         if($this->isValid($date))
01743         {
01744           if($format=="short" || $format=="s")
01745             $this->abbr=true;
01746           elseif($format=="long" || $format=="l")
01747             $this->abbr=false;
01748           switch(strtolower($what))
01749           {
01750             case 'monthname':
01751                                         $m=$this->atomDate($date,'m');
01752                                         $result = $this->NToMonth($m);
01753                                         break;
01754             case 'dayname':
01755                                         $time=$this->dateToTimestamp($date);
01756                                         $weekday = date("w",$time);
01757                                         $result = $this->NToDay($weekday);
01758                                         break;
01759             case 'dayofweek':
01760                                         $time=$this->dateToTimestamp($date);
01761                                         $weekday = date("w",$time);
01762                                         $result = $weekday+1;
01763                                         break;
01764             case 'dayofyear':
01765                                         $time=$this->dateToTimestamp($date);
01766                                         $result = date("z",$time)+1;
01767                                         break;
01768             case 'week':
01769                                         $time=$this->dateToTimestamp($date);
01770                                         $week = date("W",$time);
01771                                         $result = $week;
01772                                         break;
01773             
01774             case 'trimester':
01775                                         $m=$this->atomDate($date,'m');
01776                                         if($m>1 && $m<=3)
01777                                             $result = 1;
01778                                         elseif($m>3 && $m<=6)
01779                                             $result = 2;
01780                                         elseif($m>6 && $m<=9)
01781                                             $result = 3;
01782                                         elseif($m>9 && $m<=12)
01783                                             $result = 4;
01784                                         break;            
01785 
01786             default: $this->error='You must specify what kind of info need!';
01787           }
01788         }
01789         else
01790             $this->error='Date Format not recognized, must be "ansi", "it", "std", "br" or "en" !! '; 
01791     
01792         $this->exitOnError();
01793         return $result;
01794     }
01795 
01796         function InvertDate($date=null)
01797         {
01798                 $date=str_replace('-',$this->separator,$date);
01799                 $date=str_replace('.',$this->separator,$date);  
01800                 list($obj1, $obj2, $obj3) = split($this->separator, $date, 3);
01801                 $date = $obj3 . $this->separator . $obj2 . $this->separator . $obj1;
01802                 if ( ( $date == ($this->separator . $this->separator) ) ) $date = 'Invalid Date!';
01803                 return $date;
01804         }
01805 
01806         function CompareDate($dateA, $operator, $dateB) 
01807         {
01808                 list($dayA,$monthA,$yearA) = explode("/",$dateA);
01809                 list($dayB,$monthB,$yearB) = explode("/",$dateB);
01810                 $date1 = $yearA.$monthA.$dayA;
01811                 $date2 = $yearB.$monthB.$dayB;
01812                 switch ($operator) {
01813                         case '==':
01814                                 if ($date1 == $date2) return true; else return false; 
01815                                 break;
01816                         case '=':
01817                                 if ($date1 == $date2) return true; else return false; 
01818                                 break;
01819                         case '>':
01820                                 if ($date1 > $date2) return true; else return false; 
01821                                 break;
01822                         case '<':
01823                                 if ($date1 < $date2) return true; else return false; 
01824                                 break;
01825                         case '>=':
01826                                 if ($date1 >= $date2) return true; else return false; 
01827                                 break;
01828                         case '<=':
01829                                 if ($date1 <= $date2) return true; else return false; 
01830                                 break;
01831                         case '!=':
01832                                 if ( !($date1 == $date2) ) return true; else return false; 
01833                                 break;
01834                         case '<>':
01835                                 if ( !($date1 == $date2) ) return true; else return false; 
01836                                 break;
01837                 }
01838         }
01839 
01840         function GetDay($date)
01841         {
01842                 list($day,$month,$year) = explode("/",$date);
01843                 return $day;
01844         }
01845 
01846         function GetMonth($date)
01847         {
01848                 list($day,$month,$year) = explode("/",$date);
01849                 return $month;
01850         }
01851 
01852         function GetYear($date)
01853         {
01854                 list($day,$month,$year) = explode("/",$date);
01855                 return $year;
01856         }
01857         
01858         function Between($date1, $date, $date2)   
01859         {    
01860                 $num1 = str_replace( "/", "", $this->InvertDate($date1) );
01861                 $num  = str_replace( "/", "", $this->InvertDate($date) );      
01862                 $num2 = str_replace( "/", "", $this->InvertDate($date2) );        
01863                 if ( ($num1 <= $num) && ($num <=$num2) )     
01864                         return TRUE;    
01865                 else     
01866                         return FALSE;   
01867         } 
01868 
01869     function makeTime()
01870     {
01871        $objArgs = func_get_args();
01872        $nCount = count($objArgs);
01873        if ($nCount < 7)
01874        {
01875            $objDate = getdate();
01876            if ($nCount < 1)
01877                $objArgs[] = $objDate["hours"];
01878            if ($nCount < 2)
01879                $objArgs[] = $objDate["minutes"];
01880            if ($nCount < 3)
01881                $objArgs[] = $objDate["seconds"];
01882            if ($nCount < 4)
01883                $objArgs[] = $objDate["mon"];
01884            if ($nCount < 5)
01885                $objArgs[] = $objDate["mday"];
01886            if ($nCount < 6)
01887                $objArgs[] = $objDate["year"];
01888            if ($nCount < 7)
01889                $objArgs[] = -1;
01890        }
01891        $nYear = $objArgs[5];
01892        $nOffset = 0;
01893        if ($nYear < 1970)
01894        {    
01895            if ($nYear < 1902)
01896                return 0;
01897            else if ($nYear < 1952)
01898            {
01899                $nOffset = -2650838400;
01900                $objArgs[5] += 84;
01901                // Apparently dates before 1942 were never DST
01902                if ($nYear < 1942)
01903                    $objArgs[6] = 0;
01904            }
01905            else
01906            {
01907                $nOffset = -883612800;
01908                $objArgs[5] += 28;
01909            }
01910        }
01911   
01912     return call_user_func_array("mktime", $objArgs) + $nOffset;
01913     }
01914 }
01915 ?>
CopyLeft (L) 2001-2006 - [MIOLO Development Team] SOLIS - Cooperativa de Soluções Livres - Lajeado/RS - Brasil