1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13: namespace Pry\Date;
14:
15: use Pry\Date\Ferie;
16:
17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27:
28: class Weeks
29: {
30:
31: 32: 33: 34:
35: public $mois;
36:
37: 38: 39: 40:
41: public $jours;
42:
43: 44: 45: 46:
47: private $tabMois;
48:
49: 50: 51: 52:
53: public $ferie;
54:
55: 56: 57: 58:
59: private $annee;
60:
61: 62: 63: 64:
65: private $semaine;
66:
67: 68: 69: 70:
71: private $daysOfWeek;
72:
73: 74: 75: 76: 77:
78: public function __construct($semaine, $annee)
79: {
80: $this->jours = array('lundi', 'mardi', 'mercredi', 'jeudi', 'vendredi', 'samedi', 'dimanche');
81: $this->tabMois = array('Janvier', 'Février', 'Mars', 'Avril', 'Mai', 'Juin', 'Juillet', 'Aout', 'Septembre', 'Octobre', 'Novembre', 'Décembre');
82: $this->ferie = new Ferie($annee);
83: $this->semaine = intval($semaine);
84: $this->annee = intval($annee);
85: }
86:
87: 88: 89: 90: 91:
92: public function getBoundaries()
93: {
94: $dt = new \DateTime($this->annee . 'W' . sprintf('%02d', $this->semaine));
95:
96: $lundi = (int) $dt->format('U');
97: $dt->modify('Sunday');
98: $dimanche = (int) $dt->format('U');
99:
100: $this->daysOfWeek[0] = $lundi;
101: $this->mois = $this->tabMois[date("m", $this->daysOfWeek[0]) - 1];
102:
103: return array('lundi' => $lundi, 'dimanche' => $dimanche);
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function computeDays()
113: {
114: if (empty($this->daysOfWeek))
115: throw new \BadFunctionCallException('Use getBoundaries() first');
116: for ($i = 1; $i < 7; $i++)
117: $this->daysOfWeek[$i] = $this->daysOfWeek[$i - 1] + 86400;
118:
119: return $this->daysOfWeek;
120: }
121:
122: 123: 124: 125: 126:
127: public function computeNextPrev()
128: {
129: if ($this->semaine == 1)
130: {
131: $anneePrec = $this->annee - 1;
132: $semainePrec = date("W", $this->daysOfWeek[0] - 86400);
133:
134: $anneeSuiv = $this->annee;
135: $semaineSuiv = 2;
136: }
137: else if ($this->semaine == 52)
138: {
139: $anneePrec = $this->annee;
140: $semainePrec = 51;
141:
142: if (date("W", $this->daysOfWeek[6] + 86400) == 53)
143: {
144: $anneeSuiv = $this->annee;
145: $semaineSuiv = 53;
146: }
147: else
148: {
149: $anneeSuiv = $this->annee + 1;
150: $semaineSuiv = 1;
151: }
152: }
153: elseif ($this->semaine == 53)
154: {
155: $anneePrec = $this->annee;
156: $semainePrec = 52;
157: $anneeSuiv = $this->annee + 1;
158: $semaineSuiv = 1;
159: }
160: else
161: {
162: $anneePrec = $anneeSuiv = $this->annee;
163: $semainePrec = $this->semaine - 1;
164: $semaineSuiv = $this->semaine + 1;
165: }
166:
167: return array('annee' => array('next' => $anneeSuiv, 'prev' => $anneePrec),
168: 'semaine' => array('next' => $semaineSuiv, 'prev' => $semainePrec));
169: }
170:
171: 172: 173: 174: 175:
176: public function getMysqlDays()
177: {
178: $total = count($this->daysOfWeek);
179: $mysqlDays = array();
180:
181: for ($i = 0; $i < $total; $i++)
182: $mysqlDays[$i] = date("Y-m-d", $this->daysOfWeek[$i]);
183:
184: return $mysqlDays;
185: }
186:
187: }
188:
189: ?>