Skip to content

Instantly share code, notes, and snippets.

@forecho
Last active April 20, 2017 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save forecho/64e5d90064448795efef42e601043e86 to your computer and use it in GitHub Desktop.
Save forecho/64e5d90064448795efef42e601043e86 to your computer and use it in GitHub Desktop.
PHP 处理时间助手
namespace common\helpers;
class TimeHelper
{
/**
* 离现在时间几个月
* @param integer|string $time 时间戳或者时间
* @return int
*/
public static function dieMonth($time)
{
$data = (is_numeric($time) ? date('Y-m-d', $time) : $time);
return (new \DateTime($data))->diff(new \DateTime(date('Y-m-d')))->format('%m');
}
/**
* 离现在时间多少天
* @param integer|string $time 时间戳或者时间
* @return int
*/
public static function dieDay($time)
{
$data = (is_numeric($time) ? date('Y-m-d', $time) : $time);
return (new \DateTime($data))->diff(new \DateTime(date('Y-m-d')))->days;
}
/**
* 获取某天/当前天 最开始的时间戳
* @param string $time 时间戳 或者 2016-7-25 11:02:21
* @return int
*/
public static function beginTimestamp($time = '')
{
$time = ($time) ?: time();
$time = is_numeric($time) ? $time : strtotime($time);
return strtotime(date('Y-m-d', $time));
}
/**
* 获取某天/当前天 结束的时间戳 23:59:59
* @param string $time 时间戳 或者 2016-7-25 11:02:21
* @return int
*/
public static function endTimestamp($time = '')
{
return self::beginTimestamp($time) + 24 * 3600 - 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment