00001 <?php
00022 class Date
00023 {
00029 private $date;
00035 private $day;
00041 private $year;
00047 private $month;
00053 private $timestamp;
00058 public function __construct ($date = "")
00059 {
00060 if ($date) {
00061 $date_parts = explode("-", $date);
00062 $this->year = (int) $date_parts[0];
00063 $this->month = (int) $date_parts[1];
00064 $this->day = (int) $date_parts[2];
00065 $this->date = $date;
00066 $this->timestamp = mktime(0, 0, 0, $this->month, $this->day, $this->year);
00067 } else {
00068 $this->year = date("Y");
00069 $this->month = date("m");
00070 $this->day = date("d");
00071 $this->timestamp = time();
00072 $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
00073 }
00074 }
00080 public function getMonthName ()
00081 {
00082 return ucfirst(strftime("%B", $this->timestamp));
00083 }
00089 public function getAbrevMonthName ()
00090 {
00091 return ucfirst(strftime("%b", $this->timestamp));
00092 }
00098 public function getDay ()
00099 {
00100 return $this->day;
00101 }
00107 public function getMonth ()
00108 {
00109 return $this->month;
00110 }
00116 public function getYear ()
00117 {
00118 return $this->year;
00119 }
00124 public function getTimestamp ()
00125 {
00126 return $this->timestamp;
00127 }
00133 public function __toString ()
00134 {
00135 return $this->date;
00136 }
00141 public function addMonths ($month)
00142 {
00143 if ($this->month + $month > 12) {
00144 $this->month = ($month % 12) + 1;
00145 $this->year += ((int) ($month / 12));
00146 } else {
00147 $this->month ++;
00148 }
00149 $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
00150 $this->consolideDate();
00151 return $this->date;
00152 }
00157 public function diffMonths ($month)
00158 {
00159 if ($this->month - $month < 1) {
00160 $this->month = 12 - (($month % 12) + 1);
00161 $this->year -= ((int) ($month / 12));
00162 } else {
00163 $this->month --;
00164 }
00165 $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
00166 $this->consolideDate();
00167 return $this->date;
00168 }
00175 public function addDays ($days)
00176 {
00177 $this->date = date("Y-m-d", $this->timestamp + $days * 86400);
00178 $this->consolideDate();
00179 return $this->date;
00180 }
00187 public function diffDays ($days)
00188 {
00189 $this->date = date("Y-m-d", $this->timestamp - $days * 86400);
00190 $this->consolideDate();
00191 return $this->date;
00192 }
00199 public function addYears ($years)
00200 {
00201 $this->year += $years;
00202 $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
00203 $this->consolideDate();
00204 return $this->date;
00205 }
00212 public function diffYears ($years)
00213 {
00214 $this->year -= $years;
00215 $this->date = $this->year . "-" . sprintf("%02s", $this->month) . "-" . sprintf("%02s", $this->day);
00216 $this->consolideDate();
00217 return $this->date;
00218 }
00224 public function getUsingFormat ($format)
00225 {
00226 $datetime = new DateTime($this->date);
00227 return $datetime->format($format);
00228 }
00234 public function getDayOfWeek ()
00235 {
00236 $datetime = new DateTime($this->date);
00237 return $datetime->format("l");
00238 }
00243 public function diffDate ($date)
00244 {
00245 $date_parts = explode("-", $date);
00246 $year = (int) $date_parts[0];
00247 $month = (int) $date_parts[1];
00248 $day = (int) $date_parts[2];
00249 $timestamp = mktime(0, 0, 0, $month, $day, $year);
00250 return (int) (($this->timestamp - $timestamp) / 86400);
00251 }
00257 public function isToday ()
00258 {
00259 if ($this->date == date("Y-m-d")) {
00260 return true;
00261 } else {
00262 return false;
00263 }
00264 }
00270 public function isYesterday ()
00271 {
00272 if (! isset($this->yesterday)) {
00273 $time = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
00274 $this->yesterday = $time - 86400;
00275 }
00276 if ($this->timestamp == $this->yesterday) {
00277 return true;
00278 } else {
00279 return false;
00280 }
00281 }
00287 public function isTomorrow ()
00288 {
00289 if (! isset($this->tomorrow)) {
00290 $time = mktime(0, 0, 0, date("m"), date("d"), date("Y"));
00291 $this->tomorrow = $time - 86400;
00292 }
00293 if ($this->timestamp == $this->tomorrow) {
00294 return true;
00295 } else {
00296 return false;
00297 }
00298 }
00303 private function consolideDate ()
00304 {
00305 $date_parts = explode("-", $this->date);
00306 $this->year = (int) $date_parts[0];
00307 $this->month = (int) $date_parts[1];
00308 $this->day = (int) $date_parts[2];
00309 $this->timestamp = mktime(0, 0, 0, $this->month, $this->day, $this->year);
00310 }
00311 }