PX : code

KronoClass by Tommaso D'Argenio
Download this code


Go to Url to get complete Package!!
http://www.phpclasses.org/browse.html/package/943.html
***********************************
***********************************

<?php
/*
+--+ Project Name: KronoClass
+--+ Version: 0.5
+--+ Project Author: Tommaso D'Argenio
+--+ Author Email: rajasi@ziobudda.net
+--+ Build Date:  January 10 2003 16.18 (CET)
+--+ Update: January 22 2003

+--+ DISCLAIMER
Copyright (c) 2002-03 Tommaso D'Argenio <rajasi@ziobudda.net>
This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public
License along with this program; if not, write to the
Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
http://www.fsf.org

+--+ NOTES FROM AUTHOR
Please, if you make any change in the code let me know by an email!!!

+--+ Requirements:
        PHP 4.0+
*/

/**
* Class for compute some calculations on date and time
*
* Copyright (c) 2002-2003 Tommaso D'Argenio <rajasi@ziobudda.net>

*
* @version        $Id: class.kronos.php,v 0.4 10 January 2003 16.18.00 (CET)  $*
*/
class Krono
{
    
/* private: array that contain the name of days in long and short format*/
    
var $day_name_ext;
    var 
$day_name_con;
    
    
/* private: array that contain the name of month in long and short format*/
    
var $month_name_ext;
    var 
$month_name_con;

    
/* private: some internal variables */
    
var $data_from;
    var 
$data_to;
    var 
$error;
    
    
/* public: variable for choose long or short day names format*/
    
var $abbr;
    
    
/* public: Set to desidered language */
    
var $lan
    
    
/* private: Version number */
    
var $version='0.5';
    
    
/**
    *    public: Set to desidered date format 
    *   possible values: it -> italian (dd-mm-yyyy)
    *                              en,std -> international (mm-dd-yyyy)
    *                              ansi -> used in dbase and other source (yyyy-mm-dd)
    */
    
var $date_format;
    
    
/* public: Char for separating date */
    
var $separator;
    
    
/* public: constructor */
    
function Krono($lan='it'$date_format='it'$separator='/')
    {
        
$this->day_name_ext=
        array(
                
'it'=>array('Domenica','Lunedi','Martedi','Mercoledi','Giovedi','Venerdi','Sabato'),
                
'en'=>array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'),
                
'de'=>array('Sonntag','Montag','Dienstag','Mittwoch','Donnerstag','Freitag','Samstag'),
                
'fr'=>array('Dimanche','Lundž','Mardž','Mercredž','Jeudž','Vendredž','Samedž'),
                
'es'=>array('Domingo','Lunes','Martes','Miercole','Jueves','Viernes','Sabado'),
                
'id'=>array('Minggu''Senin''Selasa''Rabu''Kamis''Jumat''Sabtu'), 
                
'no'=>array('Mandag','Tirsdag','Onsdag','Torsdag','Fredag','Lordag','Sondag'),
                
'jp'=>array('Nichiyoubi','Getsuyoubi','Kayoubi','Suiyoubi','Mokuyoubi','Kinyoubi','Douyoubi')
            );
            
        
$this->day_name_con=
        array(
                
'it'=>array('Dom','Lun','Mar','Mer','Gio','Ven','Sab'),
                
'en'=>array('Sun','Mon','Tue','Wed','Thu','Fri','Sat'),
                
'de'=>array('Son','Mon','Die','Mit','Don','Fre','Sam'),
                
'fr'=>array('Dim','Lun','Mar','Mer','Jeu','Ven','Sam'),
                
'es'=>array('Dom','Lun','Mar','Mie','Jue','Vie','Sab'),
                
'id'=>array('Min''Sen''Sel''Rab''Kam''Jum''Sab'),
                
'no'=>array('Man','Tir','Ons','Tor','Fre','Lor','Son'),
                
'jp'=>array('Nic','Get','Kay','Sui','Mok','Kin','Dou')
            );

        
$this->month_name_ext=
        array(
                
'it'=>array('Gennaio','Febbraio','Marzo','Aprile','Maggio','Giugno','Luglio','Agosto','Settembre','Ottobre','Novembre','Dicembre'),
                
'en'=>array('January','February','March','April','May','June','July','August','September','October','November','December'),
                
'de'=>array('Januar','Februar','Marz','April','Mai','Juni','Juli','August','September','Oktober','November','Dezember'),
                
'fr'=>array('Janvier','Fevrier','Mars','Avril','Mai','Juin','Juillet','Aout','Septembre','Octobre','Novembre','Decembre'),
                
'es'=>array('Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre'),                
                
'id'=>array('Januari''Februari''Maret''April''Mei''Juni''Juli''Agustus''September''Oktober''November''Desember'),
                
'no'=>array('Januar','February','Mars','April','Mai','Juni','Juli','Oktober','Desember'),
                
'jp'=>array('Ichigatsu','Nigatsu','Sangatsu','Shigatsu','Gogatsu','Rokugatsu','Shicigatsu','Hachigatsu','Kugatsu','Jugatsu','Juichigatsu','Junigatsu')                
            );
            
        
$this->month_name_con=
        array(
                
'it'=>array('Gen','Feb','Mar','Apr','Mag','Giu','Lug','Ago','Set','Ott','Nov','Dic'),
                
'en'=>array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'),
                
'de'=>array('Jan','Feb','Mar','Apr','Mag','Jun','Jul','Aug','Sep','Okt','Nov','Dez'),
                
'fr'=>array('Jan','Fev','Mar','Avr','Mai','Jui','Jul','Aou','Sep','Oct','Nov','Dec'),
                
'es'=>array('Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'),
                
'id'=>array('Jan''Feb''Mar''Apr''Mei''Jun''Jul''Agu''Sep''Okt''Nov''Des'),
                
'no'=>array('Jan','Feb','Mar','Apr','Mai','Jun','Jul','Okt','Des'),
                
'jp'=>array('Ich','Nig','San','Shi','Gog','Rok','Shi','Hac','Kug','Jug','Jui','Jun')                
            );

        
$this->lan=$lan;
        
$this->date_format=$date_format;
        
$this->separator=$separator;
    }
    
    
/********************************************
    ********************************************
    *******    PRIVATE FUNCTIONS - DON'T TOUCH!! ******                                 
    ********************************************
    ********************************************
    ********************************************/
    
    
    /* private: function that exit with the error message if given*/
    
function exit_on_error()
    {
        if(
$this->error!='')
            echo 
' [Fatal Error] <b>'.$this->error.'</b> ';
        exit;
    }
    
    
/* private: return the last modified date of class file */
    
function _update()
    {
        
$s=stat(__FILE__);
        return 
$this->k_date('%l %d %F %Y',$s[9]);
    }
    
    
/* private: return the format string for date function according to date_format parameter and separator*/
    
function _format()
    {
        switch(
$this->date_format)
        {
            case 
'ansi': return 'Y'.$this->separator.'m'.$this->separator.'d'; break;
            case 
'it': return 'd'.$this->separator.'m'.$this->separator.'Y'; break;
            case 
'en'
            case 
'std'
                return 
'm'.$this->separator.'d'.$this->separator.'Y'; break;        
            default: 
$this->error='Date Format not recognized, must be "ansi", "it", "std" or "en" !! '$this->exit_on_error();            
        }
    }
    
    
/* private: return the literal name of language code */
    
function _language()
    {
        switch(
$this->lan)
        {
            case 
'it': return 'Italian'; break;
            case 
'en': return 'English'; break;
            case 
'de': return 'Deutch'; break;
            case 
'fr': return 'French'; break;
            case 
'es': return 'Spanish'; break;
            case 
'in': return 'Indonesian'; break;
            case 
'nw': return 'Norway'; break;
            default: return 
'Language not recognized!!'
        }
    }
    
    
/* private: print out some debug information */
    
function _debug()
    {
        echo 
'<span style="font-family:helvetica,verdana,serif;font-size:12px;color:darkgray;">
                    <b>Debug Information</b><br>
                    Format of Date: <i>'
.$this->_format().'</i><br>
                    Date Separator: <i>'
.$this->separator.'</i><br>
                    Language: <i>'
.$this->_language().'</i>
                      </span>
                    <br><hr size="1px" width="50%" color="black" align="left"><br>
                    '
;
    }
    
/* private: print out a disclaimer */
    
function _disclaimer()
    {
        echo 
'<span style="font-family:helvetica,verdana,serif;font-size:14px;color:#ff9900;">';
        echo 
'<b>KronoClass</b> v. '.$this->version.' <br>';
        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>';
        echo 
'Last modified on: '.$this->_update().'</i><br><hr size="1px" width="50%" color="black" align="left"></span><br>';
    }
    
    
/********************************************
    ********************************************
    *******    PUBLIC  FUNCTIONS - DON'T TOUCH!! ******                                 
    ********************************************
    ********************************************
    ********************************************/
    
    /* public: convert the name of a day in its numerical value. i.e.: Monday stay for 0, Saturday stay for 6*/
    
function day_to_n($day)
    {
        if(
$day=='' || strlen($day)<3)
        {
            
$this->error='Day name not valid!';
            
$this->exit_on_error();            
        }
        
        
$day=ucwords($day);        
        if(
strlen($day)==3)
            
$ar=$this->day_name_con[$this->lan];
        else
            
$ar=$this->day_name_ext[$this->lan];
            
        if(
in_array($day,$ar))
        {
            
$k=array_keys($ar,$day);
            return(
$k[0]);
        }
    }
    
    
/* public: convert the numerical value of a day in its name. Short o long format is choosed by setting the abbr value to true o false*/
    
function n_to_day($day)
    {
        if(
$day>|| $day<0)
        {
            
$this->error='Day range not valid. Must be 0 to 6!';
            
$this->exit_on_error();
        }
        
        if(
$this->abbr===true)
            return(
$this->day_name_con[$this->lan][$day]);
        elseif(
$this->abbr!=true)
            return(
$this->day_name_ext[$this->lan][$day]);
    }
    
    
/* public: convert the name of a month in its numerical value. i.e.: February stay for 2, December stay for 12*/
    
function month_to_n($month)
    {
        if(
$month=='' || strlen($month)<3)
        {
            
$this->error='Month name not valid!';
            
$this->exit_on_error();            
        }
        
        
$month=ucwords($month);        
        if(
strlen($month)==3)
            
$ar=$this->month_name_con[$this->lan];
        else
            
$ar=$this->month_name_ext[$this->lan];
            
        if(
in_array($month,$ar))
        {
            
$k=array_search($month,$ar);
            return(
$k+1);
        }
        else
            return -
1;
    }
    
    
/* public: convert the numerical value of a month in its name. Short o long format is choosed by setting the abbr value to true o false*/
    
function n_to_month($month)
    {
        if(
$month>12 || $month<1)
        {
            
$this->error='Month range not valid. Must be 1 to 12!';
            
$this->exit_on_error();
        }

        if(
$this->abbr===true)
            return(
$this->month_name_con[$this->lan][$month-1]);
        elseif(
$this->abbr!=true)
            return(
$this->month_name_ext[$this->lan][$month-1]);
    }


    
/** public: define if the day of date given is equal to day given. 
    *    Is Friday the 22nd of November 2002 ? 
    *    date according to date_format parameter passed on inizialization
    */
    
function is_day($data,$day)
    {
        
$data=str_replace('-','/',$data);
        
$data=str_replace('.','/',$data);
        
$exp=explode('/',$data);
        
        
$data_unix=$this->k_mktime($exp);
        
$giorno_unix=date('w',$data_unix);
    
        if(!
is_numeric($day))
        {
            
$day=$this->day_to_n($day);        
        }
    
        if(
$giorno_unix==$day)
            return 
1;
        else
            return 
$this->n_to_day($giorno_unix);    
    }

    
/* public: trasform a classical date format in unix timestamp format. 
    *  date according to date_format parameter passed on inizialization
    */
    
function date_to_timestamp($date)
    {
        
$date=str_replace('-','/',$date);
        
$date=str_replace('.','/',$date);
        
$exp=explode('/',$date);
        return 
$this->k_mktime($exp);
    }
    
    
/* public: define what's the day difference between two given date. 
    *  $data_ini=date_from, 
    *  $data_fin=date_to 
    *  date according to date_format parameter passed on inizialization
     */
    
function days_diff($data_ini,$data_fin)
    {
        
$data_ini=str_replace('-','/',$data_ini);
        
$data_ini=str_replace('.','/',$data_ini);
        
$data_fin=str_replace('-','/',$data_fin);
        
$data_fin=str_replace('.','/',$data_fin);
        
        
$exp_ini=explode('/',$data_ini);
        
$exp_fin=explode('/',$data_fin);
        
        
$ini=date('z',$this->k_mktime($exp_ini));
        
$fin=date('z',$this->k_mktime($exp_fin));

        
$days floor(($this->k_mktime($exp_fin)-$this->k_mktime($exp_ini))/(60*60*24));        
        return 
$days;
    }

    
/* 
        public: give the difference between two times. (i.e.: how minutes from 4.50 to 12.50?). 
        Accept only 24H format.
        
        the time is a string like: "4.50" or "4:50"
    
        return result depending on $result_in content:
            "m" -> for minutes
            "s" -> for seconds
            "h" -> for hours        
    */
    
function times_diff($time_from,$time_to,$result_in="m")
    {
        if( (
strstr($time_from,'.') || strstr($time_from,':')) && (strstr($time_to,'.') || strstr($time_to,':')) )
        {
            
$time_from=str_replace(':','.',$time_from);
            
$time_to=str_replace(':','.',$time_to);
            
            
$t1=explode('.',$time_from);
            
$t2=explode('.',$time_to);
            
            
$h1=$t1[0];
            
$m1=$t1[1];
            
            
$h2=$t2[0];
            
$m2=$t2[1];
            
            if(
$h1<=24 && $h2<=24 && $h1>=&& $h2>=&& $m1<=59 && $m2<=59 && $m1>=&& $m2>=0)
            {
                
$diff=($h2*3600+$m2*60)-($h1*3600+$m1*60);
                if(
$result_in=="s")
                    return 
$diff;
                elseif(
$result_in=="m")
                {
                    return 
$diff/60;
                }
                elseif(
$result_in=="h")
                {
                    
$r=$diff/3600;
                    
$t=explode('.',$r);
                    
$h=$t[0];
                    if(
$h>24)
                        
$h-=24;                    
                    
$m=round("0.$t[1]"*60);
                    return 
$h.'h'.$m.'m';
                }            
            }
            else
            {
                
$this->error='Time range not valid. Must be 0 to 24 for hours and 0 to 59 for minutes!';
                
$this->exit_on_error();
            }                            
        }
        else
        {
            
$this->error='Time format not valid. Must be in format HH:mm or HH.mm';
            
$this->exit_on_error();        
        }    
    }
    
    
/*
        public: add some minutes or hours to a given time.
        i.e.: (add 2 hours to 14.10 -> result is 16.10)
        Accept only 24H format.
        
        the time is a string like: "4.50" or "4:50"
        $add is the hours or minutes to add
        $what is what add to 
            "m" -> for add minutes
            "h" -> for add hours
            "t" -> for add time string given in HH:mm format
        
        result is in format HH:mm
        return -1 on error        
    */
    
function times_add($time,$add,$what)
    {
        if( (
strstr($time,'.') || strstr($time,':')))
        {
            
$time=str_replace(':','.',$time);            
            
$t1=explode('.',$time);            
            
$h1=$t1[0];
            
$m1=$t1[1];
            if(
$h1<=24 && $h1>=0  && $m1<=59 && $m1>=0)
            {
                if(
$what=="m")
                {
                    
$res=($h1*60)+$m1+$add;
                    
$r=$res/60;
                    
$t=explode('.',$r);
                    
$h=$t[0];
                    if(
$h>24)
                        
$h-=24;                    
                    
$m=round("0.$t[1]"*60);
                    return 
$h.':'.$m;
                }
                elseif(
$what=="h")
                {
                    
$res=($h1*60)+$m1+($add*60);
                    
$r=$res/60;
                    
$t=explode('.',$r);
                    
$h=$t[0];
                    if(
$h>24)
                        
$h-=24;                    
                    
$m=round("0.$t[1]"*60);
                    return 
$h.':'.$m;                
                }
                elseif(
$what=="t")
                {
                    if( (
strstr($add,'.') || strstr($add,':')))
                    {
                        
$add=str_replace(':','.',$add);            
                        
$t1=explode('.',$add);            
                        
$h2=$t1[0];
                        
$m2=$t1[1];
                        if(
$h2<=24 && $h2>=0  && $m2<=59 && $m2>=0)
                        {
                            
$res=($h1*60)+($h2*60)+$m1+$m2;
                            
$r=$res/60;
                            
$t=explode('.',$r);
                            
$h=$t[0];
                            if(
$h>24)
                                
$h-=24;
                            
$m=round("0.$t[1]"*60);
                            return 
$h.':'.$m;                                            
                        }                        
                    }                    
                    else
                    {
                        
$this->error='Time format not valid. Must be in format HH:mm or HH.mm';
                        
$this->exit_on_error();        
                    }    
                }
            }
            else
            {
                
$this->error='Time range not valid. Must be 0 to 24 for hours and 0 to 59 for minutes!';
                
$this->exit_on_error();
            }                            
        }
        else
        {
            
$this->error='Time format not valid. Must be in format HH:mm or HH.mm';
            
$this->exit_on_error();        
        }    
    }
    
    
/* public: define how days left to given date. date according to date_format parameter passed on inizialization */
    
function how_to($data)
    {
        
$data=str_replace('-','/',$data);
        
$data=str_replace('.','/',$data);
        
$exp=explode('/',$data);
        
//        $data_unix=mktime(0,0,0,$exp[1],$exp[0],$exp[2]);
        
$data_unix=$this->k_mktime($exp);
        return (
date("z",$data_unix)-(Date("z")));
    }

    
/* public: define how many days (give it in name format) are in period given. i.e.: How friday are from Nov,1 2002 to Mar,23 2003 ?*/
    
function how_days($data_ini,$data_fin,$day)
    {
        
$data_ini=str_replace('-','/',$data_ini);
        
$data_ini=str_replace('.','/',$data_ini);    
    
        
$eta_data=$this->days_diff($data_ini,$data_fin);

        
$giorno=(int)$this->atom_date($data_ini,'d');
        
$mese=(int)$this->atom_date($data_ini,'m');
        
$anno=(int)$this->atom_date($data_ini,'Y');
        
        
$count=0;

        for(
$i=0;$i<$eta_data;$i++)
        {        
            
$data=date($this->_format(),mktime(0,0,0,$mese,$giorno+$i,$anno));
            if(
$this->is_day($data,$day)===1)
                
$count++;
        }        
        return 
$count;
    }
    
    
/* public: transform date accordingly to format used */
    
function k_mktime($exp)
    {
        switch(
$this->date_format
        {
            case 
'ansi': return mktime(0,0,0,$exp[1],$exp[2],$exp[0]); break; // using YYYY-MM-DD                
            
case 'it': return mktime(0,0,0,$exp[1],$exp[0],$exp[2]); break;// using DD-MM-YYYY
            
case 'std': return mktime(0,0,0,$exp[0],$exp[1],$exp[2]); break; // using MM-DD-YYYY                                        
            
case 'en': return mktime(0,0,0,$exp[0],$exp[1],$exp[2]); break; // using MM-DD-YYYY                                                        
            
default: $this->error='Date Format not recognized, must be "ansi", "it", "std" or "en" !! '$this->exit_on_error();
        }
    }
    
    
/*
        public: return a single component of given date according to format in date_format
        $atom='m' for return month;
                    'd' for return day;
                    'y' for return last two number of year
                    'Y' for return entire year
    */
    
function atom_date($date,$atom)
    {
        
$exp=explode('/',$date);

        switch(
$this->date_format)
        {
            case 
'ansi':
            {
                switch(
$atom)
                {
                    case 
'd': return $exp[2]; break;
                    case 
'm': return $exp[1]; break;
                    case 
'y': return substr($exp[0],2,2); break;
                    case 
'Y': return $exp[0]; break;
                    default: 
$this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'$this->exit_on_error();
                }
                break;
            }            
            case 
'it':
            {
                switch(
$atom)
                {
                    case 
'd': return $exp[0]; break;
                    case 
'm': return $exp[1]; break;
                    case 
'y': return substr($exp[2],2,2); break;
                    case 
'Y': return $exp[2]; break;
                    default: 
$this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'$this->exit_on_error();
                }
                break;
            }            
            case 
'en':
            case 
'std':
            {
                switch(
$atom)
                {
                    case 
'd': return $exp[1]; break;
                    case 
'm': return $exp[0]; break;
                    case 
'y': return substr($exp[2],2,2); break;
                    case 
'Y': return $exp[2]; break;
                    default: 
$this->error='Atom not recognized, must be "d", "m", "y" or "Y" !!'$this->exit_on_error();
                }
                break;
            }            
            default: 
$this->error='Date Format not recognized, must be "ansi", "it", "std" or "en" !! '$this->exit_on_error();            
        }
    }
    

    
/* public: date like function. Using the same format functionality 
        valid format parameter:
        %l (L lowercase): Day textual long
        %d: Day of month, 2 digits with leading zeros
        %F: Month textual Long
        %Y: Year, 4 digits
        %y: Year, 2 digits
        %m: Month numeric, 2 digits with leading zeros
        %D: Day textual short
        %M: Month textual short
        %n: Month numeric, without leading zeros
        %j: Day of month, without leading zeros
    */
    
function k_date($format="%l %d %F %Y",$timestamp=0)
    {    
        if(
$timestamp==0)
            
$timestamp=time();
            

        if(!
preg_match('/\%l|\%F|\%D|\%M/',$format))
        {
            return 
date(str_replace('%','',$format),$timestamp);
        }
        else
        {
            
$out=$format;
            if(
strstr($format,'%l'))
            {
                
$this->abbr=false;
                
$out=str_replace('%l',$this->n_to_day(date('w',$timestamp)),$out);
            }
            if(
strstr($format,'%F'))
            {
                
$this->abbr=false;
                
$out=str_replace('%F',$this->n_to_month(date('m',$timestamp)),$out);
            }
            if(
strstr($format,'%D'))
            {
                
$this->abbr=true;
                
$out=str_replace('%D',$this->n_to_day(date('w',$timestamp)),$out);
            }
            if(
strstr($format,'%M'))
            {
                
$this->abbr=true;
                
$out=str_replace('%M',$this->n_to_month(date('m',$timestamp)),$out);
            }            
            if(
strstr($format,'%Y'))
                
$out=str_replace('%Y',date('Y',$timestamp),$out);
            if(
strstr($format,'%y'))
                
$out=str_replace('%y',date('y',$timestamp),$out);
            if(
strstr($format,'%d'))
                
$out=str_replace('%d',date('d',$timestamp),$out);
            if(
strstr($format,'%m'))
                
$out=str_replace('%m',date('m',$timestamp),$out);
            if(
strstr($format,'%n'))
                
$out=str_replace('%n',date('n',$timestamp),$out);
            if(
strstr($format,'%j'))
                
$out=str_replace('%j',date('j',$timestamp),$out);

            return 
$out;
        }
    }
    
    
/* Public: Perform operation like sum or subtraction on date 
        $operator: '+' -> for sum
                          'sum' -> for sum
                          'add' -> for sum
                          '-' -> for subtraction
                          'sub'-> for subtraction
                          'sot'-> for subtraction
                          
        $date given in DD/MM/YYYY format
        
        $operand is a number plus '%D' for days, '%M' for months, '%Y' for years
    
        Example:
        Add 1 month to a date:
        $obj->operation('+','10/01/2003','1%M');
        
        Subtract 20 days from a date:
        $obj->operation('-','10/01/2003','20%D');
    */    
    
function operation($operator,$date,$operand)
    {
        
$ts=$this->date_to_timestamp($date);
        
        if(!
strstr($operand,'%'))
        {
            
$this->error='Bad operand type!!';
            
$this->exit_on_error();
        }

        
$t=explode('%',$operand);
        
$how=$t[0];
        
        switch(
$t[1])
        {
            case 
'D':
            {
                if(
$operator=='+' || $operator=='sum' || $operator=='add')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts)+$how,date('Y',$ts)));
                }
                elseif(
$operator=='-' || $operator=='sub' || $operator=='sot')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts)-$how,date('Y',$ts)));                
                }
                else
                {
                    
$this->error='Operator not recognized!!';
                    
$this->exit_on_error();
                }
                break;
            }
            case 
