<?PHP
//
// Application "La poste #####"
//
// {{{ Description
/**
* Generic class for representation and manipulation of dates, times.
*
* Examples :
*
* <code>
*
* $MyDate=new LPMFDate;
* $MyDate->dateInXDays(10);
*
* //dd-mm-yyyy
* echo $MyDate->dateFormat('fr');
* //yyyy-mm-dd
* echo $MyDate->dateFormat('en');
*
* //(Lundi de Paques)= easter day + 1 day.
* $MyDate->dateInXDays(1,$MyDate->easterDay(2006));
* echo $MyDate->dateFormat('fr');
*
* //(Ascension)= easter day + 39 days.
* $MyDate->dateInXDays(39,$MyDate->easterDay(2006));
* echo $MyDate->dateFormat('fr');
*
* //(Pentecote)= easter day + 49 days.
* $MyDate->dateInXDays(49,$MyDate->easterDay(2006));
* echo $MyDate->dateFormat('fr');
*
* //(Lundi de Pentecote)= easter day + 50 days.
* $MyDate->dateInXDays(50,$MyDate->easterDay(2006));
* echo $MyDate->dateFormat('fr');
*
* //Finds the 25th day (from now) that is not a sunday and not saturday.
* $j=0;
* $k=1;
*
* do{
* $MyDate->dateInXDays($k);
* $dateDay=$MyDate->dateFormat('en');
* $datePart=explode('-',$dateDay);
* $day=mktime(0,0,0,$datePart[1],$datePart[2],$datePart[0]);
*
* if(!$MyDate->isSaturday($day) && !$MyDate->isSunday($day))
* {
* $j++;
* }
* /**
* * or
* * $MyDate->dateInXDays($k);
* * if(!$MyDate->isSaturday() && !$MyDate->isSunday()){$j++;}
* {@*}
* if($j >= 25){
* break;
* }
* $k++;
* }while(true);
*
* $MyDate->dateInXDays($k);
* echo $MyDate->dateFormat('fr');
*
* </code>
*/
// }}}
// {{{ class LPMFDate
/**
* protected class LPMFDate
*
* @package Frameworks__Foundation
* @category date
* @copyright Copyright 2004-2005, ####, La poste #####
* @author #### <###@####>
* @since PHP 4.3
* @version $Id: LPMFDate.php,v 1.2 ### Exp $
*/
class LPMFDate{
// {{{ properties
/**
* protected array $dateTime
*
* @var array
* @access protected
*/
var $dateTime;
/**
* protected int $Year
*
* @var int
* @access protected
*/
var $Year;
/**
* protected int $Month
*
* @var int
* @access protected
*/
var $Month;
/**
* protected int $Day
*
* @var int
* @access protected
*/
var $Day;
// }}}
// {{{ protected constructor LPMFDate
/**
* protected void LPMFDate()
*
* Creates a new Date object manipulation tool.
* @access protected
*/
function LPMFDate(){}
// }}}
// {{{ protected void dateInXDays()
/**
* protected void dateInXDays(int xdays[,string from1Date])
*
* Implements $this->dateTime.
*
* @see getdate()
* @see mktime()
* @see date()
* @param string $xdays
* @param string $from1Date
* @access protected
*/
function dateInXDays($xdays,$from1Date='')
{
if($from1Date=='')
{
$myDate=explode('-',date('Y-m-d'));
}else{
$myDate=explode('-',$from1Date);
}
$this->Month=$myDate[1];
$this->Year=$myDate[0];
$this->Day=$myDate[2];
$this->dateTime=getdate(mktime(0,0,0,$this->Month,$this->Day+$xdays,$this->Year));
}
// }}}
// {{{ protected string dateFormat()
/**
* protected string dateFormat([string lang])
*
* Returns a string date from array dateTime, the default
* string returned, is dd-mm-yyyy ( French Latin ) ,
* corresponding to $this->Month,$this->Day,$this->Year given by dateInXDays.
*
* @see dateInXDays()
* @param string $lang
* @return string string dateformat
* @access protected
*/
function dateFormat($lang='fr')
{
if($lang=='fr')
{
return(
sprintf('%02d',$this->dateTime['mday']).'-'.
sprintf('%02d',$this->dateTime['mon']).'-'.
$this->dateTime['year']
);
}
if($lang=='en')
{
return(
$this->dateTime['year'].'-'.
sprintf('%02d',$this->dateTime['mon']).'-'.
sprintf('%02d',$this->dateTime['mday'])
);
}
}
// }}}
// {{{ private int getTime()
/**
* private int getTime()
*
* Returns the Unix timestamp corresponding
* to $this->Month,$this->Day,$this->Year given by dateInXDays.
*
* @see dateInXDays()
* @see mktime()
* @return int timestamp gettime
* @access private
*/
function getTime()
{
return mktime(0,0,0,$this->Month,$this->Day,$this->Year);
}
// }}}
// {{{ protected boolean isSunday()
/**
* protected boolean isSunday()
*
* Returns true if the Unix timestamp for a date given, is a sunday or false if not.
*
* If $from1mktime is empty it uses the protected acces to
* $this->Month,$this->Day,$this->Year given by dateInXDays.
*
* @see dateInXDays()
* @see getTime()
* @see date()
* @param int $from1mktime
* @return bool boolean issunday
* @final isSunday()
* @access protected
*/
function isSunday($from1mktime='')
{
if(strlen($from1mktime) !='')
{
if(date('w',$from1mktime)==='0')
{
return true;
}
return false;
}else{
if(date('w',$this->getTime())==='0')
{
return true;
}
return false;
}
return false;
}
// }}}
// {{{ protected boolean isSaturday()
/**
* protected boolean isSaturday()
*
* Returns true if the Unix timestamp for a date given, is a saturday or false if not.
*
* If $from1mktime is empty it uses the protected acces to
* $this->Month,$this->Day,$this->Year given by dateInXDays.
*
* @see dateInXDays()
* @see getTime()
* @see date()
* @param int $from1mktime
* @return bool boolean issaturday
* @final isSaturday()
* @access protected
*/
function isSaturday($from1mktime='')
{
if(strlen($from1mktime) !='')
{
if(date('w',$from1mktime)==='6')
{
return true;
}
return false;
}else{
if(date('w',$this->getTime())==='6')
{
return true;
}
return false;
}
return false;
}
// }}}
// {{{ protected int easterDate()
/**
* protected int easterDate(int year)
*
* Returns the Unix timestamp corresponding to midnight on Easter of the given year.
*
* @link http://aa.usno.navy.mil/faq/docs/easter.html
* @see easter_date()
* @see easter_days()
* @param int $year
* @return int timestamp easterdate
* @final easterDate()
* @access protected
*/
function easterDate($year)
{
/**
* The rule is that Easter is the first Sunday after the first ecclesiastical full moon that occurs
* on or after March 21. The lunar cycles used by the ecclesiastical system are simple to program.
* The following algorithm will compute the date of Easter in the Gregorian Calendar system.
* The algorithm uses the year, y, to give the month, m, and day, d, of Easter.
* Please note the following: This is an integer calculation. All variables are integers
* and all remainders from division are dropped.
*
* G is the Golden Number-1
* H is 23-Epact (modulo 30)
* I is the number of days from 21 March to the Paschal full moon
* J is the weekday for the Paschal full moon (0=Sunday,1=Monday, etc.)
* L is the number of days from 21 March to the Sunday on or before the Paschal full moon (a number between -6 and 28)
*
* The algorithm is due to J.-M. Oudin (1940) and is reprinted in the
* Explanatory Supplement to the Astronomical Almanac, ed. P. K. Seidelmann (1992).
*/
$G = $year % 19;
$C = (int)($year / 100);
$H = (int)($C - (int)($C / 4) - (int)((8*$C+13) / 25) + 19*$G + 15) % 30;
$I = (int)$H - (int)($H / 28)*(1 - (int)($H / 28)*(int)(29 / ($H + 1))*((int)(21 - $G) / 11));
$J = ($year + (int)($year/4) + $I + 2 - $C + (int)($C/4)) % 7;
$L = $I - $J;
$m = 3 + (int)(($L + 40) / 44);
$d = $L + 28 - 31 * ((int)($m / 4));
$y = $year;
$E =mktime(0,0,0,$m,$d,$y);
return $E;
}
// }}}
// {{{ protected string easterDay()
/**
* protected string easterDay(int year)
*
* Returns the string date corresponding to midnight on Easter
* of the given year, the string returned, is yyyy-mm-dd ( English Latin ).
*
* @see easterDate()
* @see date()
* @param int $year
* @return string string easterday
* @final easterDay()
* @access protected
*/
function easterDay($year)
{
return date("Y-m-d",$this->easterDate($year));
}
// }}}
};
// }}}
?>