204 lines
8.5 KiB
PHP
204 lines
8.5 KiB
PHP
|
<?
|
|||
|
|
|||
|
include_once('chiffres.php');
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* Classe de gestion des m<EFBFBD>thodes relatives <EFBFBD> la date et <EFBFBD> l'heure
|
|||
|
*
|
|||
|
* <p>d<EFBFBD>tail de la classe</p>
|
|||
|
*
|
|||
|
* @name nom de la classe
|
|||
|
* @author Nom de l'auteur <adresse@Email.dom>
|
|||
|
* @link
|
|||
|
* @copyright Pr<EFBFBD>nom Nom Ann<EFBFBD>e
|
|||
|
* @version 1.0.0
|
|||
|
* @package Nom du package
|
|||
|
*/
|
|||
|
|
|||
|
class WDate {
|
|||
|
|
|||
|
private static $tabMoisEnLettres = array( 1=>'Janvier',
|
|||
|
2=>'F<>vrier',
|
|||
|
3=>'Mars',
|
|||
|
4=>'Avril',
|
|||
|
5=>'Mai',
|
|||
|
6=>'Juin',
|
|||
|
7=>'Juillet',
|
|||
|
8=>'Ao<41>t',
|
|||
|
9=>'Septembre',
|
|||
|
10=>'Octobre',
|
|||
|
11=>'Novembre',
|
|||
|
12=>'D<>cembre');
|
|||
|
|
|||
|
/** Retourne le num<EFBFBD>ro du mois donn<EFBFBD> au format texte (janvier, mars, etc...)
|
|||
|
* @param string Mois en toute lettres (janvier, mars, etc...)
|
|||
|
* @return string Mois en Chiffe (1, 3, 12) / false en cas d'erreur
|
|||
|
*/
|
|||
|
public function getNumMois($moisEnLettres) {
|
|||
|
foreach (self::$tabMoisEnLettres as $num=>$mois)
|
|||
|
$tabMoisSansAccents[$num]=strtr($mois, '<27><><EFBFBD>','eeu');
|
|||
|
return array_search(ucfirst(strtolower(strtr($moisEnLettres, '<27><><EFBFBD>','eeu'))), $tabMoisSansAccents);
|
|||
|
}
|
|||
|
|
|||
|
/** Retourne le libell<EFBFBD> nu num<EFBFBD>ro du mois pass<EFBFBD> en param<EFBFBD>tre
|
|||
|
* @param int $moisEnChiffre
|
|||
|
* @return string Libell<EFBFBD> du mois / false si le mois pass<EFBFBD> en param<EFBFBD>tre est invalide
|
|||
|
*/
|
|||
|
public function getLibelleMois($moisEnChiffre) {
|
|||
|
if ($moisEnChiffre>0 && $moisEnChiffre<13)
|
|||
|
return self::$tabMoisEnLettres[$moisEnChiffre];
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
/** Retourne le nombre de mois entre 2 dates au format Ymd
|
|||
|
**
|
|||
|
** @param int $dateDeb Date de d<EFBFBD>but au format Ymd
|
|||
|
** @param int $dateFin Date de fin (ult<EFBFBD>rieur <EFBFBD> la date de d<EFBFBD>but) au format Ymd
|
|||
|
** @return int Nombre de mois
|
|||
|
**/
|
|||
|
function nbMoisEntre($dateDeb, $dateFin=TODAY) {
|
|||
|
$dDeb=explode('-', Wdate::dateT('Ymd','Y-m-d', $dateDeb));
|
|||
|
$dFin=explode('-', Wdate::dateT('Ymd','Y-m-d', $dateFin));
|
|||
|
return ($dFin[0]*12+$dFin[1])-($dDeb[0]*12+$dDeb[1]);
|
|||
|
}
|
|||
|
|
|||
|
/**
|
|||
|
* Conversion de formats de dates selon les formats de dates d<EFBFBD>finits en php
|
|||
|
* @param string Format de la date d'entr<EFBFBD>e
|
|||
|
* @param string Format de la date de sortie
|
|||
|
* @param string Date d'entr<EFBFBD>e
|
|||
|
* @return string Date format<EFBFBD>e
|
|||
|
*/
|
|||
|
public function dateT($formatIN, $formatOUT, $date) {
|
|||
|
$M='';
|
|||
|
switch ($formatIN) {
|
|||
|
case 'd M Y': $tmp=explode(' ', $date); $d=str_replace('1er', '1', $tmp[0]); $m=self::getNumMois($tmp[1]); $Y=$tmp[2]; break;
|
|||
|
case 'dMY': $Y=substr($date,-4);
|
|||
|
if (substr($date,0,3)=='1er') { $d=1; $posM=3; }
|
|||
|
elseif (is_numeric(substr($date,1,1))) { $d=substr($date,0,2); $posM=2; }
|
|||
|
else { $d=substr($date,0,1); $posM=1; }
|
|||
|
$M=trim(substr($date, $posM, strlen($date)-4-$posM));
|
|||
|
//echo "$date:$posM:$M".EOL;
|
|||
|
$m=self::getNumMois($M);
|
|||
|
break;
|
|||
|
case 'Ymd': $d=substr($date,6,2); $m=substr($date,4,2); $Y=substr($date,0,4); break;
|
|||
|
case 'ymd': $d=substr($date,4,2); $m=substr($date,2,2); $Y=substr($date,0,2);
|
|||
|
if ($Y<50) $Y='20'.$Y; else $Y='19'.$Y; break;
|
|||
|
case 'Y-m-d': $d=substr($date,8,2); $m=substr($date,5,2); $Y=substr($date,0,4); break;
|
|||
|
case 'Y-m': $d=1; $m=substr($date,5,2); $Y=substr($date,0,4); break;
|
|||
|
case 'd/m/Y': $tmp=explode('/', $date); $d=$tmp[0]; $m=$tmp[1]; $Y=$tmp[2]; break;
|
|||
|
case 'd.m.Y': $tmp=explode('.', $date); $d=$tmp[0]; $m=$tmp[1]; $Y=$tmp[2]; break;
|
|||
|
case 'd/m/y': $tmp=explode('/', $date); $d=$tmp[0]; $m=$tmp[1];
|
|||
|
if ($tmp[2]<date('y')) $Y='20'.$tmp[2]; else $Y='19'.$tmp[2]; break;
|
|||
|
case 'd-m-Y': $tmp=explode('-', $date); $d=$tmp[0]; $m=$tmp[1]; $Y=$tmp[2]; break;
|
|||
|
case 'm/d/Y': $tmp=explode('/', $date); $d=$tmp[1]; $m=$tmp[0]; $Y=$tmp[2]; break;
|
|||
|
case 'dmY': $d=substr($date,0,2); $m=substr($date,2,2); $Y=substr($date,4,4); break;
|
|||
|
case 'dmy': $d=substr($date,0,2); $m=substr($date,2,2); $Y=substr($date,4,2);
|
|||
|
if ($Y<50) $Y='20'.$Y; else $Y='19'.$Y; break;
|
|||
|
default: return $date;
|
|||
|
}
|
|||
|
|
|||
|
if ($m*1>0 && $m*1<10) $m='0'.($m*1); if ($d*1>0 && $d*1<10) $d='0'.($d*1);
|
|||
|
|
|||
|
switch ($formatOUT) {
|
|||
|
case 'd/m/Y': return $d.'/'.$m.'/'.$Y; break;
|
|||
|
case 'm/Y': return $m.'/'.$Y; break;
|
|||
|
case 'd M Y': return $d.' '.self::$tabMoisEnLettres[$m*1].' '.$Y; break;
|
|||
|
case 'M Y': return self::$tabMoisEnLettres[$m*1].' '.$Y; break;
|
|||
|
case 'Y': return $Y; break;
|
|||
|
case 'm': return $m; break;
|
|||
|
case 'd': return $d; break;
|
|||
|
case 'Ym': return $Y.$m; break;
|
|||
|
case 'Ymd': return $Y.$m.$d; break;
|
|||
|
case 'Y-m-d': return $Y.'-'.$m.'-'.$d; break;
|
|||
|
case 'Y/m/d': return $Y.'/'.$m.'/'.$d; break;
|
|||
|
default: return $date;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/** Calcul de la date apr<EFBFBD>s application de la p<EFBFBD>riode textuelle (deux ans, six mois, quinze jours, etc...)
|
|||
|
* @todo Fonction ne fonctionnant qu'avec un chiffre en un seul mot du genre dix mais pas quatre vingt dix !!!
|
|||
|
* @param date $dateIN au format Ymd
|
|||
|
* @param string $period (ex : cinq mois, six ans, un jour, 3 mois)
|
|||
|
* @return date
|
|||
|
*/
|
|||
|
function period2Days($dateIN, $period) {
|
|||
|
$dateV=self::dateT('Ymd', 'Ymd', $dateIN);
|
|||
|
if ($dateV<>$dateIN) return NULL;
|
|||
|
$d=substr($dateIN,6,2);
|
|||
|
$m=substr($dateIN,4,2);
|
|||
|
$Y=substr($dateIN,0,4);
|
|||
|
$period=strtolower(trim(strtr($period, "-.,", ' ')));
|
|||
|
if ($period=='') return NULL;
|
|||
|
$tabP=explode(' ', $period);
|
|||
|
if ($tabP[0]*1>0) $chiffre=$tabP[0]*1*1;
|
|||
|
else $chiffre=WChiffes::ChiffresEnLettres($tabP[0]);
|
|||
|
switch (end($tabP)) {
|
|||
|
case 'mois': $dateOUT=date('Ymd', mktime(0, 0, 0, $m+$chiffre, $d, $Y)); break;
|
|||
|
case 'an':
|
|||
|
case 'ans': $dateOUT=date('Ymd', mktime(0, 0, 0, $m, $d, $Y+$chiffre)); break;
|
|||
|
case 'jour':
|
|||
|
case 'jours': $dateOUT=date('Ymd', mktime(0, 0, 0, $m, $d+$chiffre, $Y)); break;
|
|||
|
default: $dateOUT=NULL;
|
|||
|
}
|
|||
|
return $dateOUT;
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
/** le jour est il feri<EFBFBD>
|
|||
|
* @param date $date Date au format SSAA-MM-JJ ou SSAAMMJJ
|
|||
|
* @param bool $weekend Consid<EFBFBD>rer les WeekEnd comme feri<EFBFBD>s ? 1=Oui
|
|||
|
* @return bool
|
|||
|
**/
|
|||
|
function jourFerie($date, $weekend=false) {
|
|||
|
$date =str_replace('-','',strtr($date, '/.:','---'));
|
|||
|
$jour =self::dateT('Ymd', 'd', $date);
|
|||
|
$mois =self::dateT('Ymd', 'm', $date);
|
|||
|
$annee=self::dateT('Ymd', 'Y', $date);
|
|||
|
$ferie=false;
|
|||
|
|
|||
|
// Jours feri<72>es fixes
|
|||
|
if($jour == 1 && $mois == 1) $ferie = true; // 1er janvier
|
|||
|
if($jour == 1 && $mois == 5) $ferie = true; // 1er mai
|
|||
|
if($jour == 8 && $mois == 5) $ferie = true; // 8 mai
|
|||
|
if($jour == 14 && $mois == 7) $ferie = true; // 14 juillet
|
|||
|
if($jour == 15 && $mois == 8) $ferie = true; // 15 aout
|
|||
|
if($jour == 1 && $mois == 11) $ferie = true; // 1 novembre
|
|||
|
if($jour == 11 && $mois == 11) $ferie = true; // 11 novembre
|
|||
|
if($jour == 25 && $mois == 12) $ferie = true; // 25 d<>cembre
|
|||
|
|
|||
|
// f<>tes religieuses mobiles
|
|||
|
$pak = @easter_date($annee);
|
|||
|
$jp = date("d", $pak);
|
|||
|
$mp = date("m", $pak);
|
|||
|
if($jp == $jour && $mp == $mois){ $ferie = true;} // P<>ques
|
|||
|
$lpk = mktime(date("H", $pak), date("i", $pak), date("s", $pak), date("m", $pak), date("d", $pak) +1, date("Y", $pak) );
|
|||
|
$jp = date("d", $lpk);
|
|||
|
$mp = date("m", $lpk);
|
|||
|
if($jp == $jour && $mp == $mois){ $ferie = true; }// Lundi de P<>ques
|
|||
|
$asc = mktime(date("H", $pak), date("i", $pak), date("s", $pak), date("m", $pak), date("d", $pak) + 39, date("Y", $pak) );
|
|||
|
$jp = date("d", $asc);
|
|||
|
$mp = date("m", $asc);
|
|||
|
if($jp == $jour && $mp == $mois){ $ferie = true;}//ascension
|
|||
|
$pe = mktime(date("H", $pak), date("i", $pak), date("s", $pak), date("m", $pak), date("d", $pak) + 49, date("Y", $pak) );
|
|||
|
$jp = date("d", $pe);
|
|||
|
$mp = date("m", $pe);
|
|||
|
if($jp == $jour && $mp == $mois) {$ferie = true;}// Pentec<65>te
|
|||
|
$lp = mktime(date("H", $asc), date("i", $pak), date("s", $pak), date("m", $pak), date("d", $pak) + 50, date("Y", $pak) );
|
|||
|
$jp = date("d", $lp);
|
|||
|
$mp = date("m", $lp);
|
|||
|
if($jp == $jour && $mp == $mois) {$ferie = true;}// lundi Pentec<65>te
|
|||
|
|
|||
|
// Samedis et Dimanches
|
|||
|
if ($weekend) {
|
|||
|
$jour_sem = date('N',mktime(0,0,0, $mois, $jour, $annee));
|
|||
|
if($jour_sem>5) $ferie = true;
|
|||
|
}
|
|||
|
|
|||
|
return $ferie;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
?>
|