Skip to content

Instantly share code, notes, and snippets.

@target-v
Created September 16, 2016 22:11
Show Gist options
  • Select an option

  • Save target-v/7f18b11beae6174c4f5d0ba5bc7b8aad to your computer and use it in GitHub Desktop.

Select an option

Save target-v/7f18b11beae6174c4f5d0ba5bc7b8aad to your computer and use it in GitHub Desktop.
<?php
namespace App;
class Time
{
/**
* @var Текущее время
*/
protected $nowTime = 0;
/**
* @var int Разница во времени
*/
protected $diffTime = 0;
/**
* @var int Время ввода
*/
protected $time = 0;
/**
* Time constructor.
*/
public function __construct()
{
$this->nowTime = time();
}
/**
* Получить Текущее время
*
* @param string $type
* @return bool|当前时间|int|string
*/
public function getNowTime($type = '')
{
if (!empty($type)) {
return date($type, $this->nowTime);
}
return $this->nowTime;
}
/**
* Форматирование времени
*
* @param $time
* @param string $type
* @return bool|string
*/
public function normal($time)
{
//判断数据是否为空
$this->checkTime($time);
$this->time = $time;
$dTime = $this->getDiffTime();
$dYear = $this->getDiffYear();
$dDay = $this->getDiffDay();
if ($dTime < 10) {
return 'just';
}
if ($dTime < 60) {
return intval(floor($dTime / 10) * 10) . "секунду назад";
}
if ($dTime < 3600) {
return intval($dTime / 60) . "Минуту назад";
}
if ($dYear == 0 && $dDay == 0) {
return date('Сегодня H:i', $time);
}
if ($dYear == 0) {
return date("В этом году m Месяц d День H:i", $time);
}
return $this->getFullTime($time);
}
/**
* @param $time Нечеткие данные
* @return string
*/
public function mohu($time)
{
//являются ли данные пустыми
$this->checkTime($time);
$this->time = $time;
$dTime = $this->getDiffTime();
$dDay = $this->getDiffDay();
if ($dTime < 60) {
return $dTime . "Секунд назад";
}
if ($dTime < 3600) {
return intval($dTime / 60) . "Минут назад";
}
if ($dTime >= 3600 && $dDay == 0) {
return intval($dTime / 3600) . "Часов назад";
}
if ($dDay > 0 && $dDay <= 7) {
return intval($dDay) . "Дней назад";
}
if ($dDay > 7 && $dDay <= 30) {
return intval($dDay / 7) . 'Недели назад';
}
if ($dDay > 30) {
return intval($dDay / 30) . 'Месяцев назад';
}
}
/**
* @return 当前时间|int 获取时间差
*/
protected function getDiffTime()
{
return $this->nowTime - $this->time;
}
/**
* @return int Получим количество лет разницы во времени
*/
protected function getDiffYear()
{
return intval(date("Y", $this->nowTime)) - intval(date("Y", $this->time));
}
/**
* @return int Получить количество дней, разница во времени
*/
protected function getDiffDay()
{
return intval(date("z", $this->nowTime)) - intval(date("z", $this->time));
}
/**
* Проверка на пустые данные
* @param $time
* @return mixed
*/
protected function checkTime($time)
{
if(empty($time)){
return $time;
}
}
/**
* Получить полное время
*
* @param $time
* @return bool|string
*/
protected function getFullTime($time)
{
return date('Y-m-d H:m:s', $time);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment