1
0
Files
helper/extend/tools/Time.php
2020-08-06 14:58:51 +08:00

108 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +------------------------------------------------+
// |http://www.cjango.com |
// +------------------------------------------------+
// | 修复BUG不是一朝一夕的事情等我喝醉了再说吧 |
// +------------------------------------------------+
// | Author: 小陈叔叔 <Jason.Chen> |
// +------------------------------------------------+
namespace tools;
/**
* 时间戳生成类
*/
class Time
{
/**
* [今日/某日 开始和结束的时间戳]
* @param date $date 标准的时间日期格式 Y-m-d
* @return boolean|array
*/
public static function day($date = '', $rule = 'Y-m-d')
{
if (!empty($date) && !Verify::isDate($date, $rule)) {
return false;
}
$date = !empty($date) ? $date : 'today';
$start = strtotime($date);
$end = $start + 86399;
return [$start, $end];
}
/**
* 返回本周开始和结束的时间戳
* @return array
*/
public static function week()
{
$timestamp = time();
return [
strtotime(date('Y-m-d', strtotime("+0 week Monday", $timestamp))),
strtotime(date('Y-m-d', strtotime("+0 week Sunday", $timestamp))) + 24 * 3600 - 1,
];
}
/**
* 返回本月开始和结束的时间戳
* @return array
*/
public static function month($month = '', $year = '')
{
if (empty($month)) {
$month = date('m');
} elseif (!is_numeric($month) || $month < 1 || $month > 12) {
return false;
}
if (empty($year)) {
$year = date('Y');
} elseif (!is_numeric($year) || $year < 1970 || $year > 2038) {
return false;
}
$start = mktime(0, 0, 0, $month, 1, $year);
$end = mktime(23, 59, 59, $month, date('t', $start), $year);
return [$start, $end];
}
/**
* 今年/某年 开始和结束的时间戳
* @return boolean|array
*/
public static function year($year = '')
{
if (empty($year)) {
$year = date('Y');
} elseif (!is_numeric($year) || $year < 1970 || $year > 2038) {
return false;
}
return [
mktime(0, 0, 0, 1, 1, $year),
mktime(23, 59, 59, 12, 31, $year),
];
}
/**
* [日期合法性校验]
* @param [type] $data 2019-01-01
* @return boolean
*/
public static function dateVerify($date, $rule = 'Y-m-d')
{
$unixTime = strtotime($date);
if (!$unixTime) {
return false;
}
if (date($rule, $unixTime) == $date) {
return true;
} else {
return false;
}
}
}