<?php
class DatePretty {
public $second = -1;
public $minute = -1;
public $hour = -1;
public $day = -1;
public $week = -1;
public $diffsecond = -1;
public function diffPrettyBetweenDates($lastDate, $firstDate) {
if (!isset($lastDate) || $lastDate == null && $lastDate == '') {
$lastDate = date("Y-m-d H:i:s");
}
if (!isset($firstDate)) {
$firstDate = date("Y-m-d H:i:s");
}
$dt = $this->_parseDate($lastDate);
$last = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
$dt = $this->_parseDate($firstDate);
$first = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
$time = $last - $first;
if ($time < 0) {
$time = 0;
}
$this->_setDatePrettyElements($time);
}
public function diffPrettyTime($time) {
$this->_setDatePrettyElements($time);
}
public function toString() {
$response = "";
if ($this->week > 0) {
$response .= $this->week . " hafta ";
}
if ($this->day > 0) {
$response .= $this->day . " gün ";
}
if ($this->hour > 0) {
$response .= $this->hour . " saat ";
}
if ($this->minute > 0) {
$response .= $this->minute . " dakika ";
}
if ($this->second > -1) {
$response .= $this->second . " saniye ";
}
return $response;
}
private function _setDatePrettyElements($time) {
$this->diffsecond = $time;
$this->_resetValues();
$diff = $time;
$day_diff = floor($diff / 86400);
if ($day_diff == 0) {
if ($diff < 60) {
$this->second = $diff;
} else if ($diff < 3600) {
$m = floor($diff / 60);
$s = $diff - ($m * 60);
$this->second = $s;
$this->minute = $m;
} else if ($diff < 86400) {
$h = floor($diff / 3600);
$m = floor(($diff - ($h * 3600)) / 60);
$s = $diff - ($h * 3600) - ($m * 60);
$this->second = $s;
$this->minute = $m;
$this->hour = $h;
}
} else if ($day_diff < 7) {
$diff = $diff - $day_diff * 86400;
$h = floor($diff / 3600);
$m = floor(($diff - ($h * 3600)) / 60);
$s = $diff - ($h * 3600) - ($m * 60);
$this->second = $s;
$this->minute = $m;
$this->hour = $h;
$this->day = $day_diff;
} else {
$w = floor($day_diff / 7);
$day_diff = $day_diff - $w * 7;
$diff = $diff - ($w * 7 + $day_diff) * 86400;
$h = floor($diff / 3600);
$m = floor(($diff - ($h * 3600)) / 60);
$s = $diff - ($h * 3600) - ($m * 60);
$this->second = $s;
$this->minute = $m;
$this->hour = $h;
$this->day = $day_diff;
$this->week = ceil(($w * 7 + $day_diff) / 7);
}
}
private function _parseDate($d) {
$t = explode(" ", $d);
$t1 = explode("-", $t[0]);
$t2 = explode(":", $t[1]);
return array_merge($t1, $t2);
}
private function _resetValues() {
$this->second = -1;
$this->minute = -1;
$this->hour = -1;
$this->day = -1;
$this->week = -1;
}
}