00001 <?php
00002
00003
00004
00013 class DateParser
00014 {
00015 protected $year = "";
00016 protected $month = "";
00017 protected $day = "";
00018 protected $hour = 0;
00019 protected $minute = 0;
00020 protected $timestamp = "";
00021 protected $mysqlFormatDate = "";
00022 protected $mysqlFormatTime = "";
00023
00027 function __construct($date, $time="") {
00028 $this->mysqlFormatDate=$date;
00029 $this->mysqlFormatTime=$time;
00030 $this->year = strtok($date, "-");
00031 $this->month = strtok("-");
00032 $this->day = strtok("-");
00033 if($time != '') {
00034 $this->hour = strtok($time, ":");
00035 $this->minute = strtok(":");
00036 }
00037 $this->timestamp = mktime($this->hour,$this->minute,0,$this->month,$this->day,$this->year);
00038 }
00039
00050 public static function quickFormatDate($date, $formatString, $addDays='') {
00051 if ($date == "0000-00-00" || $date == "")
00052 return "";
00053 $year = strtok($date, "-");
00054 $month = strtok("-");
00055 $day = strtok(' ');
00056 if($addDays != '') {
00057 $day += $addDays;
00058 }
00059 $timestamp = mktime(0,0,0,$month,$day,$year);
00060
00061 return date($formatString, $timestamp);
00062 }
00063
00069 public static function currentDateMysql() {
00070 date('Y-m-d H:i:s');
00071 }
00072
00073
00083 public static function quickFormatTime($time, $forCalender=false) {
00084 $hour = strtok($time, ":");
00085 $minute = strtok(":");
00086 if ($forCalender) {
00087 return ($hour.$minute.'00');
00088 } else {
00089 $meridiem = ($hour > 11)?'pm':'am';
00090 $hour = ($hour == 0)?24:$hour;
00091 $hour = ($hour > 12)?$hour-12:$hour;
00092 return (ltrim($hour, '0').':'.$minute.' '.$meridiem);
00093 }
00094 }
00095
00101 public static function formatTimeAgo($timestamp) {
00102 $submissionTime = (int)((time() - strtotime($timestamp))/(60*60*24));
00103 $timeAgo = "";
00104
00105 if ($submissionTime == 1)
00106 $timeAgo = "Yesterday";
00107 else if ($submissionTime == 0)
00108 $timeAgo = "Today";
00109 else if ($submissionTime > 31) {
00110 $months = (int)($submissionTime/31);
00111
00112 if ($months > 11) {
00113 return self::quickFormatDate($timestamp, "F jS, Y");
00114 }
00115 $days = $submissionTime%31;
00116 if ($months == 1) $monthFormat = "month";
00117 else $monthFormat = "months";
00118 if ($days == 1) $dayFormat = "day";
00119 else if ($days == 0) $dayFormat = "";
00120 else $dayFormat = "days";
00121
00122 $timeAgo = $months . " " . $monthFormat . " " . $days . " " . $dayFormat . " ago";
00123 } else
00124 $timeAgo = $submissionTime . " days ago";
00125
00126 return ($timeAgo);
00127 }
00128
00129
00133 public function formatDate($formatString) {
00134 return date($formatString, $this->timestamp);
00135 }
00136
00140 public function formatTime($forCalendar=false, $time='') {
00141 if($forCalendar) {
00142 return ($this->hour.$this->minute.'00');
00143 } else {
00144 $meridiem = ($this->hour > 11)?'pm':'am';
00145 $hour12 = ($this->hour == 0)?24:$this->hour;
00146 $hour12 = ($hour12 > 12)?$hour12-12:$hour12;
00147 return (ltrim($hour12, '0').':'.$this->minute.' '.$meridiem);
00148 }
00149 }
00150
00154 public function getYear() {
00155 return $this->year;
00156 }
00157
00161 public function getMonth() {
00162 return $this->month;
00163 }
00164
00168 public function getDay() {
00169 return $this->day;
00170 }
00171
00175 public function getHour() {
00176 return $this->hour;
00177 }
00178
00182 public function getMinute() {
00183 return $this->minute;
00184 }
00185
00189 public function getTimestamp() {
00190 return $this->timestamp;
00191 }
00192
00196 public function getMysqlFormatDate() {
00197 return $this->mysqlFormatDate;
00198 }
00199
00203 public function getMysqlFormatTime() {
00204 return $this->mysqlFormatTime;
00205 }
00206 }
00207
00208 ?>