-
-
Save Telegramsrv/0962ac1b2fd60a9aab9dbce5806a7c34 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
declare(strict_types=1); | |
class TimeUtils{ | |
public const YEAR = self::DAY * 365; | |
public const MONTH = self::DAY * 30; | |
public const WEEK = self::DAY * 7; | |
public const DAY = self::HOUR * 24; | |
public const HOUR = self::MIN * 60; | |
public const MIN = self::SEC * 60; | |
public const SEC = 1; | |
/** | |
* @param int $time | |
* @param bool $accusative (винительный падеж, используйте перед словом "через") | |
* @return string | |
*/ | |
public static function timestamp2word(int $time, bool $accusative = true) : string{ | |
$result = ""; | |
if(($year = (int) ($time / self::YEAR)) > 0){ | |
$time -= $year * self::YEAR; | |
$result .= self::rightTimeCase($year, self::YEAR, $accusative); | |
} | |
if(($month = (int) ($time / self::MONTH)) > 0){ | |
$time -= $month * self::MONTH; | |
$result .= self::rightTimeCase($month, self::MONTH, $accusative); | |
} | |
if(($week = (int) ($time / self::WEEK)) > 0){ | |
$time -= $week * self::WEEK; | |
$result .= self::rightTimeCase($week, self::WEEK, $accusative); | |
} | |
if(($day = (int) ($time / self::DAY)) > 0){ | |
$time -= $day * self::DAY; | |
$result .= self::rightTimeCase($day, self::DAY, $accusative); | |
} | |
if(($hour = (int) ($time / self::HOUR)) > 0){ | |
$time -= $hour * self::HOUR; | |
$result .= self::rightTimeCase($hour, self::HOUR, $accusative); | |
} | |
if(($min = (int) ($time / self::MIN)) > 0){ | |
$time -= $min * self::MIN; | |
$result .= self::rightTimeCase($min, self::MIN, $accusative); | |
} | |
if($time > 0){ | |
$min < 0 ?: $result .= "и "; | |
$result .= self::rightTimeCase($time, self::SEC, $accusative); | |
} | |
return $result; | |
} | |
/** | |
* @param int $number | |
* @param array $format [1, 2, 5] | |
* @return string | |
*/ | |
public static function correctForm(int $number, array $format) : string{ | |
return $number . " " . $format[($number % 100 > 4 && $number % 100 < 20) ? 2 : [2, 0, 1, 1, 1, 2][min($number % 10, 5)]]; | |
} | |
/** | |
* @param int $time | |
* @param int $type | |
* @param bool $accusative | |
* @return string | |
*/ | |
public static function rightTimeCase(int $time, int $type, bool $accusative = true) : string{ | |
$cases = [ | |
self::YEAR => ["год", "года", "лет"], | |
self::MONTH => ["месяц", "месяца", "месяцев"], | |
self::WEEK => [$accusative ? "неделю" : "неделя", "недели", "недель"], | |
self::DAY => ["день", "дня", "дней"], | |
self::HOUR => ["час", "часа", "часов"], | |
self::MIN => [$accusative ? "минуту" : "минута", "минуты", "минут"], | |
self::SEC => [$accusative ? "секунду" : "секунда", "секунды", "секунд"] | |
]; | |
return self::correctForm($time, $cases[$type]) . " "; | |
} | |
} | |
/** | |
* Examples | |
*/ | |
echo "Вытащите пирог из духовки через " . TimeUtils::timestamp2word(1251) . PHP_EOL; | |
$newYear = (mktime(0, 0, 0, 1, 1, date("Y") + 1) - time()); | |
echo "До нового года осталось: " . TimeUtils::timestamp2word($newYear, false) . PHP_EOL; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment