336 lines
11 KiB
PHP
336 lines
11 KiB
PHP
|
<?php
|
||
|
class Metier_Util_Date
|
||
|
{
|
||
|
private static $tabMoisEnLettres = array(
|
||
|
1 => 'Janvier',
|
||
|
2 => 'Février',
|
||
|
3 => 'Mars',
|
||
|
4 => 'Avril',
|
||
|
5 => 'Mai',
|
||
|
6 => 'Juin',
|
||
|
7 => 'Juillet',
|
||
|
8 => 'Août',
|
||
|
9 => 'Septembre',
|
||
|
10 => 'Octobre',
|
||
|
11 => 'Novembre',
|
||
|
12 => 'Décembre'
|
||
|
);
|
||
|
|
||
|
/**
|
||
|
* Retourne le numéro du mois donné 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 static function getNumMois($moisEnLettres)
|
||
|
{
|
||
|
foreach (self::$tabMoisEnLettres as $num=>$mois) {
|
||
|
$tabMoisSansAccents[$num]=strtr($mois, 'ééûÉÉÛ','eeueeu');
|
||
|
}
|
||
|
|
||
|
return array_search(ucfirst(strtolower(strtr($moisEnLettres, 'ééûÉÉÛ','eeuEEU'))), $tabMoisSansAccents);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* Retourne le libellé nu numéro du mois passé en paramètre
|
||
|
* @param int $moisEnChiffre
|
||
|
* @return string Libellé du mois / false si le mois passé en paramè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 string $dateDeb
|
||
|
* Date de début au format Ymd
|
||
|
* @param string $dateFin
|
||
|
* Date de fin (ultérieur à la date de début) au format Ymd
|
||
|
* @return number
|
||
|
*/
|
||
|
public static function nbMoisEntre($dateDeb, $dateFin=null)
|
||
|
{
|
||
|
if (empty($dateFin)) {
|
||
|
$dateFin = date('Ymd');
|
||
|
}
|
||
|
$dDeb = explode('-', self::dateT('Ymd','Y-m-d', $dateDeb));
|
||
|
$dFin = explode('-', self::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éfinits en php
|
||
|
* @param string $formatIN
|
||
|
* Format de la date d'entrée
|
||
|
* @param string $formatOUT
|
||
|
* Format de la date de sortie
|
||
|
* @param string $date
|
||
|
* Date d'entrée
|
||
|
* @return string
|
||
|
* Date formatée
|
||
|
*/
|
||
|
public static 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));
|
||
|
$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ès application de la pé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
|
||
|
*/
|
||
|
public static function period2Days($dateIN, $period, $inThePast=false)
|
||
|
{
|
||
|
$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 = Metier_Util_Chiffre::ChiffresEnLettres($tabP[0]);
|
||
|
}
|
||
|
if ($inThePast) {
|
||
|
$chiffre = -1*$chiffre;
|
||
|
}
|
||
|
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é
|
||
|
* @param date $date
|
||
|
* Date au format SSAA-MM-JJ ou SSAAMMJJ
|
||
|
* @param bool $weekend
|
||
|
* Considérer les WeekEnd comme feriés ? 1=Oui
|
||
|
* @return bool
|
||
|
**/
|
||
|
public 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é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ô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ô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;
|
||
|
}
|
||
|
}
|