MkFramework
 All Data Structures Functions
plugin_date.php
1 <?php
2 /*
3 This file is part of Mkframework.
4 
5 Mkframework is free software: you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation, either version 3 of the License.
8 
9 Mkframework is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13 
14 You should have received a copy of the GNU Lesser General Public License
15 along with Mkframework. If not, see <http://www.gnu.org/licenses/>.
16 
17 */
24 
25  protected $iYear;
26  protected $iMonth;
27  protected $iDay;
28 
35  public function __construct($sDate=null,$sFormat='Y-m-d'){
36  if($sDate!=null){
37  if(!$this->loadFromFormat($sDate,$sFormat)){
38  return false;
39  }
40  }else{
41  $this->reset();
42  }
43  }
49  public function loadFromFormat($sDate,$sFormat){
50  list($sYear,$sMonth,$sDay)=$this->convertFromFormatToTab($sDate,$sFormat);
51  $this->setYear((int)$sYear);
52  $this->setMonth((int)$sMonth);
53  $this->setDay((int)$sDay);
54  if(!$this->isValid() ){
55  $this->reset();
56  return false;
57  }
58 
59  return true;
60  }
64  public function reset(){
65  $this->iYear=null;
66  $this->iMonth=null;
67  $this->iDay=null;
68 
69  $this->iHour=null;
70  $this->iMinute=null;
71  $this->iSecond=null;
72  }
77  public function setYear($iYear){
78  $this->iYear=intval($iYear);
79  }
84  public function setMonth($iMonth){
85  $this->iMonth=intval($iMonth);
86  }
91  public function setDay($iDay){
92  $this->iDay=intval($iDay);
93  }
94 
100  public function getYear($sFormat='YYYY'){
101  if($this->iYear==null){ return null;}
102  if($sFormat=='YY'){
103  return substr($this->iYear,-2);
104  }else if($sFormat=='YYYY'){
105  return $this->iYear;
106  }else{
107  throw new Exception('Wrong format for plugin_date::getYear()');
108  }
109  }
115  public function getMonth($sFormat='MM'){
116  if($this->iMonth==null){ return null;}
117  if($sFormat=='M'){
118  return $this->iMonth;
119  }else if($sFormat=='MM'){
120  return sprintf('%02d',$this->iMonth);
121  }else{
122  throw new Exception('Wrong format for plugin_date::getMonth()');
123  }
124  }
129  public function getStringMonth($sFormat='MM'){
130  if($this->iMonth==null){ return null;}
132  $tLangueDate=_root::getConfigVar('tLangueDate');
133 
134  if($sFormat=='MM'){
135  return $tLangueDate['tMoisLong'][(int)$this->getMonth('M')];
136  }else if($sFormat=='M'){
137  return $tLangueDate['tMoisCourt'][(int)$this->getMonth('M')];
138  }else{
139  throw new Exception('Wrong format for plugin_date::getStringMonth()');
140  }
141  }
146  public function getStringDay($sFormat='DD'){
147  if($this->iMonth==null){ return null;}
149  $tLangueDate=_root::getConfigVar('tLangueDate');
150 
151  if($sFormat=='DD'){
152  return $tLangueDate['tJourLong'][(int)$this->getWeekDay('D')];
153  }else if($sFormat=='D'){
154  return $tLangueDate['tJourCourt'][(int)$this->getWeekDay('D')];
155  }else{
156  throw new Exception('Wrong format for plugin_date::getStringDay()');
157  }
158  }
159 
165  public function getDay($sFormat='DD'){
166  if($this->iDay==null){ return null;}
167  if($sFormat=='D'){
168  return $this->iDay;
169  }else if($sFormat=='DD'){
170  return sprintf('%02d',$this->iDay);
171  }else{
172  throw new Exception('Wrong format for plugin_date::getDay()');
173  }
174  }
179  public function getWeekDay(){
180  return date('w',$this->getMkTime() ) ;
181  }
186  public function getNumberWeek(){
187  return date('W',$this->getMkTime() );
188  }
189 
190 
191 
197  public function addDay($iNb){
198  $newDate=mktime(0,0,0,$this->iMonth,$this->iDay+$iNb,$this->iYear);
199 
200  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
201  }
207  public function addMonth($iNb){
208  $newDate=mktime(0,0,0,$this->iMonth+$iNb,$this->iDay,$this->iYear);
209 
210  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
211  }
217  public function addYear($iNb){
218  $newDate=mktime(0,0,0,$this->iMonth,$this->iDay,$this->iYear+$iNb);
219 
220  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
221  }
222 
228  public function removeDay($iNb){
229  $newDate=mktime(0,0,0,$this->iMonth,$this->iDay-$iNb,$this->iYear);
230 
231  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
232  }
238  public function removeMonth($iNb){
239  $newDate=mktime(0,0,0,$this->iMonth-$iNb,$this->iDay,$this->iYear);
240 
241  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
242  }
248  public function removeYear($iNb){
249  $newDate=mktime(0,0,0,$this->iMonth,$this->iDay,$this->iYear-$iNb);
250 
251  $this->loadFromFormat(date('Y-m-d',$newDate), 'Y-m-d' );
252  }
253 
258  public function getMkTime(){
259  return mktime(0,0,0,$this->iMonth,$this->iDay,$this->iYear);
260  }
261 
267  public function isBank(){
268 
269  $iAnnee=$this->iYear;
270  $iMois=$this->iMonth;
271  $iJour=$this->iDay;
272 
273  $iJourNewDate=$this->getWeekDay();
274 
275  //calcul dimanche de paque et jeudi ascension seulement si mois d'avril ou mai
276  if(intVal($iMois)==3 or intVal($iMois)==4 or intVal($iMois)==5){
277  //calcul Paques
278  $sDimanchePaques=date('d-m-Y',easter_date($iAnnee));
279  $tDimanchePaquesTab=explode('-',$sDimanchePaques);
280 
281  $iJourDimanchePaques=$tDimanchePaquesTab[0];
282  $iMoisDimanchePaques=$tDimanchePaquesTab[1];
283 
284  $sLundiPaques=date('d-m-Y',mktime(0,0,0,$iMoisDimanchePaques,$iJourDimanchePaques+1,$iAnnee));
285  $tLundiPaquesTab=explode('-',$sLundiPaques);
286 
287  $iJourLundiPaques=intVal($tLundiPaquesTab[0]);
288  $iMoisLundiPaques=intVal($tLundiPaquesTab[1]);
289 
290  //calcul jeudi ascension = dimanche paques + 39 jour
291  $sJeudiAscension=date('d-m-Y',mktime(0,0,0,$iMoisDimanchePaques,$iJourDimanchePaques+39,$iAnnee));
292  $tJeudiAscensionTab=explode('-',$sJeudiAscension);
293 
294  $iJourJeudiAsc=intVal($tJeudiAscensionTab[0]);
295  $iMoisJeudiAsc=intVal($tJeudiAscensionTab[1]);
296  }
297 
298  if($iJourNewDate==6 or $iJourNewDate==0){
299  //samedi ou dimanche
300  return true;
301  }
302  else if($iMois==12 and $iJour==25){
303  //noel
304  return true;
305  }
306  else if($iMois==1 and $iJour==1){
307  //jour de l'an
308  return true;
309  }
310  else if($iMois==5 and ($iJour==1 or $iJour==8)){
311  //1er et 8 mai
312  return true;
313  }
314  else if($iMois==11 and ($iJour==1 or $iJour==11)){
315  //1er et 11 novembre
316  return true;
317  }
318  else if($iMois==7 and $iJour==14){
319  //14 juillet
320  return true;
321  }
322  else if($iMois==8 and $iJour==15){
323  //15 aout
324  return true;
325  }
326  else if($iMois==$iMoisLundiPaques and $iJour==$iJourLundiPaques){
327  //Paques
328  return true;
329  }
330  else if($iMois==$iMoisJeudiAsc and $iJour==$iJourJeudiAsc){
331  //Ascension
332  return true;
333  }
334  return false;
335 
336  }
341  public function isValid(){
342 
343  if($this->iYear==null or $this->iMonth==null or $this->iDay==null ){
344  return false;
345  }
346  if( $this->iYear < 1900 ){
347  return false;
348  }
349  if( $this->iMonth < 1 or $this->iMonth > 12 ){
350  return false;
351  }
352  if( $this->iDay < 1 or $this->iDay > 31 ){
353  return false;
354  }
355 
356  return true;
357  }
358 
364  public function toString($sFormat='Y-m-d'){
365  return date($sFormat,$this->getMkTime());
366  }
367 
372  public function isToday(){
373  if($this->getFormat('Y-m-d')==date('Y-m-d')){
374  return true;
375  }
376  return false;
377  }
378 
379 
380  private function convertFromFormatToTab($sDate,$sFormat){
381 
382  if($sFormat=='Y-m-d'){
383  list($iAnnee,$iMois,$iJour)=explode('-',$sDate);
384  }elseif($sFormat=='d-m-Y'){
385  list($iJour,$iMois,$iAnnee)=explode('-',$sDate);
386  }elseif($sFormat=='d/m/Y'){
387  list($iJour,$iMois,$iAnnee)=explode('/',$sDate);
388  }elseif($sFormat=='Y/m/d'){
389  list($iAnnee,$iMois,$iJour)=explode('/',$sDate);
390  }elseif($sFormat=='m-d-Y'){
391  list($iMois,$iJour,$iAnnee)=explode('-',$sDate);
392  }elseif($sFormat=='y-m-d'){
393  list($iAnnee,$iMois,$iJour)=explode('-',$sDate);
394  $iAnnee=2000+intval($iAnnee);
395  }
396 
397  return array(
398  sprintf('%04d',$iAnnee),
399  sprintf('%02d',$iMois),
400  sprintf('%02d',$iJour),
401  );
402 
403  }
404 
405 }
getStringDay($sFormat='DD')
getDay($sFormat='DD')
removeMonth($iNb)
static getConfigVar($sCatAndVar, $uDefaut=null)
Definition: class_root.php:654
removeDay($iNb)
addMonth($iNb)
toString($sFormat='Y-m-d')
loadFromFormat($sDate, $sFormat)
Definition: plugin_date.php:49
static loadLangueDate()
Definition: class_root.php:139
setMonth($iMonth)
Definition: plugin_date.php:84
setYear($iYear)
Definition: plugin_date.php:77
getYear($sFormat='YYYY')
__construct($sDate=null, $sFormat='Y-m-d')
Definition: plugin_date.php:35
getStringMonth($sFormat='MM')
setDay($iDay)
Definition: plugin_date.php:91
removeYear($iNb)
getMonth($sFormat='MM')