'M':
            {
                if(
$operator=='+' || $operator=='sum' || $operator=='add')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts)+$how,date('d',$ts),date('Y',$ts)));
                }
                elseif(
$operator=='-' || $operator=='sub' || $operator=='sot')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts)-$how,date('d',$ts),date('Y',$ts)));                
                }
                else
                {
                    
$this->error='Operator not recognized!!';
                    
$this->exit_on_error();
                }
                break;
            }
            case 
'Y':
            {
                if(
$operator=='+' || $operator=='sum' || $operator=='add')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts),date('Y',$ts)+$how));
                }
                elseif(
$operator=='-' || $operator=='sub' || $operator=='sot')
                {
                    return 
date($this->_format(),mktime(0,0,0,date('m',$ts),date('d',$ts),date('Y',$ts)-$how));                
                }
                else
                {
                    
$this->error='Operator not recognized!!';
                    
$this->exit_on_error();
                }
                break;
            }
            default: 
            {
                
$this->error='Bad operand type!!';
                
$this->exit_on_error();
            }
        }        
    }    
    
    
/* public: Return the timestamp from a NIST TIME SERVER on the net. Get the atomic time! 
        attention, have to stay on line for work!!
    */
    
function net_timestamp($server='time-a.nist.gov'$port=37
    {           
        if (
$fp fsockopen($server$port)) 
        { 
            
fputs($fp"\n"); 
            
$timevalue fread($fp49); 
            
fclose($fp); 
        } 
        else
        {
            
$this->error=$server.' Time Server unavailable or u\'re not connected on the net!!';
            
$this->exit_on_error();
        }

        
$ts = (abs(hexdec('7fffffff') - hexdec(bin2hex($timevalue)) - hexdec('7fffffff')) - 2208988800); 
        return 
$ts
    } 

    
/* public: returns the current time in swatch .beat format. Remember that 1000 beats = 24 hours */ 
    
function swatch_time() 
    { 
      
$offset 60
      
$beat_division 24 60 1000
      
$current_date getdate(time()); 
      
$hour $current_date["hours"]; 
      
$minute $current_date["minutes"]; 
      
$seconds $current_date["seconds"]; 
      
$total_minutes $minute $offset $hour 60
      
$beats round ($total_minutes $beat_division); 
      if (
$beats >= 1000
      { 
        
$beats $beats 1000
      } 
       return (
"@".$beats); 
    }     
    
    
/* public: Transform a MySQL like timestamp to a readable format
        ie: 20011210002745 -> December 10, 2001, 12:27 am
        format is defined in date_format!
    */
    
function mysql_time_easy($timestamp
    { 
        
$hour substr($timestamp,8,2);  
        
$minute substr($timestamp,10,2);  
        
$second substr($timestamp,12,2);  
        
$month substr($timestamp,4,2);  
        
$day substr($timestamp,6,2);  
        
$year substr($timestamp,0,4);  
        
$mktime mktime($hour$minute$second$month$day$year);  
        
$format=$this->_format();
        
$f=explode($this->separator,$format);
        
$format='%'.$f[0].$this->separator.'%'.$f[1].$this->separator.'%'.$f[2].' %g:%i %a';
         
$formated $this->k_date($format,$mktime); 
        return 
$formated
    } 
    
    
/* public: Get the date of Nth day of the month ..
        ie: what is the date of the 2nd Sunday of April 2003 ???        
    */
    
function get_nth_day($number,$weekday,$month,$year=0
    { 
        if(
$number>5)
        {
            
$this->error='There isn\'t more than 5 '.$weekday.' in a month, usually!!';
            
$this->exit_on_error();
        }

        
$date_counter=1;  
        
$week_counter=0;  
        
        if(
$year==0)
            
$year=date('Y');
        
        if(
strlen($weekday)>3)
            
$format_dow='%l';
        else
            
$format_dow='%D';
        
        if(!
is_numeric($month))
        {
            
$month=$this->month_to_n($month);
        }
        
        do
        {
            
$itsit=mktime(0,0,0,$month,$date_counter,$year); 
            
$dow=$this->k_date($format_dow,$itsit); 
            if(
$dow==$weekday)
            {   
                
$week_counter++; 
            } 
            
            if( (
$week_counter==$number) && ($weekday==$dow) ) 
            {
                
$week_counter=$number
                
$date_counter--;
            } 
            else 
            { 
                
$date_counter++; 
            } 
        } 
        while(
$week_counter<$number);

        
$itsit=mktime(0,0,0,$month,$date_counter,$year); 
        
$format=$this->_format();
        
$f=explode($this->separator,$format);
        
$format='%'.$f[0].$this->separator.'%'.$f[1].$this->separator.'%'.$f[2];                 
        
        if(
$this->k_date('n',$itsit)!=$month)
        {
            
$this->error='Bad request, try again!!';
            
$this->exit_on_error();
        }
        else
        {
            return 
$this->k_date($format,$itsit); 
        }
    }     
    
    
/* public: return the date in ancient roman date format 
        attention: the date is output in the form: ddmmyyyy
                        without separator    
        support maximum to 5000 years!!!
    */
    
function roman_date($date)
    {
        
$unit=array(1=>'I',2=>'II',3=>'III',4=>'IV',5=>'V',6=>'VI',7=>'VII',8=>'VIII',9=>'IX');
        
$tens=array(10=>'X',20=>'XX',30=>'XXX',40=>'XL',50=>'L',60=>'LX',70=>'LXX',80=>'LXXX',90=>'XC');
        
$hund=array(100=>'C',200=>'CC',300=>'CCC',400=>'CD',500=>'D',600=>'DC',700=>'DCC',800=>'DCCC',900=>'CM');
        
$thou=array(1000=>'M',2000=>'MM',3000=>'MMM',4000=>'MMMM',5000=>'MMMMM');    

        if(
$date=='')
        {
            
$date=date('d/m/Y',time());        
            
$old_state=$this->date_format;
            
$this->date_format='it';
            
$year=$this->atom_date($date,'Y');
            
$month=$this->atom_date($date,'m');
            
$day=$this->atom_date($date,'d');
            
$this->date_format=$old_state;
        }
        else
        {
            
$year=$this->atom_date($date,'Y');
            
$month=$this->atom_date($date,'m');
            
$day=$this->atom_date($date,'d');
        }
        
         
$y_thou substr($year, -41) * 1000
         
$y_hund substr($year, -31) * 100
         
$y_tens substr($year, -21) * 10
         
$y_unit substr($year, -11); 
         
         
$m_tens=substr($month,-2,1)*10;
         
$m_unit=substr($month,-1,1);
         
         
$d_tens=substr($day,-2,1)*10;
         
$d_unit=substr($day,-1,1);
         
         return 
$tens[$d_tens].$unit[$d_unit].
                     
$tens[$m_tens].$unit[$m_unit].
                    
$thou[$y_thou].$hund[$y_hund].$tens[$y_tens].$unit[$y_unit];
    }
}
?>

Comments or questions?
PX is running PHP 5.2.17
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.