mirror of
https://github.com/openvk/openvk
synced 2025-03-14 21:45:22 +03:00
* feat: add phpstan for static analysis * ci(actions): add phpstan action * ci(actions): do analysing inside docker container * fix(FetchToncoinTransactions): add var declaration * fix(ServiceAPI/Wall): add var declaration * fix(bootstrap): remove case-insensitive false vars * fix(VKAPI/Handlers/Board): change parameters order * fix(VKAPIRequestHandler): set fail's return type as never * fix(VKAPI/Handlers/Groups): add array declaration * fix(VKAPI/Handlers/Newsfeed): add return_banned declaration * fix(VKAPI/Handlers/Notes): move $nodez declaration up * fix(phpstan): most of the things and stupid lines of code * fix(lint) * fix(phpstan): less errors * fix(lint): again. cuz i forgot about it * fix(stan): all errors are gone now =3 --------- Co-authored-by: veselcraft <veselcraft@icloud.com>
96 lines
3.2 KiB
PHP
96 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Util;
|
|
|
|
use Chandler\Session\Session;
|
|
|
|
class DateTime
|
|
{
|
|
public const RELATIVE_FORMAT_NORMAL = 0;
|
|
public const RELATIVE_FORMAT_LOWER = 1;
|
|
public const RELATIVE_FORMAT_SHORT = 2;
|
|
|
|
private $timestamp;
|
|
private $localizator;
|
|
|
|
public function __construct(?int $timestamp = null)
|
|
{
|
|
$this->timestamp = $timestamp ?? time();
|
|
$this->localizator = Localizator::i();
|
|
}
|
|
|
|
protected function zmdate(): string
|
|
{
|
|
$then = date_create("@" . $this->timestamp);
|
|
$now = date_create();
|
|
$diff = date_diff($now, $then);
|
|
|
|
$sessionOffset = intval(Session::i()->get("_timezoneOffset"));
|
|
if ($diff->invert === 0) {
|
|
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
|
|
}
|
|
|
|
if (($this->timestamp + $sessionOffset) >= (strtotime("midnight") + $sessionOffset)) { # Today
|
|
if ($diff->h >= 1) {
|
|
return tr("time_today") . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
|
|
} elseif ($diff->i < 2) {
|
|
return tr("time_just_now");
|
|
} else {
|
|
return $diff->i === 5 ? tr("time_exactly_five_minutes_ago") : tr("time_minutes_ago", $diff->i);
|
|
}
|
|
} elseif (($this->timestamp + $sessionOffset) >= (strtotime("-1day midnight") + $sessionOffset)) { # Yesterday
|
|
return tr("time_yesterday") . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
|
|
} elseif (ovk_strftime_safe("%Y", $this->timestamp) === ovk_strftime_safe("%Y", time())) { # In this year
|
|
return ovk_strftime_safe("%e %h ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
|
|
} else {
|
|
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
|
|
}
|
|
}
|
|
|
|
public function format(string $format, bool $useDate = false): string
|
|
{
|
|
if (!$useDate) {
|
|
return ovk_strftime_safe($format, $this->timestamp);
|
|
} else {
|
|
return date($format, $this->timestamp);
|
|
}
|
|
}
|
|
|
|
public function relative(int $type = 0): string
|
|
{
|
|
switch ($type) {
|
|
case static::RELATIVE_FORMAT_NORMAL:
|
|
return mb_convert_case($this->zmdate(), MB_CASE_TITLE_SIMPLE);
|
|
case static::RELATIVE_FORMAT_LOWER:
|
|
return $this->zmdate();
|
|
case static::RELATIVE_FORMAT_SHORT:
|
|
default:
|
|
return "";
|
|
}
|
|
}
|
|
|
|
public function html(bool $capitalize = false, bool $short = false): string
|
|
{
|
|
if ($short) {
|
|
$dt = $this->relative(static::RELATIVE_FORMAT_SHORT);
|
|
} elseif ($capitalize) {
|
|
$dt = $this->relative(static::RELATIVE_FORMAT_NORMAL);
|
|
} else {
|
|
$dt = $this->relative(static::RELATIVE_FORMAT_LOWER);
|
|
}
|
|
|
|
return "<time>$dt</time>";
|
|
}
|
|
|
|
public function timestamp(): int
|
|
{
|
|
return $this->timestamp;
|
|
}
|
|
|
|
public function __toString(): string
|
|
{
|
|
return $this->relative(static::RELATIVE_FORMAT_LOWER);
|
|
}
|
|
}
|