1 | <?php |
---|
2 | |
---|
3 | require_once 'Jour.php' ; |
---|
4 | |
---|
5 | class MoisException extends Exception |
---|
6 | {} |
---|
7 | |
---|
8 | class Mois |
---|
9 | { |
---|
10 | protected $_annee = 0 ; |
---|
11 | protected $_mois = 0 ; |
---|
12 | protected $_ts = 0 ; |
---|
13 | protected $_premierJour = 0 ; |
---|
14 | protected $_nbJours = 0 ; |
---|
15 | protected $_jours = array() ; |
---|
16 | public static $minJour = 7 ; |
---|
17 | public static $maxJour = 0 ; |
---|
18 | |
---|
19 | public static $moisFr = array(1 => 'Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Août', 'Septembre', 'Octobre', 'Novembre', 'Décembre') ; |
---|
20 | |
---|
21 | public function __construct($mois, $annee) |
---|
22 | { |
---|
23 | $this -> _annee = (int) $annee ; |
---|
24 | $this -> _mois = (int) $mois ; |
---|
25 | |
---|
26 | $this -> _init() ; |
---|
27 | } |
---|
28 | |
---|
29 | protected function _init() |
---|
30 | { |
---|
31 | if( $this -> _annee > 0 && $this -> _mois > 0 && $this -> _mois <= 12 ) |
---|
32 | { |
---|
33 | $this -> _ts = mktime(0, 0, 0, $this -> _mois, 1, $this -> _annee) ; |
---|
34 | $this -> _premierJour = (int) date('N', $this -> _ts) ; |
---|
35 | $this -> _nbJours = (int) date('t', $this -> _ts) ; |
---|
36 | |
---|
37 | self::$minJour = self::$minJour < $this -> _premierJour ? self::$minJour : $this -> _premierJour ; |
---|
38 | self::$maxJour = self::$maxJour > $this -> _premierJour + $this -> _nbJours ? self::$maxJour : $this -> _premierJour + $this -> _nbJours ; |
---|
39 | for( $i = 1 ; $i <= $this -> _nbJours ; $i++ ) |
---|
40 | { |
---|
41 | $this -> _jours[] = new Jour($i, $this -> _mois, $this -> _annee) ; |
---|
42 | } |
---|
43 | } else throw new MoisException('Le format de l\'année / du mois n\'est pas valide.') ; |
---|
44 | } |
---|
45 | |
---|
46 | public function jour($num) |
---|
47 | { |
---|
48 | if( |
---|
49 | date('n', strtotime('+' . $num - $this -> _premierJour . ' days', $this -> _ts)) === date('n', $this -> _ts) |
---|
50 | && $num >= $this -> _premierJour |
---|
51 | ) |
---|
52 | return $this -> _jours[$num - $this -> _premierJour] ; |
---|
53 | else return null ; |
---|
54 | } |
---|
55 | |
---|
56 | public function __toString() |
---|
57 | { |
---|
58 | return self::$moisFr[date('n', $this -> _ts)] ; |
---|
59 | } |
---|
60 | } |
---|