Last active
April 20, 2017 10:41
-
-
Save forecho/64e5d90064448795efef42e601043e86 to your computer and use it in GitHub Desktop.
PHP 处理时间助手
This file contains 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
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