KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
date.php
Ir a la documentación de este archivo.
1 <?php
27 class Date
28 {
29 
35  private $date;
41  private $day;
47  private $year;
53  private $month;
59  private $timestamp;
60 
65  public function __construct($date = "")
66  {
67  if ($date) {
68  $date_parts = explode("-", $date);
69  $this->year = (int) $date_parts[0];
70  $this->month = (int) $date_parts[1];
71  $this->day = (int) $date_parts[2];
72  $this->date = $date;
73  $this->timestamp = mktime(0, 0, 0, $this->month, $this->day, $this->year);
74  } else {
75  $this->year = date("Y");
76  $this->month = date("m");
77  $this->day = date("d");
78  $this->timestamp = time();
79  $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
80  }
81  }
82 
88  public function getMonthName()
89  {
90  return ucfirst(strftime("%B", $this->timestamp));
91  }
92 
98  public function getAbrevMonthName()
99  {
100  return ucfirst(strftime("%b", $this->timestamp));
101  }
102 
108  public function getDay()
109  {
110  return $this->day;
111  }
112 
118  public function getMonth()
119  {
120  return $this->month;
121  }
122 
128  public function getYear()
129  {
130  return $this->year;
131  }
132 
137  public function getTimestamp()
138  {
139  return $this->timestamp;
140  }
141 
147  public function __toString()
148  {
149  return $this->date;
150  }
151 
156  public function addMonths($month)
157  {
158  if ($this->month + $month > 12) {
159  $this->month = ($month % 12) + 1;
160  $this->year += ( (int) ($month / 12));
161  } else {
162  $this->month++;
163  }
164  $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
165  $this->consolideDate();
166  return $this->date;
167  }
168 
173  public function diffMonths($month)
174  {
175  if ($this->month - $month < 1) {
176  $this->month = 12 - (($month % 12) + 1);
177  $this->year -= ( (int) ($month / 12));
178  } else {
179  $this->month--;
180  }
181  $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
182  $this->consolideDate();
183  return $this->date;
184  }
185 
192  public function addDays($days)
193  {
194  $this->date = date("Y-m-d", $this->timestamp + $days * 86400);
195  $this->consolideDate();
196  return $this->date;
197  }
198 
205  public function diffDays($days)
206  {
207  $this->date = date("Y-m-d", $this->timestamp - $days * 86400);
208  $this->consolideDate();
209  return $this->date;
210  }
211 
218  public function addYears($years)
219  {
220  $this->year += $years;
221  $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
222  $this->consolideDate();
223  return $this->date;
224  }
225 
232  public function diffYears($years)
233  {
234  $this->year -= $years;
235  $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
236  $this->consolideDate();
237  return $this->date;
238  }
239 
245  public function getUsingFormat($format)
246  {
247  $datetime = new DateTime($this->date);
248  return $datetime->format($format);
249  }
250 
256  public function getDayOfWeek()
257  {
258  $datetime = new DateTime($this->date);
259  return $datetime->format("l");
260  }
261 
266  public function diffDate($date)
267  {
268  $date_parts = explode("-", $date);
269  $year = (int) $date_parts[0];
270  $month = (int) $date_parts[1];
271  $day = (int) $date_parts[2];
272  $timestamp = mktime(0, 0, 0, $month, $day, $year);
273  return (int) (($this->timestamp - $timestamp) / 86400);
274  }
275 
281  public function isToday()
282  {
283  if ($this->date == date("Y-m-d")) {
284  return true;
285  } else {
286  return false;
287  }
288  }
289 
295  public function isYesterday()
296  {
297  $time = mktime(0, 0, 0, date("m"), date("d"), date("Y")) - 86400;
298 
299  if ($this->timestamp == $time) {
300  return true;
301  } else {
302  return false;
303  }
304  }
305 
311  public function isTomorrow()
312  {
313  $time = mktime(0, 0, 0, date("m"), date("d"), date("Y")) - 86400;
314 
315  if ($this->timestamp == $time) {
316  return true;
317  } else {
318  return false;
319  }
320  }
321 
326  private function consolideDate()
327  {
328  $date_parts = explode("-", $this->date);
329  $this->year = (int) $date_parts[0];
330  $this->month = (int) $date_parts[1];
331  $this->day = (int) $date_parts[2];
332  $this->timestamp = mktime(0, 0, 0, $this->month, $this->day, $this->year);
333  }
334 
335 }