openvk/Web/Util/DateTime.php

96 lines
3.2 KiB
PHP
Raw Normal View History

<?php
declare(strict_types=1);
2020-06-07 19:04:43 +03:00
namespace openvk\Web\Util;
2024-12-13 16:23:33 +03:00
use Chandler\Session\Session;
2020-06-07 19:04:43 +03:00
class DateTime
{
public const RELATIVE_FORMAT_NORMAL = 0;
public const RELATIVE_FORMAT_LOWER = 1;
public const RELATIVE_FORMAT_SHORT = 2;
2020-06-07 19:04:43 +03:00
private $timestamp;
private $localizator;
public function __construct(?int $timestamp = null)
2020-06-07 19:04:43 +03:00
{
$this->timestamp = $timestamp ?? time();
$this->localizator = Localizator::i();
}
2020-06-07 19:04:43 +03:00
protected function zmdate(): string
{
$then = date_create("@" . $this->timestamp);
$now = date_create();
$diff = date_diff($now, $then);
2024-12-13 16:23:33 +03:00
$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) {
2020-06-07 19:04:43 +03:00
return tr("time_just_now");
} else {
2020-06-07 19:04:43 +03:00
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);
2020-06-07 19:04:43 +03:00
} else {
return ovk_strftime_safe("%e %B %Y ", $this->timestamp) . tr("time_at_sp") . ovk_strftime_safe(" %R", $this->timestamp);
2020-06-07 19:04:43 +03:00
}
}
public function format(string $format, bool $useDate = false): string
2020-06-07 19:04:43 +03:00
{
if (!$useDate) {
2020-09-29 22:59:59 +03:00
return ovk_strftime_safe($format, $this->timestamp);
} else {
2020-06-07 19:04:43 +03:00
return date($format, $this->timestamp);
}
2020-06-07 19:04:43 +03:00
}
public function relative(int $type = 0): string
2020-06-07 19:04:43 +03:00
{
switch ($type) {
2020-06-07 19:04:43 +03:00
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:
return "";
}
}
public function html(bool $capitalize = false, bool $short = false): string
2020-06-07 19:04:43 +03:00
{
if ($short) {
2020-06-07 19:04:43 +03:00
$dt = $this->relative(static::RELATIVE_FORMAT_SHORT);
} elseif ($capitalize) {
2020-06-07 19:04:43 +03:00
$dt = $this->relative(static::RELATIVE_FORMAT_NORMAL);
} else {
2020-06-07 19:04:43 +03:00
$dt = $this->relative(static::RELATIVE_FORMAT_LOWER);
}
2020-06-07 19:04:43 +03:00
return "<time>$dt</time>";
}
public function timestamp(): int
2020-06-07 19:04:43 +03:00
{
return $this->timestamp;
}
public function __toString(): string
2020-06-07 19:04:43 +03:00
{
return $this->relative(static::RELATIVE_FORMAT_LOWER);
}
}