|
|
@ -5,12 +5,13 @@ namespace Wruczek\TSWebsite\Utils;
|
|
|
|
use Wruczek\TSWebsite\Utils\Language\LanguageUtils;
|
|
|
|
use Wruczek\TSWebsite\Utils\Language\LanguageUtils;
|
|
|
|
|
|
|
|
|
|
|
|
class DateUtils {
|
|
|
|
class DateUtils {
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Returns current date format based on current user language. If it cannot
|
|
|
|
* Returns current date format based on current user language. If it cannot
|
|
|
|
* be retrieved, default value is returned
|
|
|
|
* be retrieved, default value is returned
|
|
|
|
* @return string date format
|
|
|
|
* @return string date format
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function getDateFormat() {
|
|
|
|
public static function getDateFormat() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return LanguageUtils::i()->translate("DATE_FORMAT");
|
|
|
|
return LanguageUtils::i()->translate("DATE_FORMAT");
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
} catch (\Exception $e) {
|
|
|
@ -23,7 +24,7 @@ class DateUtils {
|
|
|
|
* be retrieved, default value is returned
|
|
|
|
* be retrieved, default value is returned
|
|
|
|
* @return string time format
|
|
|
|
* @return string time format
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function getTimeFormat() {
|
|
|
|
public static function getTimeFormat() {
|
|
|
|
try {
|
|
|
|
try {
|
|
|
|
return LanguageUtils::i()->translate("TIME_FORMAT");
|
|
|
|
return LanguageUtils::i()->translate("TIME_FORMAT");
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
} catch (\Exception $e) {
|
|
|
@ -36,8 +37,8 @@ class DateUtils {
|
|
|
|
* @param $timestamp
|
|
|
|
* @param $timestamp
|
|
|
|
* @return false|string
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function formatToDate($timestamp) {
|
|
|
|
public static function formatDate($timestamp) {
|
|
|
|
return date($this->getDateFormat(), $timestamp);
|
|
|
|
return date(self::getDateFormat(), $timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -45,8 +46,8 @@ class DateUtils {
|
|
|
|
* @param $timestamp
|
|
|
|
* @param $timestamp
|
|
|
|
* @return false|string
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function formatToTime($timestamp) {
|
|
|
|
public static function foramtTime($timestamp) {
|
|
|
|
return date($this->getTimeFormat(), $timestamp);
|
|
|
|
return date(self::getTimeFormat(), $timestamp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
@ -55,63 +56,8 @@ class DateUtils {
|
|
|
|
* @param string $additional additional date format
|
|
|
|
* @param string $additional additional date format
|
|
|
|
* @return false|string
|
|
|
|
* @return false|string
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
public function formatToDateTime($timestamp, $additional = "") {
|
|
|
|
public static function formatDatetime($timestamp, $additional = "") {
|
|
|
|
return date("{$this->getDateFormat()} {$this->getTimeFormat()} $additional", $timestamp);
|
|
|
|
return date(trim(self::getDateFormat() . ", " . self::getTimeFormat() . " " . $additional), $timestamp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Formats timestamp into "time ago" string
|
|
|
|
|
|
|
|
* For example, timestamp set to 60 seconds ago will return "1 minute ago"
|
|
|
|
|
|
|
|
*
|
|
|
|
|
|
|
|
* Taken from StackOverflow: https://stackoverflow.com/a/18602474
|
|
|
|
|
|
|
|
* @param $timestamp int timestamp with past date
|
|
|
|
|
|
|
|
* @param bool $full if true, full date will be returned. For example "5 hours, 2 minutes, 8 seconds"
|
|
|
|
|
|
|
|
* @return string timestamp formatted to fuzzy date. Marf.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function fuzzyDate($timestamp, $full = false) {
|
|
|
|
|
|
|
|
$now = new \DateTime;
|
|
|
|
|
|
|
|
$ago = (new \DateTime)->setTimestamp($timestamp);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$diff = $now->diff($ago);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$diff->w = floor($diff->d / 7);
|
|
|
|
|
|
|
|
$diff->d -= $diff->w * 7;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$string = [
|
|
|
|
|
|
|
|
'y' => 'year',
|
|
|
|
|
|
|
|
'm' => 'month',
|
|
|
|
|
|
|
|
'w' => 'week',
|
|
|
|
|
|
|
|
'd' => 'day',
|
|
|
|
|
|
|
|
'h' => 'hour',
|
|
|
|
|
|
|
|
'i' => 'minute',
|
|
|
|
|
|
|
|
's' => 'second'
|
|
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
foreach ($string as $k => &$v) {
|
|
|
|
|
|
|
|
if ($diff->$k) {
|
|
|
|
|
|
|
|
$v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
unset($string[$k]);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!$full) $string = array_slice($string, 0, 1);
|
|
|
|
|
|
|
|
return $string ? implode(', ', $string) . ' ago' : 'just now';
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* Returns fuzzy date with abbreviation showing precise date
|
|
|
|
|
|
|
|
* @see fuzzyDate
|
|
|
|
|
|
|
|
* @param $timestamp
|
|
|
|
|
|
|
|
* @param bool $full
|
|
|
|
|
|
|
|
* @return string
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function fuzzyDateHTML($timestamp, $full = false) {
|
|
|
|
|
|
|
|
$fuzzyDate = $this->fuzzyDate($timestamp, $full);
|
|
|
|
|
|
|
|
$fullDate = $this->formatToDateTime($timestamp, "T");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return '<abbr data-fuzzydate="' . $timestamp . '"></abbr>';
|
|
|
|
|
|
|
|
// return '<abbr data-toggle="tooltip" title="' . htmlentities($fullDate) . '">' . htmlentities($fuzzyDate) . '</abbr>';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|